Application error when uploading files Spring Boot

Spring Boot

table of Contents

Problem Description

Spring Boot application (using the default embedded Tomcat) when you upload a file, occasionally upload failures occur, the background error log message as follows: "The temporary upload location is not valid".

The reason Track

The root cause of this problem is caused by the Tomcat file upload mechanism!
Tomcat when dealing with file uploads, the client will upload files written to a temporary directory, the default temporary directory in / tmp path, such as: "/ tmp / tomcat.6574404581312272268.18333 / work / Tomcat / localhost / ROOT".
The operating system for the / tmp directory will from time to time to clean up, if just because the error will clean up the operating system results in a corresponding temporary directory is deleted, the client then upload the file: "The temporary upload location is not valid".
In fact, the track will be found at the source, if not explicitly set Tomcat file upload temporary directory, the default read is the property Servlet context object "javax.servlet.context.tempdir" value, the following source code:

  • org.apache.catalina.connector.Request
private void parseParts(boolean explicit) {
    //...
    MultipartConfigElement mce = this.getWrapper().getMultipartConfigElement();
    //...
    // 读取MultipartConfigElement对象的location属性
    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 {
        // 如果location属性值为空,则读取Servlet上下文对象的属性“javax.servlet.context.tempdir”值(如:/tmp/tomcat.6574404581312272268.18333/work/Tomcat/localhost/ROOT)
        location = (File)context.getServletContext().getAttribute("javax.servlet.context.tempdir");
    }
    //...
}

Solution

Since it is temporary because the path to the uploaded file is deleted problems caused, make sure to change the temporary directory is not deleted.
Two kinds of solutions:
(1) "spring.servlet.multipart.location" explicitly specified by the configuration parameters Spring Boot temporary directory to upload files, make sure that the path already exists, and that the directory is not cleared by the operating system.

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

As described above, the temporary directory to upload files to the specified path "/ data / tmp" below.

In fact, in Spring Boot all the configuration parameters on uploading files is 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 MultipartConfigElement Spring container object, specify a path through the MultipartConfigFactory.
Found in the above source tracing, Tomcat will use the MultipartConfigElementobject's location attribute as a temporary directory to upload files.

/**
 * 配置上传文件临时目录
 * @return
 */
@Bean
public MultipartConfigElement multipartConfigElement() {
    MultipartConfigFactory factory = new MultipartConfigFactory();
    // tmp.dir参数在启动脚本中设置
    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 (!tmpFile.exists()) {
        tmpFile.mkdirs();
    }
    // 明确指定上传文件的临时目录
    factory.setLocation(location);
    return factory.createMultipartConfig();
}

[Reference]
https://stackoverflow.com/questions/50523407/the-temporary-upload-location-tmp-tomcat-4296537502689403143-5000-work-tomcat/50523578 at The LOCATION IS the Upload the Temporary not! Valid
HTTPS: //blog.csdn the .net / llibin1024530411 / article / details / 79474953 SpringBoot project temporary upload location *** is not valid issue

Guess you like

Origin www.cnblogs.com/nuccch/p/11546494.html