spring boot上传文件报错 The temporary upload location [] is not valid

org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.io.IOException: The temporary upload location [/tmp/tomcat.7832576988140954499.80/work/Tomcat/localhost/NGS-Report] is not valid

Caused by: java.io.IOException: The temporary upload location [/tmp/tomcat.7832576988140954499.80/work/Tomcat/localhost/NGS-Report] is not valid
    at org.apache.catalina.connector.Request.parseParts(Request.java:2846)
    at org.apache.catalina.connector.Request.parseParameters(Request.java:3215)
    at org.apache.catalina.connector.Request.getParameter(Request.java:1145)
    at org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:381)
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:75)
    ... 23 common frames omitted

原因:centos会对temp目录进行自动清理,

清理规则取决于/usr/lib/tmpfiles.d/tmp.conf文件的设定:

#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

# See tmpfiles.d(5) for details

# Clear tmp directories separately, to make them easier to override
v /tmp 1777 root root 10d
v /var/tmp 1777 root root 30d

# Exclude namespace mountpoints created with PrivateTmp=yes
x /tmp/systemd-private-%b-*
X /tmp/systemd-private-%b-*/tmp
x /var/tmp/systemd-private-%b-*
X /var/tmp/systemd-private-%b-*/tmp

从中可以看出/temp目录如果10天没有更新就会被清理。

springboot项目启动时会在/temp目录下创建tomcat.************.80该目录,用于上传文件的临时存储目录,如果超过10天没有进行上传操作,该目录就会被删除,当再次上传时就会因为找不到该目录而报错

有多种解决办法:1、手动创建该目录,再次上传,问题解决

                                  mkdir -p /tmp/tomcat.7832576988140954499.80/work/Tomcat/localhost/NGS-Report

                              2、在代码中指定临时文件存储目录

@Configuration
public class MultipartConfig {
    @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();
    }
}

3、设置/temp目录下的tomcat.************.80目录不被清理

在/usr/lib/tmpfiles.d/tmp.conf文件中增加

x /tmp/tomcat.*

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值