spring-boot上传文件,临时文件地址无效

org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.io.IOException: The temporary upload location [/tmp/tomcat.2737591724424319502.8062/work/Tomcat/localhost/ROOT] is not valid
临时文件地址无效

参考链接:重点设置临时目录的绝对路径  System.getProperty("user.dir") + "/data/tmp";

解决办法: 启动类中加入配置临时文件目录


    @Bean
    MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        String location = System.getProperty("user.dir") + "/data/tmp";
        File tmpFile = new File(location);
        if (!tmpFile.exists()) {
            tmpFile.mkdirs();
        }
        factory.setLocation(location);
        return factory.createMultipartConfig();
    }
 

1.重启服务。

2.增加启动参数-Djava.io.tmpdir=自定义目录

3.注入配置:

@Bean  
 MultipartConfigElement multipartConfigElement() {  
    MultipartConfigFactory factory = new MultipartConfigFactory();  
    factory.setLocation("/app/pttms/tmp");  
    return factory.createMultipartConfig();  
}
 

你可能感兴趣的:(SpringBoot,spring,boot)