springboot /tmp 临时目录的具体实现

springboot应用服务再启动的时候,会在操作系统的/tmp目录,本文主要介绍了springboot /tmp 临时目录的具体实现,具有一定的参考价值,感兴趣的可以了解一下

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 临时目录内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持vb.net教程C#教程python教程SQL教程access 2010教程xin3721自学网

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值