2015-05-04 95 views
0

我正在嘗試上傳文件以及文本字段。儘管據說我無法通過request.getParameter("remarks");檢索文本字段的值,但我導入了commons-fileupload。雖然我似乎無法弄清楚什麼是錯我的代碼,其中,它在這一行List<FileItem> items = upload.parseRequest(request);一個錯誤說SEVERE: Servlet.service() for servlet [SubmitResult] in context with path [/HIS] threw exception [Servlet execution threw an exception] with root cause java.lang.ClassNotFoundException: org.apache.commons.io.IOUtils使用多部分表單數據和文本字段上傳文件

HTML:

<div class="modal-body"> 
    <input type="hidden" name="orderid2" id="orderid2"> 
    <table> 
     <tr> 
      <td> 
       Remarks: 
      </td> 
      <td> 
       <input type="text" name="remarks" placeholder="Remarks" autocomplete="off" class="form-control placeholder-no-fix"> <br> 
      </td> 
     </tr> 
     <tr> 
      <td> 
       Upload File: &nbsp; 
      </td> 
      <td> 
       <input type="file" name="file" id="file" accept="image/png, .txt, application/pdf, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/msword"/> <br> 
      </td> 
     </tr> 
    </table> 
</div> 

的Servlet:

String rtempfile = File.createTempFile("temp", "1").getParent(); 
    MultipartRequest multi = new MultipartRequest(request, rtempfile, 15*1024*1024); 
    Enumeration files = multi.getFileNames(); 

    DiskFileItemFactory factory = new DiskFileItemFactory(); 
    ServletFileUpload upload = new ServletFileUpload(factory); 
    List<FileItem> items = upload.parseRequest(request); 
    Iterator<FileItem> iter = items.iterator(); 
    while(iter.hasNext()) { 
     FileItem item = iter.next(); 
     if(item.isFormField()) { 
     String name = item.getFieldName(); 
     String value = item.getString(); 
      if(name.equals("orderid2")) 
       order.setOrderID(Integer.parseInt(value)); 
      else if(name.equals("remarks")) 
       order.setRemarks(value); 
     } 
    } 
    orderDAO.submitResult(order, multi, files); 
+3

似乎您沒有將Commons IO jar包含到您的項目中,只需下載幷包含它們即可。 http://commons.apache.org/proper/commons-io/download_io.cgi – Parth

+0

@Parth謝謝!這解決了錯誤 – nubteens

回答

0
  MultipartRequest multi = new MultipartRequest(request,appPath,10*1024*1024); // 10MB 
      Enumeration params = multi.getParameterNames(); 
      while (params.hasMoreElements()) 
      { 
       paramname = (String) params.nextElement(); 

       if(paramname.equalsIgnoreCase("cname")) 
       { 
        Class=multi.getParameter(paramname); 
       } 

       if(paramname.equalsIgnoreCase("pno")) 
       { 
        phone=multi.getParameter(paramname); 
       } 
       if(paramname.equalsIgnoreCase("fee")) 
       { 
        fee=multi.getParameter(paramname); 
       } 

       if(paramname.equalsIgnoreCase("fname")) 
       { 
        image=multi.getParameter(paramname); 
       } 

       if(paramname.equalsIgnoreCase("fname1")) 
       { 
        image1=multi.getParameter(paramname); 
       } 
      } 
+0

雖然代碼是讚賞,它應該總是有一個附帶的解釋。這並不需要很長時間,但它是可以預料的。 – peterh

相關問題