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(path,temp.getName()); item.write(file); } } } catch (FileUploadException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); }
异常出在了List items = upload.parseRequest(request);这句
请求解答
还有如果中断了上传,那么输入输出流是怎么进行管理的?
[解决办法]
把要上传的文件读取到内存中 (string ,管道)中 然后 看客户端是怎么操作的 如果 是终止 那么 就把 这些数据先保存到硬盘上 如果是终止就要删除数据 那么 就不用吧数据写进硬盘上
[解决办法]
里面的内容是这样的: ServletFileUpload::::::
public List parseRequest(HttpServletRequest request)
throws FileUploadException
{
return parseRequest(new ServletRequestContext(request));
}
--->
public List parseRequest(RequestContext ctx)
throws FileUploadException
{
try
{
FileItemIterator iter = getItemIterator(ctx);
List items = new ArrayList();
FileItemFactory fac = getFileItemFactory();
if (fac == null) {
throw new NullPointerException("No FileItemFactory has been set.");
}
while (iter.hasNext()) {
FileItemStream item = iter.next();
FileItem fileItem = fac.createItem(item.getFieldName(), item.getContentType(), item.isFormField(), item.getName());
try
{
Streams.copy(item.openStream(), fileItem.getOutputStream(), true);
}
catch (FileUploadIOException e) {
throw ((FileUploadException)e.getCause());
} catch (IOException e) {
throw new IOFileUploadException("Processing of multipart/form-data request failed. " + e.getMessage(), e);
}
items.add(fileItem);
}
return items;
} catch (FileUploadIOException e) {
throw ((FileUploadException)e.getCause());
} catch (IOException e) {
throw new FileUploadException(e.getMessage(), e);
}
}