1.生成机制
在linux系统中,springboot应用服务再启动(java -jar 命令启动服务)的时候,会在操作系统的/tmp目录下生成一个tomcat*的文件目录,上传的文件先要转换成临时文件保存在这个文件夹下面。
因为流取一次消费之后,后面无法再从流中获取数据,所以缓存方便后续复用;
2.产生异常
上线后可能tomcat临时文件夹会被linux删除,会报找不到错误,现在赶紧记录一下,已被不时之需
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
3.解决办法
3.1 重启大法
既然目录被删除了,重启一下服务,让系统重新生成该目录,临时解决(但是以后目录还可能被删除)
3.1 从linux层面修改 /tmp目录的清理策略
配置一下不删除tmp目录下的tomcat
vim /usr/lib/tmpfiles.d/tmp.conf # 添加下面一行 x /tmp/tomcat.* # 重启服务 systemctl restart systemd-tmpfiles-clean
3.2 增加jvm配置
#定临时目录为/app/xxx/tmp -djava.io.tmpdir=/app/xxx/tmp(自定义路径)
3.3 增加jvm配置
-java.tmp.dir=/data/upload_tmp
3.4 添加spring boot配置
spring: http: multipart: location: /data/upload_tmp
3.5 使用配置类配置
在spring容器中注册multipartconfigelement对象,通过multipartconfigfactory指定路径,路径不存在的话就创建
@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(); }
到此这篇关于springboot /tmp 临时目录的具体实现的文章就介绍到这了,更多相关springboot /tmp 临时目录内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论