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.*