异常堆栈:org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is java.io.IOException: The temporary upload location [/tmp/tomcat.4486980100283550281.8098/work/Tomcat/localhost/ROOT] is not valid

原因:

在Linux 系统中,SpringBoot 应用服务在启动(java -jar 命令启动服务)的时候,会在操作系统的/tmp目录下生成一个tomcat*的文件目录,上传的文件先要转换成临时文件保存在这个文件夹下面。由于临时/tmp目录下的文件,在长时间(10天)没有使用的情况下,就会被系统机制自动删除掉。所以如果系统长时间无人问津的话,就可能导致上面这个问题。

临时解决方法:

手动在/tmp下创建相应得文件夹

server:
  tomcat:
    basedir: /home/flexible/temp

通过Java配置的方式指定缓存文件夹来解决此问题:

/**
     * 文件上传临时路径
     */
    @Bean
    MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        String location = System.getProperty("user.dir") + "/home/flexible/temp";
        File tmpFile = new File(location);
        if (!tmpFile.exists()) {
            tmpFile.mkdirs();
        }
        factory.setLocation(location);
        return factory.createMultipartConfig();
    }