1. 程式人生 > 程式設計 >解決springboot 多執行緒使用MultipartFile讀取excel檔案內容報錯問題

解決springboot 多執行緒使用MultipartFile讀取excel檔案內容報錯問題

springboot專案開啟多執行緒

解決springboot 多執行緒使用MultipartFile讀取excel檔案內容報錯問題

解決springboot 多執行緒使用MultipartFile讀取excel檔案內容報錯問題

啟動類加註解開啟 @EnableAsync,實現類方法加註解 @Async

前端頁面

解決springboot 多執行緒使用MultipartFile讀取excel檔案內容報錯問題

報錯資訊

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檔案內容報錯問題就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。