在linux系统中,springboot应用服务再启动(java -jar 命令启动服务)的时候,会在操作系统的/tmp目录下生成一个tomcat*的文件目录,上传的文件先要转换成临时文件保存在这个文件夹下面。
因为流取一次消费之后,后面无法再从流中获取数据,所以缓存方便后续复用;
上线后可能tomcat临时文件夹会被Linux删除,会报找不到错误,现在赶紧记录一下,已被不时之需
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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 |
既然目录被删除了,重启一下服务,让系统重新生成该目录,临时解决(但是以后目录还可能被删除)
配置一下不删除tmp目录下的tomcat
1 2 3 4 5 6 7 8 |
vim /usr/lib/tmpfiles.d/tmp.conf
# 添加下面一行
x /tmp/tomcat.*
# 重启服务 systemctl restart systemd-tmpfiles-clean |
1 2 |
#定临时目录为/app/xxx/tmp -Djava.io.tmpdir=/app/xxx/tmp(自定义路径) |
1 |
-java.tmp.dir=/data/upload_tmp |
1 2 3 4 |
spring: http: multipart: location: /data/upload_tmp |
在Spring容器中注册MultipartConfigElement对象,通过MultipartConfigFactory指定路径,路径不存在的话就创建
1 2 3 4 5 6 7 8 9 10 11 |
@Bean public 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(); } |