2016-09-29 19 views
1

我想創建一個文件上傳。我的文件輸入:Java Fileupload getParts()返回文件和一個空值

<input class="form-control" name="file" type="file" size="20" accept="application/pdf, image/jpeg, image/png, text/plain" multiple > 

而這個servlet中的for-loop迭代零件以獲得其總大小。它遍歷所選文件和一個大小爲1且內容類型爲null的附加元素。這導致了一個錯誤。

for (Part part : request.getParts()) { 
      System.out.println("Dateigrösse: " + part.getSize()); 
      uploadSize = uploadSize + part.getSize(); 
      System.out.println("Dateityp: " + part.getContentType()); 
      if(part.getContentType().equals("application/pdf") || part.getContentType().equals("text/plain") || part.getContentType().equals("image/jpeg") || part.getContentType().equals("image/png")) { 
      } else { 
       types = false; 
      } 
     } 

Here I iterate over the list and print out the size & type

首先,它的工作,但知道有這個空元素。我該怎麼辦?它來自哪裏?我什麼都不改變。我使用oracle EE7 doc中的代碼和一篇着名的在線文章。在這個for循環之前,我沒有改變任何東西。

預先感謝您。

回答

0

getParts()也會返回所有指定的輸入。所以它將我選擇的主題作爲一部分返回給我。現在我正在過濾名爲「文件」的輸入。