Causes and Solutions of Errors in Spring Boot Uploading File

  • 2021-08-16 23:52:21
  • OfStack

Problem description

The Spring Boot application (using the default embedded Tomcat) occasionally fails to upload files, and the background error log information is as follows: "The temporary upload location is not valid".

Cause tracing

The root cause of this problem is the file upload mechanism of Tomcat!
When processing file uploads, Tomcat writes files uploaded by the client to a temporary directory, which defaults to the/tmp path, such as: "/tmp/tomcat. 6574404581312272268.18333/work/Tomcat/localhost/ROOT".
However, the operating system will clean the/tmp directory from time to time. If the corresponding temporary directory is deleted just because of the cleaning of the operating system, the client will report an error when uploading files again: "The temporary upload location is not valid".
In fact, tracing the source code under 1 will find that if the file upload temporary directory of Tomcat is not explicitly set, the default reading is the attribute "javax. servlet. context. tempdir" value of Servlet context object, as follows:

org.apache.catalina.connector.Request

private void parseParts(boolean explicit) {
  //...
  MultipartConfigElement mce = this.getWrapper().getMultipartConfigElement();
  //...
  //  Read MultipartConfigElement Object's location Attribute 
  String locationStr = mce.getLocation();
  File location;
  if (locationStr != null && locationStr.length() != 0) {
    location = new File(locationStr);
    if (!location.isAbsolute()) {
      location = (new File((File)context.getServletContext().getAttribute("javax.servlet.context.tempdir"), locationStr)).getAbsoluteFile();
    }
  } else {
    //  If location Property value is empty, read the Servlet Attributes of the context object " javax.servlet.context.tempdir "Values such as: /tmp/tomcat.6574404581312272268.18333/work/Tomcat/localhost/ROOT ) 
    location = (File)context.getServletContext().getAttribute("javax.servlet.context.tempdir");
  }
  //...
}

Solution

Since the problem is caused by deleting the temporary path of the uploaded file, make sure that changing the temporary directory will not be deleted.
Two solutions:
(1) The temporary directory for uploading files is explicitly specified through the configuration parameter "spring. servlet. multipart. location" of Spring Boot, ensuring that the path already exists and that the directory is not cleared by the operating system.


spring.servlet.multipart.location=/data/tmp

As shown above, assign the temporary directory where the files are uploaded to the path "/data/tmp".

In fact, all the configuration parameters for uploading files in Spring Boot are as follows:


# MULTIPART (MultipartProperties)
spring.servlet.multipart.enabled=true # Whether to enable support of multipart uploads.
spring.servlet.multipart.file-size-threshold=0B # Threshold after which files are written to disk.
spring.servlet.multipart.location= # Intermediate location of uploaded files.
spring.servlet.multipart.max-file-size=1MB # Max file size.
spring.servlet.multipart.max-request-size=10MB # Max request size.
spring.servlet.multipart.resolve-lazily=false # Whether to resolve the multipart request lazily at the time of file or parameter access.

(2) Explicitly register the MultipartConfigElement object in the Spring container, and specify a path through MultipartConfigFactory.
As we found in the above source tracing, Tomcat will use MultipartConfigElement The location property of the object is used as a temporary directory for uploading files.


/**
 *  Configure the temporary directory for uploading files 
 * @return
 */
@Bean
public MultipartConfigElement multipartConfigElement() {
  MultipartConfigFactory factory = new MultipartConfigFactory();
  // tmp.dir Parameters are set in the startup script 
  String path = System.getProperty("tmp.dir");
  if(path == null || "".equals(path.trim())) {
    path = System.getProperty("user.dir");
  }
  String location = path + "/tmp";
  File tmpFile = new File(location);
  //  If the temporary directory does not exist, create 
  if (!tmpFile.exists()) {
    tmpFile.mkdirs();
  }
  //  Explicitly specify the temporary directory for uploading files 
  factory.setLocation(location);
  return factory.createMultipartConfig();
}

Reference

https://stackoverflow.com/questions/50523407/the-temporary-upload-location-tmp-tomcat-4296537502689403143-5000-work-tomcat/50523578

The above is the Spring Boot application upload file times error reason and the solution detailed content, more about Spring Boot application upload file times error information please pay attention to this site other related articles!


Related articles: