1. 程式人生 > >The temporary upload location is not valid

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());
            }
    }
}

原文連結:https://www.jianshu.com/p/3275ce42dc1b