好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

springboot临时文件存储目录配置方式

springboot临时文件存储目录配置

场景:

上传文件功能报错,然后排查日志。

报错日志:

The temporary upload location [/tmp/tomcat.7957874575370093230.8088/work/Tomcat/localhost/ROOT] is not valid

原因:

在linux系统中,springboot应用服务再启动(java -jar 命令启动服务)的时候,会在操作系统的/tmp目录下生成一个tomcat*的文件目录,上传的文件先要转换成临时文件保存在这个文件夹下面。

由于临时/tmp目录下的文件,在长时间(10天)没有使用的情况下,就会被系统机制自动删除掉。所以如果系统长时间没有使用到临时文件夹,就可能导致上面这个问题。

解决办法:

1.创建临时文件夹:

?

1

mkdir -p /tmp/tomcat .7957874575370093230.8088 /work/Tomcat/localhost/ROOT

后面可能还会出现这种情况

2.application.properties重新配置一个文件目录,然后重启项目

?

1

2

# 存放Tomcat的日志、Dump等文件的临时文件夹,默认为系统的tmp文件夹

server.tomcat.basedir= /data/apps/temp

3.配置类配置临时文件存储目录

?

1

2

3

4

5

6

@Bean

MultipartConfigElement multipartConfigElement() {

     MultipartConfigFactory factory = new MultipartConfigFactory();

     factory.setLocation(tmepPath);

     return factory.createMultipartConfig();

}

Springboot修改临时文件的存储位置

报错

项目在线运行了一段时间后,上传文件时抛出如下异常:

The temporary upload location [/tmp/tomcat.*.80/work/Tomcat/localhost/ROOT] is not valid

经过查找,采用了如下的解决方式【修改临时文件的位置】

在application.yml 文件中添加

?

1

2

location:

   tempDir: /opt/location/tempDir #此处为*unix的系统相关位置

项目中添加配置类

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

@Configuration

public class MultipartConfig {

    @Value ( "${location.tempDir:/opt/tempDir}" )

    private String tempDir;

 

    @Bean

    MultipartConfigElement multipartConfigElement() {

       MultipartConfigFactory factory = new MultipartConfigFactory();

       File tmpDirFile = new File(tempDir);

       // 判断文件夹是否存在

       if (!tmpDirFile.exists()) {

          tmpDirFile.mkdirs();

       }

       factory.setLocation(tempDir);

       return factory.createMultipartConfig();

    }

}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://blog.csdn.net/programmeryu/article/details/91606979

查看更多关于springboot临时文件存储目录配置方式的详细内容...

  阅读:30次