打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
使用MultipartFile做文件上传报FileNotFoundException问题总结
                                             --------------------------------------使用MultipartFile做文件上传报FileNotFoundException问题总结--------------------------------------
public String upload(@RequestParam("file") MultipartFile multipartFile)
问题:使用spring的MultipartFile做文件上传操作时,报java.io.IOException: java.io.FileNotFoundException: /tmp/tomcat.273391201583741210.8080/work/Tomcat/localhost/ROOT/tmp/source/IMG_20160129_132623.jpg (No such file or directory)
原因:当使用MultipartFile做上传操作时,
      1:spring是先将上传文件存放在一个临时地址,默认/tmp目录下
      2:进入controller进行业务操作
      linux环境中/tmp目录是存放临时文件的,当这个目录下的子目录10天之内,没有使用过,linux就会将目录删掉,然后上传文件时就会在第一步发生异常


修改方式:第一种方式:将linux下的存放应用的临时文件的那个子目录,非/tmp目录,设置为不删除 tmpwatch命令

         第二种方式:设置MultipartConfig的location,将临时文件的存放地址,改为非/tmp目录

                             

@BeanMultipartConfigElement multipartConfigElement() {    MultipartConfigFactory factory = new MultipartConfigFactory();    factory.setLocation("d://123/");    return factory.createMultipartConfig();}

         第三种方式:修改tomcat的临时文件存放目录,spring boot中使用server.tomcat.basedir=d://file/参数配置

server.tomcat.basedir=d://123/

相关源码:
--->org.apache.catalina.connector.Request#parseParts
------>MultipartConfigElement mce = this.getWrapper().getMultipartConfigElement()获取MultipartConfig,没有自定义配置时,取默认
mce = {MultipartConfigElement@6283} 
location = ""
  value = {char[0]@6329} 
  hash = 0
maxFileSize = 1048576
maxRequestSize = 10485760
fileSizeThreshold = 0


------> 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 {
                    //当没有配置MultipartConfig存放文件的存放目录时,存放在下面获取到的目录(spring boot的临时文件存放目录)下,spring boot启动时会生成一个下面参数对应的路径
                    location = (File)context.getServletContext().getAttribute("javax.servlet.context.tempdir");
                }
--->org.apache.tomcat.util.http.fileupload.DeferredFileOutputStream#thresholdReached
------>this.memoryOutputStream.writeTo(fos);//保存临时文件的方法                                    
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Tomcat源代码编译跟导入Eclipse
war文件
SpringMVC文件上传下载
springboot2.x文件上传
Xshell查看服务器日志
Linux中Tomcat 自动设置CATALINA
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服