In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article "SpringBoot file upload temporary target is deleted exception how to deal with" most people do not understand, so the editor summarized the following content, detailed, clear steps, with a certain reference value, I hope you can get something after reading this article, let's take a look at this "SpringBoot file upload temporary target is deleted exception how to deal with" article.
1. Business background
We used SpringCloud for project development, and one of the main services (involving image upload) is the Spring Boot microservice in the test environment. Because this project has been online, no related cloth changes and packaging have been made for this project for a long time.
Recently, Party A needs to add some functions to the business of this project. But the test needs to upload the course cover when uploading the course, and it is found that the picture upload interface of the course cover has reported an error of 500.
I can't find the error message in the backend log directory. Only the front end that is separated from the front 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 had been deleted. Finally, I found an article to make it clear that there are the following points:
(1) after the SpringBoot project is started, the system automatically creates the following three directories under the / tmp directory by default
Hsperfdata_root
Tomcat.*.8080, (ends with the end of the project)
Tomcat-docbase.*.8080
(2), Multipart (form-data), the default is to create temporary files in the second directory.
(3) CentOS7 cleans the temporary file directory regularly.
The cleanup 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 overridev / tmp 1777 root root 10d # Clean up directories and files from 10 days ago under / tmp v / var/tmp 1777 root root 30d # clean up directories and files from 30 days ago under var/tmp
According to the above, we can see the temporary directory where we upload files, which will be cleaned up regularly every 10 days in CentOS7.
As a result, the article began to say that the temporary directory of uploaded files did not exist, so the upload problem was reported incorrectly by 500. My colleague said to start a micro-service and specifically said to start the gateway service.
Then again, starting any SpringBoot microservice can upload (because starting any native microservice will generate a corresponding temporary directory). Finally, a micro service is started, and it is true that it can be uploaded.
I don't want to dabble in solving the problem as long as it is solved. Finally, according to the search articles and analysis, I think there are three solutions.
2. Three solutions 2.1.Modification of CentOS cleaning temporary directory rules directly
Direct violence does not specify all temporary directories, and fine management does not clear the tomcat directory for uploaded files.
The cleanup rules of the / tmp directory mainly depend on the setting of the / usr/lib/tmpfiles.d/tmp.conf file:
We can configure this file. For example, if you don't want the system to automatically clean up directories under / tmp that start with tomcat, you can add the following to the configuration file:
X / tmp/tomcat.*2.2, specify your own upload file directory through the SpringBoot startup configuration annotation (@ Configuration)
Change the storage path of temporary files and specify a custom non-CentOS7 system default temporary directory, which avoids the situation that the system clears the temporary directory at a fixed time. The implementation code is as follows
@ Configurationpublic class MultipartConfig {/ * temporary path for file upload * / @ 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 scheme, but the Profile information is set in the configuration of SpringBoot
Configure in the propertites/yaml file: spring.http.multipart.location= your cache file path
Spring.mvc.static-path-pattern=/upload/**spring.http.multipart.max-file-size=10MB# specifies temporary directory of uploaded files, spring.http.multipart.location=/opt/data/upload3, and results presentation.
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 find out what caused it. I am also very grateful to the Internet, which allows many great gods to share the problems and solutions they have found. At the same time, it also explains the principle, which is helpful to solve the problem from the root.
Problems encountered in uploading SpringBoot files and their solutions: code error report
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 error will be reported. You need to specify a temporary file directory in the application configuration file.
Server.tomcat.basedir= file path
If you configured the spring.http.multipart.location= file path, you need to add that path.
Create a class that contains the following methods:
@ Beanpublic MultipartConfigElement multipartConfigElement () {MultipartConfigFactory factory = new MultipartConfigFactory (); factory.setLocation (". / tmp"); return factory.createMultipartConfig ();}
Restart will take effect.
Two: code error report
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,springboot uses MultipartFile to receive files in springmvc.
Change it and you can succeed.
The above is about the article "how to deal with the deletion exception of the temporary target of SpringBoot file upload". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.