Solve the problem of Processing of multipart/form-data request failed. /upload/A.tmp (No such file or directory)

Using spring boot to upload, everything works fine at first, but an error occurs when uploading the file after a while.

ERROR org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[dispatcherServlet]  - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. C:\Users\hp\AppData\Local\Temp\tomcat.7007283165982469173.7102\work\Tomcat\localhost\ROOT\upload_82a53f5c_e2da_43a5_9b71_d5349d403a82_00000067.tmp (系统找不到指定的路径。)] with root cause

what is the root cause? The error message has already been said, but the path cannot be found. This path is the directory of the tomcat temporary file.

So why is everything working fine in the first place? And after a while, the error is reported?

In fact, this is related to the survival time of the temporary folder. If we do not specify the tomcat running root directory, then when the sping boot project starts, the system will automatically create a project running temporary directory, usually in the \App Data\Local\Temp\ directory. This temporary file has its own cleanup policy and is automatically cleaned up after a period of time. So why did everything work fine at the beginning, but after a period of time, it reported that the path could not be found.

There are a lot of solutions on the web, but many don't really work, like:

The first one: Modify maxInMemorySize. It is said that if the upload file size is smaller than this parameter, no temporary file will be generated. So make it smaller, but it doesn't help.

<bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8" />
        <!-- Specifies that the total size of the uploaded files cannot exceed 2000KB. Note that the limit of the maxUploadSize property is not for a single file, but the sum of the capacities of all files -->
        <property name="maxUploadSize" value="2048000" />
        <!-- Set the maximum value that is allowed to be written to memory during file upload, in bytes, the default is 10240 -->
        <property name="maxInMemorySize" value="2048" />  
    </bean>
location
The second: The modified value location can be understood as a temporary file directory, which can be configured location by the value, but it has no effect under spring boot

/**
 * File upload temporary path
 */
 @Bean
 MultipartConfigElement multipartConfigElement() {
    MultipartConfigFactory factory = new MultipartConfigFactory();
    factory.setLocation("/app/pttms/tmp");
    return factory.createMultipartConfig();
}
The final solution (of course not necessarily the problem you encountered, just how I solved it)

In the #spring boot project, application.properties adds the following configuration to specify the base directory of tomcat, otherwise the tomcat container embedded in spring boot will create a temporary directory, but this directory has a regular clearing policy, which will affect the use of web container resources, so Specifies to create a directory in the project root directory
server.tomcat.basedir =. / deployer / tomcat




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325530779&siteId=291194637