异步上传文件报错,重复使用java流

如果上传的时候,是同步上传的,不会出现问题。
如果是异步上传的,如果你传入的异步方法是 MultipartFile file,就会包下面的错

java.io.FileNotFoundException: D:\usr\local\tomcat\work\Tomcat\localhost\upload_46ba607a_d50c_4618_9b66_234953a0ba1f_00000002.tmp (系统找不到指定的文件。)
	at java.io.FileInputStream.open0(Native Method)
	at java.io.FileInputStream.open(FileInputStream.java:195)
	at java.io.FileInputStream.(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.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:746)
	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.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:47)
	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.interceptor.AsyncExecutionInterceptor.lambda$invoke$0(AsyncExecutionInterceptor.java:115)
	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)

解决方法:
调用异步Async方法的时候,传入file.getInputStream(),就能解决问题。
如果想要多次使用同一个流:
比如:参数是 is = file.getInputStream()

import org.apache.poi.util.IOUtils; //可以使用别的工具类,也可以自己写

byte[] bytes = IOUtils.toByteArray(is);
ByteArrayInputStream bisUpload = new ByteArrayInputStream(bytes);
ByteArrayInputStream bisExcel = new ByteArrayInputStream(bytes);

service.upload(bisUpload ); //线程上传到服务器
ExcelUtil.readExcel2007(bisExcel); //然后再读文件

你可能感兴趣的:(java,bug)