Springboot file upload error

Exception information:

org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request;nested exception is java.io.IOException: The temporary upload location [/tmp/tomcat.1978174608501890996.8083/work/Tomcat/localhost/pmap] is not valid org.springframework.web.multipart.support.StandardMultipartHttpServletRequest .parseRequest(StandardMultipartHttpServletRequest.java:112) org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.

Reason:

< p>In the Linux system, when the springboot application service is started (the java -jar command starts the service), a tomcat* file directory will be generated in the /tmp directory of the operating system, and the uploaded file must be converted into a temporary file for storage. Under this folder. The files in the temporary /tmp directory will be automatically deleted by the system mechanism if they are not used for a long time (10 days). So if the system is left unattended for a long time, it may cause the above problem.

Solution:

  1. Just restart the project, and the temporary folder will be re-created
  2. Configure in the configuration file Temporary directory of tomcat
    server.tomcat.basedir=/home/tmp

  3. < li>Write a configuration class, configure the directory through @Bean

    /**
    * Temporary path for file upload
    */
    @Bean
    MultipartConfigElement multipartConfigElement() {
    MultipartConfigFactory factory = new MultipartConfigFactory();
    Factory.setLocation("/home/tmp");
    Return factory.createMultipartConfig();
    }

server.tomcat.basedir=/home/tmp< /pre>

/**
* Temporary path for file upload
*/
@Bean
MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
Factory.setLocation("/home/tmp");
Return factory.createMultipartConfig();
}

Leave a Comment

Your email address will not be published.