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天前的目录和文件
x /tmp/tomcat.*
@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(); } }
spring.mvc.static-path-pattern=/upload/** spring.http.multipart.max-file-size=10MB #指定上传文件临时目录 spring.http.multipart.location=/opt/data/upload
##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!