SpringBoot 上传文件到linux服务器 异常java.io.FileNotFoundException: /tmp/tomcat.50898……解决方案
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot 上传文件到linux服务器 异常java.io.FileNotFoundException: /tmp/tomcat.50898……解决方案相关的知识,希望对你有一定的参考价值。
java.io.IOException: java.io.FileNotFoundException: /tmp/tomcat.5089835798184465073.8081/work/Tomcat/localhost/ROOT/www/server/apache-tomcat-8.5.32/webapps/jxmstory/uploadfile/201811/1541155647100.jpg (No such file or directory)
at org.apache.catalina.core.ApplicationPart.write(ApplicationPart.java:122)
at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile.transferTo(StandardMultipartHttpServletRequest.java:255)
at com.sh.controller.admin.AttAchController.upload(AttAchController.java:226)
at com.sh.controller.admin.AttAchController$$FastClassBySpringCGLIB$$15ecb206.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:747)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:52)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:52)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:689)
at com.sh.controller.admin.AttAchController$$EnhancerBySpringCGLIB$$4003049c.upload(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:877)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:783)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:877)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123)
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.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
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.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109)
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.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)
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:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
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)
Caused by: java.io.FileNotFoundException: /tmp/tomcat.5089835798184465073.8081/work/Tomcat/localhost/ROOT/www/server/apache-tomcat-8.5.32/webapps/jxmstory/uploadfile/201811/1541155647100.jpg (No such file or directory)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(FileOutputStream.java:270)
at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
at java.io.FileOutputStream.<init>(FileOutputStream.java:162)
at org.apache.tomcat.util.http.fileupload.disk.DiskFileItem.write(DiskFileItem.java:400)
at org.apache.catalina.core.ApplicationPart.write(ApplicationPart.java:120)
... 71 more
报错原因:
之前上传文件部分代码如下
压缩上传可以 普通上传在file.transferTo(fie);
会报错
因为上面创建临时文件的时候执行了file.transferTo(temFile);
方法 该方法执行一遍后文件流已经关闭
//文件上传
//创建临时文件 将原文件file放入临时文件temFile
File temFile = File.createTempFile("temp", "." + fileType);
file.transferTo(temFile);
if(temFile.length()>1024*1024*10)
//大于10M 压缩上传
String filedir = realDirectory+"/" + filename; // 以系统时间作为上传文件名称,设置上传文件的完整路径
ImgUtill.compressImage(temFile.getAbsolutePath(), filedir);
else
//普通上传
File fi = new File(realDirectory);
try
if (!fi.isDirectory()) // 如果文件夹不存在就新建
fi.mkdirs();
File fie = new File(realDirectory, filename);
file.transferTo(fie);
catch (IOException e)
e.printStackTrace();
throw BusinessException.withErrorCode(ErrorConstant.Att.UPLOAD_FILE_FAIL)
.withErrorMessageArguments(e.getMessage());
temFile.deleteOnExit(); // 令临时文件在JVM关闭的时候自动删除
temFile.delete(); // 立刻删除临时文件
修改后的上传文件部分代码
@PostMapping(value = "upload")
@ResponseBody
public APIResponse upload(@RequestParam("file") MultipartFile[] files,
HttpServletRequest request,
HttpServletResponse response) throws Exception
//文件存在
if (files != null)
//遍历图片
for (MultipartFile file : files)
//定义上传文件路径、文件名、文件格式
java.util.Date Datenow=new java.util.Date();//获取当前日期
java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyyMMdd");
String nowdate = formatter.format(Datenow).substring(0,6); //将日期格式化
//服务器真实路径
String realDirectory = WebConst.FILE_REAL_PATH + nowdate;
//原文件名
String fileName = file.getOriginalFilename();
//文件类型
String fileType="";
if (fileName.substring(fileName.length()-4).equals("jpeg"))
fileType=".jpeg";
else
fileType = fileName.substring(fileName.length()-3);
//对文件进行重新命名
String filename = System.currentTimeMillis() + "." + fileType;
//文件上传
if(file.getSize()>1024*1024*5)
//大于10M 压缩上传
//创建临时文件 将原文件file放入临时文件temFile
File temFile = File.createTempFile("temp", "." + fileType);
file.transferTo(temFile);
String filedir = realDirectory+"/" + filename; // 以系统时间作为上传文件名称,设置上传文件的完整路径
ImgUtill.compressImage(temFile.getAbsolutePath(), filedir);
temFile.deleteOnExit(); // 令临时文件在JVM关闭的时候自动删除
temFile.delete(); // 立刻删除临时文件
else
//普通上传
File fi = new File(realDirectory);
try
if (!fi.isDirectory()) // 如果文件夹不存在就新建
fi.mkdirs();
File fie = new File(realDirectory, filename);
file.transferTo(fie);
catch (IOException e)
e.printStackTrace();
throw BusinessException.withErrorCode(ErrorConstant.Att.UPLOAD_FILE_FAIL)
.withErrorMessageArguments(e.getMessage());
return APIResponse.success();
else
return APIResponse.fail("文件不存在");
这个时候普通上传依然报上面的错误
解决方法
上传路径使用绝对路径
修改常量类中定义的文件路径 在 www 前加 /
//服务器真实地址(linux根目录需要以/开头)
public static String FILE_REAL_PATH = "/www/server/apache-tomcat-8.5.32/webapps/jxmstory/uploadfile/";
未完
原文地址:https://blog.csdn.net/Ani521smile/article/details/83660328
以上是关于SpringBoot 上传文件到linux服务器 异常java.io.FileNotFoundException: /tmp/tomcat.50898……解决方案的主要内容,如果未能解决你的问题,请参考以下文章