The temporary upload location [/tmp/omcat...]is not valid临时文件tmp回收异常的处理

最近项目比较忙,总结下最近遇到的比较棘手的问题
前期开发的一个springboot项目中包含上传功能模块,春节期间长时间( >10天)没有用上传功能导致tmp临时流中转文件夹被回收了,从而导致相应功能报错无法加载页面,报错信息如下:

Caused by: java.io.IOException: The temporary upload location [/tmp/tomcat.5992714642279188607.8088/work/Tomcat/localhost/kb-test] is not valid
	at org.apache.catalina.connector.Request.parseParts(Request.java:2821)
	at org.apache.catalina.connector.Request.parseParameters(Request.java:3185)
	at org.apache.catalina.connector.Request.getParameter(Request.java:1116)
	at org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:381)
	at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:84)
	... 28 common frames omitted
2021-02-20 11:30:46.143 [http-nio-8088-exec-4] ERROR o.jeecg.common.exception.JeecgBootExceptionHandler:57 - Failed to parse multipart servlet request; nested exception is java.io.IOException: The temporary upload location [/tmp/tomcat.5992714642279188607.8088/work/Tomcat/localhost/kb-test] is not valid
org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is java.io.IOException: The temporary upload location [/tmp/tomcat.5992714642279188607.8088/work/Tomcat/localhost/kb-test] is not valid
	at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.handleParseFailure(StandardMultipartHttpServletRequest.java:123)

当时着急,重启了一下项目生成临时tmp文件暂时解决了问题。
在网上找了一些相关的资料,今天把这个临时文件配置了一下,先看一下为什么这个临时文件会过期回收,在系统的/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.conf最下面可以设置排除过期回收来解决问题

x /tmp/tomcat.*

但是项目在运行这个资源被暂用,设置不一定能成功。

第二方案:
1、在application.yml中配置临时路径

#配置上传临时文件路径本地测试为window而部署为linux所以定义了两个路径
location:
  localTemp: C://temp #此处为windows的系统相关位置
  serverTemp: /usr/temp #此处为*unix的系统相关位置

2、定义配置类

@Configuration
public class MultipartConfig {

    @Value("${location.localTemp}")
    private String localTemp;

    @Value("${location.serverTemp}")
    private String serverTemp;

    private String temp;

    private static  String serveType;

    static{
        serveType = System.getProperty("os.name");//获取操作系统的名称
        if(serveType.toLowerCase().contains("window")){//赋值系统类型为windows
            serveType = "windows";
        }else{//赋值系统类型为linux
            serveType = "linux";
        }
    }

    @Bean
    MultipartConfigElement multipartConfigElement(){
        MultipartConfigFactory factory = new MultipartConfigFactory();
        if(serveType.equals("windows")){
            temp = localTemp;
        }else{
            temp = serverTemp;
        }
        File tempDirFile = new File(temp);
        //判断文件是否存在
        if(!tempDirFile.exists()){
            //不存在则创建文件
            tempDirFile.mkdirs();
        }
        factory.setLocation(temp);
        return factory.createMultipartConfig();
    }
}

这样项目部署运行后会自动根据系统类型创建临时目录,问题解决。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值