SoFunction
Updated on 2025-03-08

springboot /tmp Specific implementation of temporary directories

1. Generation mechanism

In the Linux system, when the springboot application service is started again (java -jar command starts the service), a tomcat* file directory will be generated in the operating system /tmp directory. The uploaded files must first be converted into temporary files and saved in this folder.

Because after the stream is consumed once, data cannot be obtained from the stream, so the cache is convenient for subsequent reuse;

2. Produce an exception

After going online, the temporary tomcat folder may be deleted by Linux, and an error will be reported. Now you can record it quickly, and it has been needed from time to time.

cat /usr/lib//
#  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 (5) for details

# Clear tmp directories separately, to make them easier to override
v /tmp 1777 root root 10d
v /var/tmp 1777 root root 30d

# 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

3. Solution

3.1 Restart method

Since the directory has been deleted, restart the service and let the system regenerate the directory, and temporarily resolve it (but the directory may be deleted in the future)

3.1 Modify the cleanup strategy of /tmp directory from the Linux level

Configure not to delete tomcat in the tmp directory

vim /usr/lib//

# Add the following line
x /tmp/tomcat.*

# Restart the servicesystemctl restart systemd-tmpfiles-clean

3.2 Add JVM configuration

#Customize the temporary directory as /app/xxx/tmp-=/app/xxx/tmp(Custom path)

3.3 Add JVM configuration

-=/data/upload_tmp

3.4 Add spring boot configuration

spring:
  http:
    multipart:
      location: /data/upload_tmp

3.5 Configuring using configuration class

Register the MultipartConfigElement object in the Spring container and specify the path through the MultipartConfigFactory. If the path does not exist, create it

@Bean
public MultipartConfigElement multipartConfigElement() {
	MultipartConfigFactory factory = new MultipartConfigFactory();
	String location = ("")+"/data/tmp";
	File tmpFile = new File(location);
	if (!()){
		();
	}
	(location);
	return ();
}

This is the article about the specific implementation of springboot /tmp temporary directory. For more related springboot /tmp temporary directory content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!