问题描述
上传文件报错
java.io.IOException: The temporary upload location [/tmp/tomcat.4836362739890928439.2000/work/Tomcat/localhost/base] is not valid
问题分析
上传文件存放临时目录找不到导致错误
解决方案
参考https://blog.csdn.net/zdyueguanyun/article/details/79271300 初始化SpringBoot的Bean:MultipartConfigElement。指定上传临时目录路径
@Configuration
public class CommonConfiguration implements Serializable {
// Spring Boot上传需要指定一个临时文件目录,否则上传会报错
[@Bean](https://my.oschina.net/bean)
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
String dirName = "springboot-upload-temp";
String localTempFileDir = System.getProperty("user.home") + File.separator + dirName;
File dir = new File(localTempFileDir);
if (!dir.exists()) {
dir.mkdirs();
}
factory.setLocation(localTempFileDir);
return factory.createMultipartConfig();
}
}
效果
[ew119@VM_4_13_centos springboot-upload-temp]$ pwd
/home/ew119/springboot-upload-temp