2010-12-15 32 views
2

我有這一段代碼(我沒有寫,只是保持):apache.commons.fileupload拋出MalformedStreamException

public class MyMultipartResolver extends CommonsMultipartResolver{  
    public List parseEmptyRequest(HttpServletRequest request) throws IOException, FileUploadException { 
      String contentType = request.getHeader(CONTENT_TYPE); 
      int boundaryIndex = contentType.indexOf("boundary="); 

      InputStream input = request.getInputStream(); 

      byte[] boundary = contentType.substring(boundaryIndex + 9).getBytes(); 
      MultipartStream multi = new MultipartStream(input, boundary); 
      multi.setHeaderEncoding(getHeaderEncoding()); 

      ArrayList items = new ArrayList(); 
      boolean nextPart = multi.skipPreamble(); 
      while (nextPart) { 
       Map headers = parseHeaders(multi.readHeaders()); 
       // String fieldName = getFieldName(headers); 

       String subContentType = getHeader(headers, CONTENT_TYPE); 
       if (subContentType == null) { 
        FileItem item = createItem(headers, true); 
        OutputStream os = item.getOutputStream(); 
        try { 
         multi.readBodyData(os); 
        } finally { 
         os.close(); 
        } 
        items.add(item); 
       } else { 
        multi.discardBodyData(); 
       } 

       nextPart = multi.readBoundary(); 
      } 

      return items; 
     } 
} 

我使用公地fileupload.jar 1.2.1版明明代碼使用一些過時的方法...
不管怎麼說,嘗試使用此代碼上傳非常大的文件(780 MB)我得到這樣的:

org.apache.commons.fileupload.MultipartStream$MalformedStreamException: Stream ended unexpectedly 
    at org.apache.commons.fileupload.MultipartStream$ItemInputStream.makeAvailable(MultipartStream.java:983) 
    at org.apache.commons.fileupload.MultipartStream$ItemInputStream.read(MultipartStream.java:887) 
    at java.io.InputStream.read(InputStream.java:89) 
    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.MultipartStream.readBodyData(MultipartStream.java:593) 
    at org.apache.commons.fileupload.MultipartStream.discardBodyData(MultipartStream.java:619) 

是從「multi.discardBodyData拋出() ;」線。
我的問題: 如何避免此錯誤,並能夠成功收集FileItems?

+0

的瀏覽器的連接可能超時使用它。在它發生之前需要多長時間? – skaffman 2010-12-15 15:18:54

+0

它立即發生,我在我的機器上本地運行服務器 – Spiderman 2010-12-15 15:21:08

回答

0

(org.apache.commons.fileupload.MultipartStream.MalformedStreamException e) 
     { 

      e.printStackTrace(); 

      return ERROR; 
     } 

捕獲異常,並通過..either InputStreamReturn Error處理它在Struts動作標籤

+0

它不回答OP的問題。 – songyuanyao 2014-05-08 05:55:27

相關問題