commons-fileupload中断上传的异常如何处理?
我在使用swfupload和fileupload共同完成文件的上传功能
因为提供了可以中断上传的功能,但是在中断的同时会抛出异常
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.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)
at com.sini.utils.upload.UploadServlet.doPost(UploadServlet.java:43)
at
javax.servlet.http.HttpServlet.service(HttpServlet.java:710) at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
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:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
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:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)
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(Unknown Source)
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)
... 16 more
异常的意思是以不希望的情况结束了流(Stream)
Java code
// 创建工厂
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
// 设置文件上传大小(单位KB)
upload.setSizeMax(-1);
try {
List items = upload.parseRequest(request);
Iterator it = items.iterator();
// 循环取出表单中元素
while (it.hasNext()) {
FileItem item = (FileItem) it.next();
// 如果是FormField,则获取文件的名称并上传,否则打印表单信息
if (item.isFormField()) { // 不是文件域
String postName = item.getFieldName(); // 参数名
if("username".equals(postName)){
System.out.println("用户名是:"+item.getString()); // 参数值
}else if("userid".equals(postName)){
System.out.println("用户id是:"+item.getString());
}
} else { // 当是文件域的时候,执行上传写入硬盘操作
File temp = new File(item.getName());
System.out.println("文件名为:"+item.getName());
String path = request.getSession().getServletContext().getRealPath("/upload/");
File file = new File(