SpringBoot项目文件上传报The temporary upload location is not vaild

背景

最近,负责的应用间歇性出现文件导入功能失效的问题,重启应用后恢复,但隔几天又出现同样的情况。提示异常The temporary upload location [...] is not vaild,如图:

异常信息

原因

该异常信息字面意思是“临时上传位置不可用”,发现是临时文件目录被Cent OS7清理导致。参见这篇博文:

CentOS7的/tmp目录自动清理规则

解决

方法一

/tmp目录的清理规则主要取决于/usr/lib/tmpfiles.d/tmp.conf文件的设定,可以在这个配置文件中增加如下配置项,避免tomcat临时目录被清理:

x /tmp/tomcat.*

方法二

在应用中增加一个Config类,自定义一个临时文件目录位置:

import java.io.File;
import javax.servlet.MultipartConfigElement;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MultipartConfig {
    @Bean
    MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        String location = System.getProperty("user.dir") + "/tempfile";
        File tmpFile = new File(location);
        if (!tmpFile.exists()) {
            tmpFile.mkdirs();
        }
        factory.setLocation(location);
        return factory.createMultipartConfig();
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。