在部署springboot项目,使用multipartfile上传文件会出现以下异常
failed to parse multipart servlet request; nested exception is java.io.ioexception: the temporary upload location [/tmp/tomcat.3016596448718765136.18001/work/tomcat/localhost/xx] is not valid"
出现这种情况的原因是由于在部署项目的时候没有指定应用的临时io目录,spring会直接使用操作系统的临时目录,而操作系统的临目录会在一定的周期类回收,导致隔一段时间后上传文件会出现上述问题。
解决上述文件有很多种方式,根据网友经验,总结以下四种设置临时目录的方法:
一、配置tomcat的basedir
server:
tomcat:
basedir: /usr/local/temp二、配置multipart的location
spring:
servlet:
multipart:
location: /usr/local/temp三、启动jar指定tmpdir
java -jar -djava.io.tmpdir=/usr/local/temp xxx.jar
四、编码注入
@bean
public multipartconfigelement multipartconfigelement() {
multipartconfigfactory factory = new multipartconfigfactory();
factory.setlocation("/usr/local/temp");
return factory.createmultipartconfig();
}
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论