GeXiangDong

精通Java、SQL、Spring的拼写,擅长Linux、Windows的开关机

0%

Spring Boot项目启动长时间后无法上传文件

遇到的问题

Spring boot项目启动过一段较长时间后,在使用上传文件相关功能时,会出现错误。错误信息和下面的类似,临时目录不存在了

Could not parse multipart servlet request; 
nested exception is java.io.IOException: 
The temporary upload location [/tmp/tomcat.7313397276953595407.8090/work/Tomcat/localhost/ROOT] is not valid。

Could not parse multipart servlet request; 
nested exception is java.io.IOException: 
The temporary upload location [C:\Users\Administrator\AppData\Local\Temp\tomcat.7174298170552445669.8002\work\Tomcat\localhost\server] is not valid

原因

这是由于tomcat默认使用的临时目录是在系统临时目录下创建的,而系统临时目录被系统清理了,删除了tomcat的临时目录。

解决方案

知道原因后解决办法也就有了:把tomcat的临时目录设置到一个固定的不会被清理的目录下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Configuration
public class MultipartConfig {

@Bean
MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
String tempLocation = "c:\\temp";
File tmpFile = new File(tempLocation);
if (!tmpFile.exists()) {
tmpFile.mkdirs();
}
factory.setLocation(tempLocation);
return factory.createMultipartConfig();
}
}