SpringBoot upload file: temporary file directory does not exist

Problem: After the
springboot project is deployed to the server, after running for a period of time, when processing some file upload interfaces, an exception is reported afterwards.


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

That is, the temporary file directory does not exist.

Reason: After
checking the information, it was found that linux automatically cleaned up the files under '/ tmp'.
After the springboot project starts, the system will automatically create several directories under the '/ tmp' directory (my project is the following folder)

  1. hsperfdata_root,
  2. tomcat. ************. 8080, (The end is the end of the project
  3. tomcat-docbase. *********. 8080.
    When processing requests in the form of Multipart (form-data), temporary files are created in the second directory by default.

The main reason may be that the default configuration of the system will automatically clean up the temporary file directory that has not been operated for a period of time, which makes it impossible to query the temporary file directory during upload.

CentOS6 and below (including) use watchtmp + cron to achieve the effect of regularly cleaning up temporary files. This has changed in CentOS7. Under CentOS7, the system uses systemd to manage variable and temporary files. There are 3 related system services :

systemd-tmpfiles-setup.service :Create Volatile Files and Directories
systemd-tmpfiles-setup-dev.service:Create static device nodes in /dev
systemd-tmpfiles-clean.service :Cleanup of Temporary Directories

There are also three places related to configuration files:
/etc/tmpfiles.d/ .conf
/run/tmpfiles.d/
.conf
/usr/lib/tmpfiles.d/*.conf
/ tmp directory cleaning rules mainly depend on / usr /lib/tmpfiles.d/tmp.conf file settings, the default configuration content is:


#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

# See tmpfiles.d(5) for details

# 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天前的目录和文件

# Exclude namespace mountpoints created with PrivateTmp=yes
x /tmp/systemd-private-%b-*
X /tmp/systemd-private-%b-*/tmp
x /var/tmp/systemd-private-%b-*
X /var/tmp/systemd-private-%b-*/tmp


There are four solutions :
1. Restart the project and automatically generate the temporary file directory again, but if the temporary file directory is not operated for a period of time, the problem may occur again.
2. Configure the location of the temporary file directory for Multipart (form-data) requests, not under / tmp.


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

3. Manually create the corresponding directory in the server.
4. Add system configuration so that the system will not automatically clear the temporary file directory in tomcat. * Format.
x /tmp/tomcat.* can be added to the above file.

Published 24 original articles · praised 0 · visits 109

Guess you like

Origin blog.csdn.net/jiangxiayouyu/article/details/105606760