场景:
上传文件功能报错,然后排查日志。
报错日志:
the temporary upload location [/tmp/tomcat.7957874575370093230.8088/work/tomcat/localhost/root] is not valid
在linux系统中,springboot应用服务再启动(java -jar 命令启动服务)的时候,会在操作系统的/tmp目录下生成一个tomcat*的文件目录,上传的文件先要转换成临时文件保存在这个文件夹下面。
由于临时/tmp目录下的文件,在长时间(10天)没有使用的情况下,就会被系统机制自动删除掉。所以如果系统长时间没有使用到临时文件夹,就可能导致上面这个问题。
1.创建临时文件夹:
mkdir -p /tmp/tomcat.7957874575370093230.8088/work/Tomcat/localhost/ROOT
后面可能还会出现这种情况
2.application.properties重新配置一个文件目录,然后重启项目
# 存放Tomcat的日志、Dump等文件的临时文件夹,默认为系统的tmp文件夹 server.tomcat.basedir=/data/apps/temp
3.配置类配置临时文件存储目录
@Bean
MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
factory.setLocation(tmepPath);
return factory.createMultipartConfig();
}项目在线运行了一段时间后,上传文件时抛出如下异常:
The temporary upload location [/tmp/tomcat.*.80/work/Tomcat/localhost/ROOT] is not valid
经过查找,采用了如下的解决方式【修改临时文件的位置】
在application.yml 文件中添加
location: tempDir: /opt/location/tempDir #此处为*unix的系统相关位置
@Configuration
public class MultipartConfig {
@Value("${location.tempDir:/opt/tempDir}")
private String tempDir;
@Bean
MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
File tmpDirFile = new File(tempDir);
// 判断文件夹是否存在
if (!tmpDirFile.exists()) {
tmpDirFile.mkdirs();
}
factory.setLocation(tempDir);
return factory.createMultipartConfig();
}
}以上就是springboot怎么配置临时文件存储目录的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号