springboot临时文件存储目录配置场景:
上传文件功能报错,然后排查日志。
报错日志:
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(); }
springboot修改临时文件的存储位置报错项目在线运行了一段时间后,上传文件时抛出如下异常:
the temporary upload location [/tmp/tomcat.*.80/work/tomcat/localhost/root] is not valid
经过查找,采用了如下的解决方式【修改临时文件的位置】
在application.yml 文件中添加
location: tempdir: /opt/location/tempdir #此处为*unix的系统相关位置
项目中添加配置类@configurationpublic 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怎么配置临时文件存储目录的详细内容。