2014-04-02 75 views
2

我想從本地文件系統上傳一個選定的文件到應用程序內的文件夾。 這裏使用的技術是jsp,servlet。Servlet文件上傳ismultipartcontent返回false

單擊Example.jsp的按鈕後,控件傳遞給Request servlet。 但是,如果請求是多部分的檢查返回false。 所以「對不起,這個Servlet只處理文件上傳請求」被打印在控制檯中。

Example.jsp

<form action="Request" method="Post" name = "invoices" target="_self"> 

<div class="invoicetable"> 
<table> 

    <colgroup> 
     <col class="first" span="1"> 
     <col class="rest" span="1"> 
    </colgroup> 

    <tr> 
     <td>Start date:</td> 
     <td><input type = "text" name = "start_date" id="datepicker" size = "6" maxlength="10"/></td> 
    </tr> 
    <tr> 
     <td>End date:</td> 
     <td><input type = "text" name = "end_date" id="datepicker2" size = "6" maxlength="10"/></td> 
    </tr> 
    <tr> 
     <form action="Request" enctype="multipart/form-data" method="post" name = "upload"> 
      <table> 
      <tr> 
       <td>Input File:</td> 
       <td><input type = "file" name ="datafile" size="30" maxlength="200"/></td> 
      </tr> 
      </table> 
      <div> 
       <input type ="submit" name="invoice" value="Press"/> to upload the file! 
       <input type="hidden" name = "type" value="upload">  
      </div> 

     </form> 
    </tr> 
    <tr> 
     <td>Regular</td> 
     <td> 
      <input type = "radio" name ="input_type" value="regular"/> 

     </td> 
    </tr> 
     <tr> 
     <td>Manual</td> 
     <td> 
      <input type = "radio" name ="input_type" value="manual" checked/> 

     </td> 
    </tr> 
     <tr> 
     <td>Final</td> 
     <td> 
      <input type = "radio" name ="input_type" value="final"/> 

     </td> 
    </tr> 
</table> 

</div> 

<div style="margin-left: 20px"> 
    <input type ="submit" name="invoice" value="Submit" class="button"/> 
    <input type="hidden" name = "type" value="invoice">  
</div> 

</form> 

請求 - doPost方法

jsp_request = request.getParameter("type"); 
if (jsp_request.equalsIgnoreCase("upload")) { 
      String file_location = request.getParameter("datafile"); 
      ServletFileUpload uploader = null; 

        //Checking if the request is multipart 
      if(ServletFileUpload.isMultipartContent(request)){ 


        try { 
         String upload_location_holder = request.getServletContext().getRealPath(""); 
         upload_location_holder = upload_location_holder.substring(0,upload_location_holder.indexOf(".")) + 
         upload_location_holder.substring(upload_location_holder.lastIndexOf("/")+1); 


         //excel file goes into the input files folder 
         String excel_name = upload_location_holder + "/WebContent/input_files/" + file_location; 

         File file = new File(excel_name); 

         DiskFileItemFactory fileFactory=new DiskFileItemFactory(); 

         fileFactory.setRepository(file); 
         uploader = new ServletFileUpload(fileFactory); 

         List<FileItem> fileItemsList = uploader.parseRequest(request); 
         Iterator<FileItem> fileItemsIterator = fileItemsList.iterator(); 

         while (fileItemsIterator.hasNext()) { 
          FileItem item = (FileItem) fileItemsIterator.next(); 
          if (!item.isFormField()) { 
           String fileName = item.getName();  
           String root = getServletContext().getRealPath("/"); 
           File path = new File(root + "/uploads"); 
           if (!path.exists()) { 
            boolean status = path.mkdirs(); 
           } 

           File uploadedFile = new File(path + "/" + fileName); 
           System.out.println(uploadedFile.getAbsolutePath()); 
           item.write(uploadedFile); 
          } 
         } 

         //File uploaded successfully 
         request.setAttribute("message", "File Uploaded Successfully"); 
        } catch (Exception ex) { 
         // request.setAttribute("message", "File Upload Failed due to " + ex); 
         ex.printStackTrace(); 
        }   

       }else{ 
        System.out.println("Sorry this Servlet only handles file upload request"); 
       } 


} 

我試圖使用Google爲可能的解決方案。但沒有得到任何答案。 任何幫助表示讚賞。

回答

5

親愛的,在你的表單標籤使用enctype="multipart/form-data",如:

form action="Request" method="Post" name="logout" enctype="multipart/form-data" 
+0

我已經在內部形式使用ENCTYPE = 「的multipart/form-data的」(上傳) –

+1

@EvesMary的問題是,你有一種「內在」形式;你不能在另一個'

'元素中有''元素。 –

+0

@AnthonyGrist:ohh..ok..i wil在刪除內部表單後更新你.. –