Home  >  Article  >  Java  >  How to configure the temporary file storage directory in springboot

How to configure the temporary file storage directory in springboot

WBOY
WBOYforward
2023-05-12 23:01:043743browse

springboot temporary file storage directory configuration

Scenario:

The upload file function reports an error, and then check the log.

Error log:

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

Cause:

In the Linux system, when the springboot application service is restarted (the java -jar command starts the service), a tomcat* file will be generated in the /tmp directory of the operating system. Directory, the uploaded files must first be converted into temporary files and saved under this folder.

Because the files in the temporary /tmp directory are not used for a long time (10 days), they will be automatically deleted by the system mechanism. Therefore, if the system has not used the temporary folder for a long time, it may cause the above problem.

Solution:

1. Create a temporary folder:

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

This may happen again later

2. Reconfigure application.properties A file directory, and then restart the project

# 存放Tomcat的日志、Dump等文件的临时文件夹,默认为系统的tmp文件夹
server.tomcat.basedir=/data/apps/temp

3. Configuration class configuration temporary file storage directory

@Bean
    MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setLocation(tmepPath);
        return factory.createMultipartConfig();
    }

Springboot modifies the storage location of temporary files

Error reporting

After the project has been running online for a period of time, the following exception is thrown when uploading files:

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

After searching, the following solution was adopted [Modify the location of the temporary file]

Add to the application.yml file

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

Add the configuration class to the project

@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();
   }
}

The above is the detailed content of How to configure the temporary file storage directory in springboot. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete