问题描述

spring boot应用(使用默认的嵌入式tomcat)在上传文件时,偶尔会出现上传失败的情况,后台报错日志信息如下:“the temporary upload location is not valid”。

原因追踪

这个问题的根本原因是tomcat的文件上传机制引起的!
tomcat在处理文件上传时,会将客户端上传的文件写入临时目录,这个临时目录默认在/tmp路径下,如:“/tmp/tomcat.6574404581312272268.18333/work/tomcat/localhost/root”。
而操作系统对于/tmp目录会不定时进行清理,如果正好因为操作系统的清理导致对应的临时目录被删除,客户端再上传文件时就会报错:“the temporary upload location is not valid”。
实际上,追踪一下源码会发现,如果不明确设置tomcat的文件上传临时目录,默认读取的是servlet上下文对象的属性“javax.servlet.context.tempdir”值,如下源码:

  • org.apache.catalina.connector.request
private void parseparts(boolean explicit) {
  //...
  multipartconfigelement mce = this.getwrapper().getmultipartconfigelement();
  //...
  // 读取multipartconfigelement对象的location属性
  string locationstr = mce.getlocation();
  file location;
  if (locationstr != null && locationstr.length() != 0) {
    location = new file(locationstr);
    if (!location.isabsolute()) {
      location = (new file((file)context.getservletcontext().getattribute("javax.servlet.context.tempdir"), locationstr)).getabsolutefile();
    }
  } else {
    // 如果location属性值为空,则读取servlet上下文对象的属性“javax.servlet.context.tempdir”值(如:/tmp/tomcat.6574404581312272268.18333/work/tomcat/localhost/root)
    location = (file)context.getservletcontext().getattribute("javax.servlet.context.tempdir");
  }
  //...
}

解决办法

既然是因为上传文件的临时路径被删除导致的问题,就要确保改临时目录不会被删除。
2种解决方法:
(1)通过spring boot的配置参数“spring.servlet.multipart.location”明确指定上传文件的临时目录,确保该路径已经存在,而且该目录不会被操作系统清除。

spring.servlet.multipart.location=/data/tmp

如上所示,将上传文件的临时目录指定到路径“/data/tmp”下。

实际上,在spring boot中关于上传文件的所有配置参数如下所示:

# multipart (multipartproperties)
spring.servlet.multipart.enabled=true # whether to enable support of multipart uploads.
spring.servlet.multipart.file-size-threshold=0b # threshold after which files are written to disk.
spring.servlet.multipart.location= # intermediate location of uploaded files.
spring.servlet.multipart.max-file-size=1mb # max file size.
spring.servlet.multipart.max-request-size=10mb # max request size.
spring.servlet.multipart.resolve-lazily=false # whether to resolve the multipart request lazily at the time of file or parameter access.

(2)在spring容器中明确注册multipartconfigelement对象,通过multipartconfigfactory指定一个路径。
在上述源码追踪中就发现,tomcat会使用multipartconfigelement对象的location属性作为上传文件的临时目录。

/**
 * 配置上传文件临时目录
 * @return
 */
@bean
public multipartconfigelement multipartconfigelement() {
  multipartconfigfactory factory = new multipartconfigfactory();
  // tmp.dir参数在启动脚本中设置
  string path = system.getproperty("tmp.dir");
  if(path == null || "".equals(path.trim())) {
    path = system.getproperty("user.dir");
  }
  string location = path + "/tmp";
  file tmpfile = new file(location);
  // 如果临时目录不存在则创建
  if (!tmpfile.exists()) {
    tmpfile.mkdirs();
  }
  // 明确指定上传文件的临时目录
  factory.setlocation(location);
  return factory.createmultipartconfig();
}

参考

以上就是spring boot应用上传文件时报错的原因及解决方案的详细内容,更多关于spring boot应用上传文件时报错的资料请关注www.887551.com其它相关文章!