刚启动程序, 上传图片, 一切正常, 过了几天后, 居然报错了:

org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; 
nested exception is java.io.IOException: The temporary upload location [/tmp/tomcat-docbase.4594736703313813956.8103/work/Tomcat/localhost/ROOT] is not valid
org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.parseRequest(StandardMultipartHttpServletRequest.java:112)
org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.(StandardMultipartHttpServletRequest.java:86)
.......

异常信息上看, 找不到目录”/tmp/tomcat-docbase.4594736703313813956.8103/work/Tomcat/localhost/ROOT”

这个目录是springboot启动后会默认创建的, 文件先上传到这个目录,再做处理, 处理完后会自动删掉.

因为在/tmp下, 所以一段时间后linux会自动清理掉这个目录, 所以就会报找不到目录的问题.

处理方案:

  1. 重启服务,重新生成目录;或者 手动创建该目录 (紧急处理)
  2. 修改配置: 在 application.yml中加入(前提是用tomcat)
server:
  tomcat:
    basedir: /data/app/tmp

3. 向spring容器中,注入自定义的MultipartConfigElement

@Inject
private Environment environment;

@Bean
@ConditionalOnProperty( {"app.dir.tmp"})
public MultipartConfigElement multipartConfigElement() {
  String baseDir = environment.getProperty("app.dir.tmp");
  MultipartConfigFactory configFactory = new MultipartConfigFactory();
  configFactory.setLocation(baseDir);
  return configFactory.createMultipartConfig();
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注