我是靠谱客的博主 自由向日葵,最近开发中收集的这篇文章主要介绍springboot 上传文件报错:java.io.IOException: The temporary upload location [/tmp/tomcat.xxx] is not valid,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

springboot 上传文件报错:java.io.IOException: The temporary upload location [/tmp/tomcat.xxx] is not valid

前言:
之前上线半个月后的项目,突然发现图片上传的接口有问题,无法上传,提示java.io.IOException: The temporary upload location [/tmp/tomcat.1920962677180834063.9092/work/Tomcat/localhost/ROOT] is not valid ,经过百度,才知道这个是springboot项目文件上传的一个坑。

异常详细

2021-07-15 15:34:01.074 ERROR 48843 --- [nio-9092-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet]
: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is java.io.IOException: The temporary upload location [/tmp/tomcat.1920962677180834063.9092/work/Tomcat/localhost/ROOT] is not valid] with root cause
java.io.IOException: The temporary upload location [/tmp/tomcat.1920962677180834063.9092/work/Tomcat/localhost/ROOT] is not valid
at org.apache.catalina.connector.Request.parseParts(Request.java:2821)
at org.apache.catalina.connector.Request.parseParameters(Request.java:3185)
at org.apache.catalina.connector.Request.getParameter(Request.java:1116)
at org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:381)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:84)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.filterAndRecordMetrics(WebMvcMetricsFilter.java:117)
at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:106)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:200)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)

分析问题

spring boot的应用服务在启动的时候,会生成在操作系统的/tmp目录下生成一个tomcat.*的文件目录,用于"java.io.tmpdir"文件流操作。

程序对文件的操作时:会生成临时文件,暂存在临时文件中;lunix 系统的tmpwatch 命令会删除10天未使用的临时文件;长时间不操作,导致/tmp下面的tomcat临时文件目录被删除,且删除的文件不可恢复,上传文件时获取不到文件目录,所以就报了开头的错啦。

我进到/tmp的文件下,看有没有文件

解决方案一:

重启项目

解决方案二:

重启项目

1.在配置文件中配置tomcat的临时目录:

properties
server.tomcat.basedir=/home/temp
yml
server:
tomcat:
basedir: /home/temp
  1. 写个配置类,通过@Bean的方式配置目录:
/**
* 文件上传临时路径
*/
@Bean
MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
factory.setLocation("/home/temp");
return factory.createMultipartConfig();
}

建议使用用第二种,在项目启动时,生成该文件,一劳永逸,杜绝后患

最后

以上就是自由向日葵为你收集整理的springboot 上传文件报错:java.io.IOException: The temporary upload location [/tmp/tomcat.xxx] is not valid的全部内容,希望文章能够帮你解决springboot 上传文件报错:java.io.IOException: The temporary upload location [/tmp/tomcat.xxx] is not valid所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(41)

评论列表共有 0 条评论

立即
投稿
返回
顶部