异步处理MultipartFile No such file or directory的分析

Posted qingshan-tang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了异步处理MultipartFile No such file or directory的分析相关的知识,希望对你有一定的参考价值。

背景

项目中开发导入功能,因为数据量比较大,所以要求后端异步操作(个人觉得前端ajax处理最好,有空再试一下)。但是操作中发现改为异步之后,相同代码的情况下会报(No such file or directory)异常

2020-02-28 16:22:51.322 [pool-2-thread-1] ERROR c.c.l.m.business.service.impl.CommonServiceImpl - /private/var/folders/gj/_gbl0m854pn1lw6dn2k4w1gr0000gn/T/tomcat.5469750062936240832.8081/work/Tomcat/localhost/lm-admin/upload_bc9f994f_f4dc_4a2e_9933_dbcec37b3a7e_00000001.tmp (No such file or directory)
com.alibaba.fastjson.JSONException: write javaBean error, fastjson version 1.2.45, class org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile
    at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:465)
    at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:120)
    at com.alibaba.fastjson.serializer.JSONSerializer.write(JSONSerializer.java:280)
    at com.alibaba.fastjson.JSON.toJSONString(JSON.java:673)
    at com.alibaba.fastjson.JSON.toJSONString(JSON.java:611)
    at com.alibaba.fastjson.JSON.toJSONString(JSON.java:576)
    at com.citi.lm.modules.business.service.impl.CommonServiceImpl.readAndHandleFile(CommonServiceImpl.java:93)
    at com.citi.lm.modules.business.service.impl.CommonServiceImpl$$FastClassBySpringCGLIB$$82c8f695.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:684)
    at com.citi.lm.modules.business.service.impl.CommonServiceImpl$$EnhancerBySpringCGLIB$$5f90c28f.readAndHandleFile(<generated>)
    at com.citi.lm.modules.business.controller.CommonController$1.call(CommonController.java:84)
    at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266)
    at java.util.concurrent.FutureTask.run(FutureTask.java)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException
    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 com.alibaba.fastjson.util.FieldInfo.get(FieldInfo.java:484)
    at com.alibaba.fastjson.serializer.FieldSerializer.getPropertyValueDirect(FieldSerializer.java:135)
    at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:249)
    ... 16 more
Caused by: java.io.FileNotFoundException: /private/var/folders/gj/_gbl0m854pn1lw6dn2k4w1gr0000gn/T/tomcat.5469750062936240832.8081/work/Tomcat/localhost/lm-admin/upload_bc9f994f_f4dc_4a2e_9933_dbcec37b3a7e_00000001.tmp (No such file or directory)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at org.apache.tomcat.util.http.fileupload.disk.DiskFileItem.getInputStream(DiskFileItem.java:194)
    at org.apache.catalina.core.ApplicationPart.getInputStream(ApplicationPart.java:100)
    at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile.getBytes(StandardMultipartHttpServletRequest.java:245)
    ... 23 more

 

找问题

不啰嗦过程了,最后将焦点集中在接口接收的MultipartFile类型参数上,然后逐句打印跟踪MultipartFile类型对象发现,在主线程结束响应之后,MultipartFile类型对象就消失了。

技术图片

 

 

 技术图片

 

 

分析 

说明在响应结束之后,file的内存区域被清除回收了,和servlet规范一致。个人理解,file对象就像servlet的request和response对象一样,请求一结束就被清理了。

 

解决

目前没有解决办法,因为后来项目需求把接收的文件保存下来,那么对我来说就不是问题了。但是我想可以在创建处理线程之前把file中的二进制流转成其他形式保留下来,然后对新保留的数据进行处理应该行得通,有机会再试吧。

 

参考:https://blog.csdn.net/yinni11/article/details/83793930

以上是关于异步处理MultipartFile No such file or directory的分析的主要内容,如果未能解决你的问题,请参考以下文章

报错记录SpringBoot中MultipartFile上传报/tmp/tomcat.***.tmp (No such file or directory)

ceph-create-keys: No such file or directory 处理

#!/bin/bash - no such file or directory

adb pull 报错处理:adb: error: cannot create file/directory 'E:': No such file or directory

sparksql错误报No such file or director

Failed to connect socket to '/var/run/libvirt/libvirt-sock': No such file or directory错误处理(示