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

How to configure the temporary file storage directory in springboot

WBOY
Release: 2023-05-12 23:01:04
forward
4091 people have browsed it

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
Copy after login

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
Copy after login

3. Configuration class configuration temporary file storage directory

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

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的系统相关位置
Copy after login

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();
   }
}
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template