java.io.IOException: The temporary upload location [/tmp/tomcat.**.*/work/Tomcat/
一、报错原因
1.springboot的应用服务在启动时,会在操作系统的/tmp目录下生成一个Tomcat.*的文件目录,用于"java.io.tmpdir"文件流操作;
2.程序对文件的操作时:会生成临时文件,暂存在临时文件中;Linux系统的tmpwatch 命令会删除10天未使用的临时文件;长时间不进行上传操作,导致/tmp下面的tomcat临时文件目录被删除,且删除的文件不可恢复,上传文件时获取不到文件目录,导致报错
二、解决方案
方案1.重启spring boot
方案2.添加配置
java -Djava.io.tmpdir=./temp -jar 你的程序.jar -Dspring.config.location=file:./application.properties
方案3.彻底解决(推荐!!!)
在springboot的application.java中添加代码
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.servlet.MultipartConfigElement;
@MapperScan("com.cn.mapper")
//1.该处也需要配置下
@SpringBootApplication(exclude = {MultipartAutoConfiguration.class})
@EnableTransactionManagement
@EnableScheduling
public class EngineApplication {
public static void main(String[] args) {
SpringApplication.run(EngineApplication.class, args);
}
/**
* 解决文件上传,临时文件夹被程序自动删除问题
*
* 文件上传时自定义临时路径
* @return
*/
@Bean
MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
//2.该处就是指定的路径(需要提前创建好目录,否则上传时会抛出异常)
factory.setLocation("/data/uploadtmp");
return factory.createMultipartConfig();
}
}
结束说明:
springboot版本2.0.4
初次整理,个人项目亲测可行,特此记录下。