一、MultipartFile 上传文件临时目录自动清理机制隐患
1、默认存储在/tmp下, 但是linux的/tmp有自动清理的机制,
cat /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
2、改变默认位置
采用配置类:
@Configuration
public class MultipartConfig {
/**
* 文件上传临时路径
*/
@Bean
MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
String location = "自定义临时文件路径";
File tmpFile = new File(location);
if (!tmpFile.exists()) {
tmpFile.mkdirs();
}
factory.setLocation(location);
return factory.createMultipartConfig();
}
}
配置文件方式:
#指定multipartFile临时文件存放路径,但是内置的tomcat创建的临时文件夹路径还是不变的
spring.servlet.multipart.location=临时文件路径
#指定springboot启动时内置tomcat创建的临时文件夹的路径
#server.tomcat.basedir=/data/ops/app/cms/cache