当前位置 : 主页 > 编程语言 > java >

MultipartConfigFactory服务器默认tmp临时目录失效问题

来源:互联网 收集:自由互联 发布时间:2023-10-10
问题: MultipartException: Failed to parse multipart servlet request; nested exception is java.lang.Runtime 错误日志: org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested excepti

问题:

MultipartException: Failed to parse multipart servlet request; nested exception is java.lang.Runtime

错误日志:

org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is java.lang.RuntimeException: java.nio.file.NoSuchFileException: /tmp/undertow.8081.6091954911906268442/undertow2435234810596519507upload
        at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.handleParseFailure(StandardMultipartHttpServletRequest.java:124)
        at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.parseRequest(StandardMultipartHttpServletRequest.java:115)
        at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.<init>(StandardMultipartHttpServletRequest.java:88)
        at org.springframework.web.multipart.support.StandardServletMultipartResolver.resolveMultipart(StandardServletMultipartResolver.java:87)
        at org.springframework.web.servlet.DispatcherServlet.checkMultipart(DispatcherServlet.java:1178)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1012)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
        at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)

问题缘由

在 Linux 系统中,Spring Boot 应用以 java -jar 命令启动时,会在操作系统的 /tmp 目录下生成一个 tomcat(或 undertow )临时目录,上传的文件先要转换成临时文件保存在这个文件夹下面。由于临时 /tmp 目录下的文件,在长时间(10天)没有使用的情况下,系统执行了 tmp 目录清理服务(systemd-tmpfiles-clean.service),导致 /tmp/undertow…8090 文件被清理,然而在上传的时候,undertow 服务器需要创建/tmp/undertow…8090/undertow…upload 临时文件,但是调用 Files.createFile(…) 的时候就会发现找不到父目录,才导致了以上的错误。

解决方案

1)重启

无法一劳永逸

2)配置文件指定目录

需要在服务器手动创建文件夹

mkdir -p /data/tmp

MultipartConfigFactory服务器默认tmp临时目录失效问题_java

3)添加启动参数 -java.tmp.dir=/path/to/application/temp/

或者启动服务时自动创建临时目录

@Configuration
public class SpringCustomizedConfig {
    private static final Logger log = LoggerFactory.getLogger(SpringCustomizedConfig.class);

    public SpringCustomizedConfig() {
    }

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory multipartConfigFactory = new MultipartConfigFactory();
        String userDirPath = System.getProperty("user.dir");
        log.info("*********[SpringCustomizedConfig][multipartConfigElement] user.dir is : {}.", userDirPath);
        String uploadPath = userDirPath + "/multipartUpload/";
        log.info("*********[SpringCustomizedConfig][multipartConfigElement]  uploadPath is : {}.", uploadPath);
        File file = new File(uploadPath);

        try {
            if (!file.exists()) {
                file.mkdirs();
            }
        } catch (Exception var6) {
            log.error("*********[SpringCustomizedConfig][multipartConfigElement] upload folder create failed, exception ", var6);
        }

        multipartConfigFactory.setLocation(uploadPath);
        return multipartConfigFactory.createMultipartConfig();
    }
}

上一篇:创建线程的三种方式
下一篇:没有了
网友评论