首页  

springboot文件上传异常分析及解决方案     所属分类 springboot 浏览量 1344
文件上传异常
The temporary upload location xxx is not valid  

/tmp/tomcat.888888888.8088/work/Tomcat/localhost/ROOT
发现该目录不存在
手工创建该目录后,文件上传ok

增加配置 指定目录 (这个其实没啥作用,应该是别的用途)
server.tomcat.basedir=/tmp 
目录  存在 有权限 且会自动清理 

系统会自动清理tmp目录

可配置不删除指定目录
/usr/lib/tmpfiles.d/tmp.conf
# 添加一行
x /tmp/tomcat.*



手工配置目录 推荐这种方式

@Bean
MultipartConfigElement multipartConfigElement() {
    MultipartConfigFactory factory = new MultipartConfigFactory();
    factory.setLocation("/tmp");
    return factory.createMultipartConfig();
}



FileController  
文件上传处理 , 返回相关信息 
譬如临时文件路径 ,原始文件名,文件类型,大小等信息


MultipartConfigElement  getLocation 断点 ,跟踪到关键代码 

org.apache.catalina.connector.Request.parseParts(boolean)

org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(RequestContext)




{
    "path": "/private/tmp/upload_7b51dd99_608c_4d44_8068_f8175ee653c2_00000000.tmp",
    "fileInputStream": "sun.nio.ch.FileChannelImpl@682014b1",
    "size": 300,
    "contentType": "text/plain",
    "class": "org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile",
    "inputStreamInfo": "java.io.FileInputStream@179b7a06",
    "originalFilename": "test.txt",
    "info": "org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile@2e605d7"
}

手工配置目录 后 生成的临时文件
/private/tmp/upload_5a48fb1c_0520_4c12_9cfc_28cfe5cf04b7_00000002.tmp

默认
/private/var/folders/w6/2wzjcqzn4njd5z_df4whdzzr0000gn/T/tomcat.6039888980664124566.8090/work/Tomcat/localhost/ROOT/upload_0a839e25_7d17_425d_829f_fc4a64e321f0_00000000.tmp

父目录被删掉 ,会报错

文件上传产生的临时文件会被清理 


这个问题最好的办法是在框架代码里处理,临时目录不存在则创建



查看环境信息
http://127.0.0.1:8090/actuator/env

java.io.tmpdir
/var/folders/w6/2wzjcqzn4njd5z_df4whdzzr0000gn/T/

org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is java.io.IOException: The temporary upload location [/private/var/folders/w6/2wzjcqzn4njd5z_df4whdzzr0000gn/T/tomcat.2462919395664695755.8090/work/Tomcat/localhost/ROOT] is not valid


http://127.0.0.1:8090/upload.html



FileController 完整代码
https://gitee.com/dyyx/springboothello/blob/master/src/main/java/demo/controller/FileController.java



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 1MBMax 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.
springboot配置大全

上一篇     下一篇
jdk里的泛型

springboot2单元测试

项目管理八大原则

linux kill 命令

springboot依赖本地jar打包配置

eclipse安装lombok