問題:centos7上spring boot項目上傳文件偶爾會報錯,原因是springboot上傳文件會在本地有一個緩衝池,在centos7上,如果項目時間長沒使用過,這個目錄會被centos7刪除掉,然後上傳文件的時候就會報錯。
/**
* 文件上傳臨時路徑
*/
@Bean
public MultipartConfigElement multipartConfigElement(){
MultipartConfigFactory multipartConfigFactory = new MultipartConfigFactory();
String location = System.getProperty("user.dir") + "/data/tmp";
File file = new File(location);
if(!file.exists()){
file.mkdirs();
}
multipartConfigFactory.setLocation(location);
return multipartConfigFactory.createMultipartConfig();
}
解決辦法:把上面代碼寫到啓動類裏面。
原理:把springboot上傳文件時候改變臨時目錄,不使用centos7提供的目錄。
完美解決