Home  >  Article  >  Java  >  How to handle the exception when the temporary target of SpringBoot file upload is deleted

How to handle the exception when the temporary target of SpringBoot file upload is deleted

王林
王林forward
2023-05-15 19:16:141564browse

1. Business background

We used Spring Cloud for project development, and one of the main services (involving image uploading), the SpringBoot microservice, was in the test environment. Because this project has already been launched, no relevant changes or packaging have been made for this project for a long time.

Due to the recent need for Party A to add some new functions to this project. However, when uploading the course during the test, the course cover needs to be uploaded, and I found that the image upload interface for uploading the course cover reported an error of 500.

I cannot find the error message in the backend log directory. Only the front-end with separate front-end and back-end returns the following error message when calling the interface

Could not parse multipart servlet request;
nested exception is java.io.IOException:
The temporary upload location [/tmp/tomcat/ocalhost/ROOT] is not valid

Finally, I searched according to the error prompt and found that the temporary directory of tomcat was deleted. Finally, I found an article that explained the following points more clearly:

(1). After the SpringBoot project is started, the system will automatically create the following three directories in the /tmp directory by default

  • hsperfdata_root,

  • tomcat.************.8080, (the end is the end of the project)

  • tomcat-docbase.************.8080

##(2), Multipart (form- data), the default is to create a temporary file in the second directory

(3). CentOS7 regularly cleans the temporary file directory

The cleaning rules of the /tmp directory mainly depend on the settings of the /usr/lib/tmpfiles.d/tmp.conf file. The default configuration content is:

# Clear tmp directories separately, to make them easier to override
v /tmp 1777 root root 10d           #   清理/tmp下10天前的目录和文件
v /var/tmp 1777 root root 30d       #   清理/var/tmp下30天前的目录和文件

Based on the above conditions, it can be seen that The temporary directory where we upload files will be cleaned regularly every 10 days in CentOS7.

So there was a problem that the temporary directory where the uploaded files appeared at the beginning of the article did not exist, so the upload problem reported an error 500. My colleague said something about starting a certain microservice and specifically said that it is enough to start the gateway service.

Having said that, starting any SpringBoot microservice can achieve uploading (because starting any local microservice will generate a corresponding temporary directory). Finally, a microservice was started, and the results could indeed be uploaded.

I don’t want to just try to solve the problem as soon as the problem is solved. Finally, based on the searched articles and analysis, I personally think there are the following three solutions.

2. Three solutions

2.1. Directly modify the CentOS clean-up temporary directory rules

Directly specify all temporary directories by force, and refine management for uploaded files The tomcat directory is not cleared.

The cleaning rules of the /tmp directory mainly depend on the settings of the /usr/lib/tmpfiles.d/tmp.conf file:

We can configure this file, for example, you do not want the system to automatically To clean up the directory starting with tomcat under /tmp, then add the following content to the configuration file:

x /tmp/tomcat.*

2.2. Specify your own upload file directory through SpringBoot startup configuration annotation (@Configuration)

Change the storage path of temporary files and specify a custom non-CentOS7 system default temporary directory, so as to avoid the system clearing the temporary directory regularly. The implementation code is as follows

@Configuration
public class MultipartConfig { 
    /**
     * 文件上传临时路径
     */
    @Bean
    MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        String location = System.getProperty("user.dir") + "/data/upload/tmp";
        File tmpFile = new File(location);
        if (!tmpFile.exists()) {
            tmpFile.mkdirs();
        }
        factory.setLocation(location);
        return factory.createMultipartConfig();
    }
}

2.3. The principle is similar to the second solution, but the Profile information is set in the SpringBoot configuration

Configure in the properties/yaml file: spring.http.multipart. location= Your cache file path

spring.mvc.static-path-pattern=/upload/**
spring.http.multipart.max-file-size=10MB
#指定上传文件临时目录
spring.http.multipart.location=/opt/data/upload

3. Results display

How to handle the exception when the temporary target of SpringBoot file upload is deleted

How to handle the exception when the temporary target of SpringBoot file upload is deleted##4. Summary

When we encounter a problem, we may only see that the problem has been solved on the surface. But as a technology, we should delve into what causes it. I am also very grateful to the Internet, which allows many great experts to share the problems and solutions they have discovered. At the same time, the principles are also explained, which will help solve the problem from the root cause.


Problems and solutions encountered in SpringBoot file upload

1: Code error

Could not parse multipart servlet request; nested exception is java.io .IOException: The temporary upload location [D:xyqbtestworkTomcatlocalhostxyqb fsmarvels-core-v2] is not valid

Solution:

There is no temporary directory for uploading files in springboot, so the above report will be reported Error, you need to specify the temporary file directory in the application configuration file

server.tomcat.basedir=file path

If spring.http.multipart.location=file path is configured, you need to add this path.

Create a class that contains the following methods:

@Bean
public MultipartConfigElement multipartConfigElement() {
    MultipartConfigFactory factory = new MultipartConfigFactory();
    factory.setLocation("./tmp");
    return factory.createMultipartConfig();
}

It will take effect after restarting.

2: Code error

Failed to convert value of type "org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile" to required type "org.springframework.web.multipart.commons .CommonsMultipartFile"; nested exception is java.lang.IllegalStateException: Cannot convert value of type "org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile" to required type "org.springframework.web.multipart.commons.CommonsMultipartFile": no matching editors or conversion strategy found

Solution:

CommonsMultipartFile is used to receive files in springmvc, and MultipartFile is used in springboot.

Replace it and it will be successful.

The above is the detailed content of How to handle the exception when the temporary target of SpringBoot file upload is deleted. 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