• SpringBoot 上传文件到linux服务器 异常java.io.FileNotFoundException: /tmp/tomcat.50898……解决方案


    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{
    
        <span class="token comment">//文件存在</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>files <span class="token operator">!=</span> null<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token comment">//遍历图片</span>
            <span class="token keyword">for</span> <span class="token punctuation">(</span>MultipartFile file <span class="token operator">:</span> files<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token comment">//定义上传文件路径、文件名、文件格式</span>
                java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>Date Datenow<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//获取当前日期</span>
                java<span class="token punctuation">.</span>text<span class="token punctuation">.</span>SimpleDateFormat formatter <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">java<span class="token punctuation">.</span>text<span class="token punctuation">.</span>SimpleDateFormat</span><span class="token punctuation">(</span><span class="token string">"yyyyMMdd"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                String nowdate <span class="token operator">=</span> formatter<span class="token punctuation">.</span><span class="token function">format</span><span class="token punctuation">(</span>Datenow<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//将日期格式化</span>
                <span class="token comment">//服务器真实路径</span>
                String realDirectory <span class="token operator">=</span> WebConst<span class="token punctuation">.</span>FILE_REAL_PATH <span class="token operator">+</span> nowdate<span class="token punctuation">;</span>
                <span class="token comment">//原文件名</span>
                String fileName <span class="token operator">=</span> file<span class="token punctuation">.</span><span class="token function">getOriginalFilename</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token comment">//文件类型</span>
                String fileType<span class="token operator">=</span><span class="token string">""</span><span class="token punctuation">;</span>
                <span class="token keyword">if</span> <span class="token punctuation">(</span>fileName<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span>fileName<span class="token punctuation">.</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span><span class="token string">"jpeg"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    fileType<span class="token operator">=</span><span class="token string">".jpeg"</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span><span class="token keyword">else</span><span class="token punctuation">{</span>
                    fileType <span class="token operator">=</span> fileName<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span>fileName<span class="token punctuation">.</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>
                <span class="token comment">//对文件进行重新命名</span>
                String filename <span class="token operator">=</span> System<span class="token punctuation">.</span><span class="token function">currentTimeMillis</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">"."</span> <span class="token operator">+</span> fileType<span class="token punctuation">;</span>
    
                <span class="token comment">//文件上传</span>
                <span class="token keyword">if</span><span class="token punctuation">(</span>file<span class="token punctuation">.</span><span class="token function">getSize</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">&gt;</span><span class="token number">1024</span><span class="token operator">*</span><span class="token number">1024</span><span class="token operator">*</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
                    <span class="token comment">//大于10M 压缩上传</span>
                    <span class="token comment">//创建临时文件 将原文件file放入临时文件temFile</span>
                    File temFile <span class="token operator">=</span>  File<span class="token punctuation">.</span><span class="token function">createTempFile</span><span class="token punctuation">(</span><span class="token string">"temp"</span><span class="token punctuation">,</span> <span class="token string">"."</span> <span class="token operator">+</span> fileType<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    file<span class="token punctuation">.</span><span class="token function">transferTo</span><span class="token punctuation">(</span>temFile<span class="token punctuation">)</span><span class="token punctuation">;</span>
    
                    String filedir <span class="token operator">=</span> realDirectory<span class="token operator">+</span><span class="token string">"/"</span> <span class="token operator">+</span> filename<span class="token punctuation">;</span> <span class="token comment">// 以系统时间作为上传文件名称,设置上传文件的完整路径</span>
                    ImgUtill<span class="token punctuation">.</span><span class="token function">compressImage</span><span class="token punctuation">(</span>temFile<span class="token punctuation">.</span><span class="token function">getAbsolutePath</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> filedir<span class="token punctuation">)</span><span class="token punctuation">;</span>
    
                    temFile<span class="token punctuation">.</span><span class="token function">deleteOnExit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 令临时文件在JVM关闭的时候自动删除</span>
                    temFile<span class="token punctuation">.</span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 立刻删除临时文件</span>
                <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
                    <span class="token comment">//普通上传</span>
                    File fi <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span>realDirectory<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token keyword">try</span> <span class="token punctuation">{</span>
                        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>fi<span class="token punctuation">.</span><span class="token function">isDirectory</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 如果文件夹不存在就新建</span>
                            fi<span class="token punctuation">.</span><span class="token function">mkdirs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                        <span class="token punctuation">}</span>
                        File fie <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span>realDirectory<span class="token punctuation">,</span> filename<span class="token punctuation">)</span><span class="token punctuation">;</span>
                        file<span class="token punctuation">.</span><span class="token function">transferTo</span><span class="token punctuation">(</span>fie<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                        e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                        <span class="token keyword">throw</span> BusinessException<span class="token punctuation">.</span><span class="token function">withErrorCode</span><span class="token punctuation">(</span>ErrorConstant<span class="token punctuation">.</span>Att<span class="token punctuation">.</span>UPLOAD_FILE_FAIL<span class="token punctuation">)</span>
                                <span class="token punctuation">.</span><span class="token function">withErrorMessageArguments</span><span class="token punctuation">(</span>e<span class="token punctuation">.</span><span class="token function">getMessage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
            <span class="token keyword">return</span> APIResponse<span class="token punctuation">.</span><span class="token function">success</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> APIResponse<span class="token punctuation">.</span><span class="token function">fail</span><span class="token punctuation">(</span><span class="token string">"文件不存在"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    
    <span class="token punctuation">}</span>
    

    这个时候普通上传依然报上面的错误

    解决方法

    上传路径使用绝对路径

    修改常量类中定义的文件路径 在 www 前加 /

        //服务器真实地址(linux根目录需要以/开头)
        public static String FILE_REAL_PATH = "/www/server/apache-tomcat-8.5.32/webapps/jxmstory/uploadfile/";
    

    未完

          </div>
    

    原文地址:https://blog.csdn.net/Ani521smile/article/details/83660328

  • 相关阅读:
    Mysql DQL语言执行顺序
    MySQL核心技术——DQL语言
    Java基础第二十九天总结——Java8新特性
    Java基础第二十八天总结——反射机制
    在对csv文件做批量获取时无法获取,程序不动
    tensorflow 对csv数据进行批量获取
    tensorflow 做多元线性回归时怎样对非数据型数据(分类型数据)进行处理(编码)
    flatten函数
    用 sklearn包中的 linear_model 实现多元线性回归
    tensorflow实现多元线性回归时预测出的参数为nan
  • 原文地址:https://www.cnblogs.com/jpfss/p/10985127.html
Copyright © 2020-2023  润新知