The temporary upload location is not valid

java.io.IOException: The temporary upload location [/tmp/tomcat.1593253653386650830.8220/work/Tomcat/localhost/ROOT] is not valid

最近我们的几个服务有点问题: 本来好好的服务, 跑个10几天某些接口就调不通了返回500, 一看日志全是如上异常.

原因

Tomcat需要创建临时目录来存放上传的文件, 当POST请求的content-type是multipart/form-data的时候就会访问这个目录, 而这个目录在Linux系统中默认建在/tmp目录下, 10天就会被清除, 引发上述异常.

解决方法:

增加配置: server.tomcat.basedir=/yourpath/${spring.application.name} 这样就可以自定义临时目录

附代码

Tomcat.java

    protected void initBaseDir() {
        String catalinaHome = System.getProperty(Globals.CATALINA_HOME_PROP);
        if (basedir == null) {
            basedir = System.getProperty(Globals.CATALINA_BASE_PROP);
        }
        if (basedir == null) {
            basedir = catalinaHome;
        }
        if (basedir == null) {
            // Create a temp dir.
            basedir = System.getProperty("user.dir") +
                "/tomcat." + port;
        }

        File baseFile = new File(basedir);
        baseFile.mkdirs();
        try {
            baseFile = baseFile.getCanonicalFile();
        } catch (IOException e) {
            baseFile = baseFile.getAbsoluteFile();
        }
        server.setCatalinaBase(baseFile);
        System.setProperty(Globals.CATALINA_BASE_PROP, baseFile.getPath());
        basedir = baseFile.getPath();

        if (catalinaHome == null) {
            server.setCatalinaHome(baseFile);
        } else {
            File homeFile = new File(catalinaHome);
            homeFile.mkdirs();
            try {
                homeFile = homeFile.getCanonicalFile();
            } catch (IOException e) {
                homeFile = homeFile.getAbsoluteFile();
            }
            server.setCatalinaHome(homeFile);
        }
        System.setProperty(Globals.CATALINA_HOME_PROP,
                server.getCatalinaHome().getPath());
    }

EmbeddedServletContainerFactory.java

    public EmbeddedServletContainer getEmbeddedServletContainer(
            ServletContextInitializer... initializers) {
        Tomcat tomcat = new Tomcat();
        File baseDir = (this.baseDirectory != null ? this.baseDirectory
                : createTempDir("tomcat"));
        tomcat.setBaseDir(baseDir.getAbsolutePath());
        Connector connector = new Connector(this.protocol);
        tomcat.getService().addConnector(connector);
        customizeConnector(connector);
        tomcat.setConnector(connector);
        tomcat.getHost().setAutoDeploy(false);
        configureEngine(tomcat.getEngine());
        for (Connector additionalConnector : this.additionalTomcatConnectors) {
            tomcat.getService().addConnector(additionalConnector);
        }
        prepareContext(tomcat.getHost(), initializers);
        return getTomcatEmbeddedServletContainer(tomcat);
    }

ServerProperties.java

@ConfigurationProperties(
    prefix = "server",
    ignoreUnknownFields = true
)
public class ServerProperties implements EmbeddedServletContainerCustomizer, EnvironmentAware, Ordered {

    private File basedir;

    void customizeTomcat(ServerProperties serverProperties, TomcatEmbeddedServletContainerFactory factory) {
            if(this.getBasedir() != null) {
                factory.setBaseDirectory(this.getBasedir());
            }
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在今天我们部门没能完成缴费目标,做为团队的一员,没能承担起应有的责任,在此做出深刻的检讨。首先,在今天二次上门当中...
    穿墙小石头阅读 382评论 0 0
  • 一 又是一年春正暖,为君首赋鹧鸪天。 撑开纸伞青石巷,摇落乌篷碧玉潭。 多少梦,在江南,相思总惹夜不眠。 无言独自...
    清江月影映西楼阅读 1,106评论 16 24
  • 1. 前台部门的工作,比起后台,杂乱无章很多。 每天开门营业,你都不知道,会碰到什么。这样也有一个好处,它迫使你活...
    文晓玲阅读 461评论 7 6
  • 1.安排好的事情是如此必要却还是拖着不做?! 2.错误的观念又出现了,怎么办?!
    一觉天亮阅读 153评论 0 0
  • 187、2018-2-11-3:09 爸爸,我希望看见你开开心心地笑 昨夜父子俩一起去洗澡,回来针尖对麦芒!儿子要...
    Zole瑜阅读 282评论 0 0