Home >Java >javaTutorial >How to handle the exception when the temporary target of SpringBoot file upload is deleted
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 solutions2.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)
@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 configurationConfigure 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/upload3. Results display
##4. Summary
Problems and solutions encountered in SpringBoot file upload
Solution:
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.
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
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!