SpringBoot不分离版本上传文件提示IO异常 The temporary upload location [ 技术杂谈 ]
九叶的小窝 文章 正文

九叶
{{nature("2022-09-30 17:42:47")}}更新1. 问题描述
周一还在公交车上我看开发群里有人说给xxx医院做的系统出现问题了无法导入人员清单
异常图如下
问题是SpringBoot不分离版本长时间运行会导致项目的临时文件夹(/tmp/tomcat.xxxxxxxx.port/...)被操作系统自动清理脚本强制删除 导致上传文件的时候文件夹不存在导致IO异常抛出
2. 问题解决方案
2.1 修改操作系统对tmp文件夹的清理规则
默认规则为 tmp文件夹下 十天前的文件
在排除目录下键入
x /tmp/tomcat.*
即可排除掉项目文件 但是这样不够优雅 长时间上传文件不处理可能会占用部分存储空间
2.2 通过SpringBoot配置重定向上传目录到其他位置
@Configuration
public class MultipartConfig {
/**
* 文件上传临时路径
*/
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
String location = System.getProperty("user.dir") + "/tmp";
File tmpFile = new File(location);
if (!tmpFile.exists()) {
tmpFile.mkdirs();
}
factory.setLocation(location);
return factory.createMultipartConfig();
}
}
也可以将bean写到启动类下面
3. 其他的小设置
由于将临时文件夹重定向到外部 似乎springboot不会对文件夹过期文件进行删除
我们需要新建一个 crontab
来管理过期文件
# find 重定向目录(详细路径或同级文件夹) -mtime +天数(整数) -name '*.tmp' | grep $1_ | xargs rm
find ./tmp -mtime +29 -name '*.tmp' | grep $1_ | xargs rm
这样就可以啦
{{nature('2021-12-07 15:29:00')}} {{format('2286')}}人已阅读
{{nature('2021-01-31 20:01:00')}} {{format('1069')}}人已阅读
{{nature('2020-12-11 21:17:00')}} {{format('989')}}人已阅读
{{nature('2021-04-22 15:16:00')}} {{format('983')}}人已阅读
目录
标签云
一言
评论 0
{{userInfo.data?.nickname}}
{{userInfo.data?.email}}