SprintBoot上傳文件報錯解決辦法

問題描述

上傳文件報錯java

java.io.IOException: The temporary upload location [/tmp/tomcat.4836362739890928439.2000/work/Tomcat/localhost/base] is not valid

問題分析

上傳文件存放臨時目錄找不到致使錯誤spring

解決方案

參考https://blog.csdn.net/zdyueguanyun/article/details/79271300 初始化SpringBoot的Bean:MultipartConfigElement。指定上傳臨時目錄路徑centos

@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
相關文章
相關標籤/搜索