• 异步处理MultipartFile -- No such file or directory


    背景

    项目中开发导入功能,因为数据量比较大,所以要求后端异步操作。发现改为异步之后,相同代码的情况下会报(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类型参数上。debug跟踪MultipartFile类型参数发现,在主线程结束响应之后,MultipartFile类型参数就消失了。

     

    分析 

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

    解决

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

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

  • 相关阅读:
    python的多线程问题
    python 调用函数时使用星号 *, **
    python正则中的贪婪与非贪婪
    python中文处理
    Python 模块之 ConfigParser: 用 Python 解析配置文件
    substr使用注意
    [转]互联网后台服务的协议设计
    Java设计模式从精通到入门四 工厂方法模式
    logback中logger详解
    logback实践笔记
  • 原文地址:https://www.cnblogs.com/qingshan-tang/p/12377998.html
Copyright © 2020-2023  润新知