gpt4 book ai didi

解决springboot 多线程使用MultipartFile读取excel文件内容报错问题

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 24 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章解决springboot 多线程使用MultipartFile读取excel文件内容报错问题由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

springboot项目开启多线程 。

解决springboot 多线程使用MultipartFile读取excel文件内容报错问题

解决springboot 多线程使用MultipartFile读取excel文件内容报错问题

启动类加注解开启 @EnableAsync,实现类方法加注解 @Async 。

前端页面 。

解决springboot 多线程使用MultipartFile读取excel文件内容报错问题

报错信息 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
java.io.FileNotFoundException: C:\Users\dongao\AppData\Local\Temp\tomcat. 1255209411477782290.8051 \work\Tomcat\localhost\ROOT\upload_7d7b99e5_38da_4a03_93e0_bff20cb48022_00000000.tmp (系统找不到指定的文件。)
  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.getInputStream(StandardMultipartHttpServletRequest.java: 250 )
  at com.dongao.project.utils.UploadUtil.readExcel(UploadUtil.java: 156 )
  at com.dongao.project.utils.UploadUtil.readExcelToMap(UploadUtil.java: 98 )
  at com.dongao.project.importbatch.service.ImportBatchServiceImpl.importData(ImportBatchServiceImpl.java: 161 )
  at com.dongao.project.importbatch.service.ImportBatchServiceImpl$$FastClassBySpringCGLIB$$440ed2f6.invoke(<generated>)
  at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java: 218 )
  at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java: 749 )
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java: 163 )
  at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java: 88 )
  at com.dongao.support.cat.CatUtils.proceed(CatUtils.java: 18 )
  at com.dongao.support.cat.CatSpringAop.getObject(CatSpringAop.java: 26 )
  at com.dongao.support.cat.CatSpringAop.aroundServiceMethod(CatSpringAop.java: 22 )
  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.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java: 644 )
  at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java: 633 )
  at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java: 70 )
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java: 186 )
  at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java: 294 )
  at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java: 98 )
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java: 186 )
  at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java: 93 )
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java: 186 )
  at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java: 688 )
  at com.dongao.project.importbatch.service.ImportBatchServiceImpl$$EnhancerBySpringCGLIB$$a16c4d4e.importData(<generated>)
  at com.dongao.project.importbatch.service.ImportBatchServiceImpl$$FastClassBySpringCGLIB$$440ed2f6.invoke(<generated>)
  at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java: 218 )
  at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java: 749 )
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java: 163 )
  at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java: 294 )
  at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java: 98 )
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java: 186 )
  at org.springframework.aop.interceptor.AsyncExecutionInterceptor.lambda$invoke$ 0 (AsyncExecutionInterceptor.java: 115 )
  at java.util.concurrent.FutureTask.run(FutureTask.java: 266 )
  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 )
: 36 : 46.137 25472 [threadPoolTaskExecutor- 1 ] INFO com.ruoyi.framework.datasource.DynamicDataSourceContextHolder - [setDataSourceType, 26 ] - 切换到SLAVE数据源
: 36 : 46.163 25472 [threadPoolTaskExecutor- 1 ] ERROR org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler - [handleUncaughtException, 39 ] - Unexpected exception occurred invoking async method: public void com.dongao.project.importbatch.service.ImportBatchServiceImpl.importData(org.springframework.web.multipart.MultipartFile,java.lang.Long) throws java.lang.Exception
java.lang.IndexOutOfBoundsException: Index: 0 , Size: 0
  at java.util.ArrayList.rangeCheck(ArrayList.java: 653 )
  at java.util.ArrayList.get(ArrayList.java: 429 )
  at com.dongao.project.importbatch.service.ImportBatchServiceImpl.importData(ImportBatchServiceImpl.java: 178 )
  at com.dongao.project.importbatch.service.ImportBatchServiceImpl$$FastClassBySpringCGLIB$$440ed2f6.invoke(<generated>)
  at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java: 218 )
  at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java: 749 )
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java: 163 )
  at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java: 88 )
  at com.dongao.support.cat.CatUtils.proceed(CatUtils.java: 18 )
  at com.dongao.support.cat.CatSpringAop.getObject(CatSpringAop.java: 26 )
  at com.dongao.support.cat.CatSpringAop.aroundServiceMethod(CatSpringAop.java: 22 )
  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.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java: 644 )
  at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java: 633 )
  at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java: 70 )
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java: 186 )
  at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java: 294 )
  at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java: 98 )
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java: 186 )
  at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java: 93 )
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java: 186 )
  at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java: 688 )
  at com.dongao.project.importbatch.service.ImportBatchServiceImpl$$EnhancerBySpringCGLIB$$a16c4d4e.importData(<generated>)
  at com.dongao.project.importbatch.service.ImportBatchServiceImpl$$FastClassBySpringCGLIB$$440ed2f6.invoke(<generated>)
  at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java: 218 )
  at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java: 749 )
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java: 163 )
  at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java: 294 )
  at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java: 98 )
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java: 186 )
  at org.springframework.aop.interceptor.AsyncExecutionInterceptor.lambda$invoke$ 0 (AsyncExecutionInterceptor.java: 115 )
  at java.util.concurrent.FutureTask.run(FutureTask.java: 266 )
  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 )

问题分析 。

前端页面传过来的文件会存在临时文件夹中,如下 。

C:\Users\dongao\AppData\Local\Temp\tomcat.1255209411477782290.8051\work\Tomcat\localhost\ROOT\ 。

这个时候如果单线程操作,即正常程序流程解析是不会有问题的; 。

如果走异步多线程解析文件,主线程已经结束了,临时文件会被清空,这时候再来读取文件就会报错 。

java.io.FileNotFoundException: C:\Users\dongao\AppData\Local\Temp\tomcat.1255209411477782290.8051\work\Tomcat\localhost\ROOT\upload_7d7b99e5_38da_4a03_93e0_bff20cb48022_00000000.tmp (系统找不到指定的文件。) 。

问题处理 。

由于主线程结束,临时文件被清空,导致多线程业务类无法获取到临时文件而报错(系统找不到指定的文件。),此时可以在主线程中转换获取文件流信息 。

InputStream is = file.getInputStream(),

文件流信息存储在内存中,多线程时也可以获取到文件内容,解决问题.

以上这篇解决springboot 多线程使用MultipartFile读取excel文件内容报错问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我.

原文链接:https://blog.csdn.net/csdn565973850/article/details/106575090 。

最后此篇关于解决springboot 多线程使用MultipartFile读取excel文件内容报错问题的文章就讲到这里了,如果你想了解更多关于解决springboot 多线程使用MultipartFile读取excel文件内容报错问题的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com