java如何取消文件上传操作?

mylovetiantian 2013-01-31 08:37:19
如题所述,如果文件在执行上传操作过程中,因为意外或者因为需要取消其操作,那么此时就会抛出异常,而不能执行取消操作,而此时如何才能进行取消操作,而不抛出异常呢?
我用的是FileOutputStream和FileInputStream也就是I/O流进行文件的上传操作?
...全文
413 5 打赏 收藏 举报
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
mylovetiantian 2013-02-01
怎么没人关注了?
  • 打赏
  • 举报
回复
鸡肋啊 2013-01-31
引用 楼主 mylovetiantian 的回复:
如题所述,如果文件在执行上传操作过程中,因为意外或者因为需要取消其操作,那么此时就会抛出异常,而不能执行取消操作,而此时如何才能进行取消操作,而不抛出异常呢? 我用的是FileOutputStream和FileInputStream也就是I/O流进行文件的上传操作? web java
你的取消操作是写在哪的呢?如果是写在抛异常那的话应该会执行的
  • 打赏
  • 举报
回复
mylovetiantian 2013-01-31
2013-01-31 16:09:21,744 - org.apache.struts2.dispatcher.multipart.MultiPartRequest -52573 [http-8080-2] WARN MultiPartRequest:49 - Unable to parse request org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Stream ended unexpectedly at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:359) at org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest.parse(JakartaMultiPartRequest.java:93) at org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper.<init>(MultiPartRequestWrapper.java:75) at org.apache.struts2.dispatcher.Dispatcher.wrapRequest(Dispatcher.java:708) at org.apache.struts2.dispatcher.FilterDispatcher.prepareDispatcherAndWrapRequest(FilterDispatcher.java:327) at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:367) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Thread.java:619) Caused by: org.apache.commons.fileupload.MultipartStream$MalformedStreamException: Stream ended unexpectedly at org.apache.commons.fileupload.MultipartStream$ItemInputStream.makeAvailable(MultipartStream.java:964) at org.apache.commons.fileupload.MultipartStream$ItemInputStream.read(MultipartStream.java:887) at java.io.InputStream.read(InputStream.java:85) at org.apache.commons.fileupload.util.Streams.copy(Streams.java:94) at org.apache.commons.fileupload.util.Streams.copy(Streams.java:64) at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:354) ... 17 more 2013-01-31 16:09:21,775 - org.apache.struts2.interceptor.FileUploadInterceptor -52604 [http-8080-2] WARN FileUploadInterceptor:45 - Processing of multipart/form-data request failed. Stream ended unexpectedly ************************************************************************************************ ************************************************************************************************ public String uploadVideo(){ FileOutputStream fos = null; FileInputStream fis = null; ThreadPoolExecutor threadPool = new ThreadPoolExecutor(3, 100, 10, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(3), new ThreadPoolExecutor.DiscardOldestPolicy()); Thread thread=new Thread(); threadPool.execute(thread); thread.start(); synchronized(thread){ try{ String realPath="D:/company/upload/video"; String tempName=sdf.format(new Date())+"-"+videoName; if(video==null||video.equals("")){ System.out.println("file"); return ERROR; } if(videoName==null||videoName.equals("")){ System.out.println("videoname"+videoName); return ERROR; } if(videoEx==null||videoEx.equals("")){ System.out.println("videoex"+videoEx); return ERROR; } fos = new FileOutputStream(realPath + "\\" + tempName); fis = new FileInputStream(getVideo()); byte[] buffer = new byte[1024]; int len = 0; if(buffer.length>0){ while ((len = fis.read(buffer)) > 0) { fos.write(buffer, 0, len); } } return SUCCESS; }catch(Exception e){ log.error("文件上传失败:",e); return ERROR; }finally{ close(fos, fis); thread.interrupt(); threadPool.shutdown(); } } } 如上是我的代码和异常,不知如何解决此类问题?
  • 打赏
  • 举报
回复
BoJerry 2013-01-31
使用线程应该可以吧。
  • 打赏
  • 举报
回复
大板牙花生 2013-01-31
多线程能不能解决?
  • 打赏
  • 举报
回复
相关推荐
发帖
Web 开发
加入

8.0w+

社区成员

Java Web 开发
社区管理员
  • Web 开发社区
申请成为版主
帖子事件
创建了帖子
2013-01-31 08:37
社区公告
暂无公告