title: Servlet file upload ismultipartcontent returns false
body: <p>I am trying to upload a selected file from the local file system to a folder within the application.
  Here technology used is jsp, servlet.</p>
  
  <p>On clicking the Press button of Example.jsp, the control passes to the Request servlet.
  But the checking that if the request is multi part returns false.
  so "Sorry this Servlet only handles file upload request" is printed in the console.</p>
  
  <p>Example.jsp</p>
  
  <pre><code>&lt;form action="Request" method="Post" name = "invoices"  target="_self"&gt;
  
  &lt;div class="invoicetable"&gt;  
  &lt;table&gt;
  
      &lt;colgroup&gt;
          &lt;col class="first" span="1"&gt;
          &lt;col class="rest" span="1"&gt;
      &lt;/colgroup&gt;
  
      &lt;tr&gt;
          &lt;td&gt;Start date:&lt;/td&gt;
          &lt;td&gt;&lt;input type = "text" name = "start_date" id="datepicker"  size = "6" maxlength="10"/&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;End date:&lt;/td&gt;
          &lt;td&gt;&lt;input type = "text" name = "end_date" id="datepicker2"  size = "6" maxlength="10"/&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;form action="Request" enctype="multipart/form-data" method="post" name = "upload"&gt;
              &lt;table&gt;
              &lt;tr&gt;
                  &lt;td&gt;Input File:&lt;/td&gt;
                  &lt;td&gt;&lt;input type = "file" name ="datafile" size="30" maxlength="200"/&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;/table&gt;
              &lt;div&gt;
                  &lt;input type ="submit" name="invoice" value="Press"/&gt; to upload the file!
                  &lt;input type="hidden"  name = "type" value="upload"&gt;      
              &lt;/div&gt;
  
          &lt;/form&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;Regular&lt;/td&gt;
          &lt;td&gt;
              &lt;input type = "radio" name ="input_type" value="regular"/&gt;
  
          &lt;/td&gt;
      &lt;/tr&gt;
          &lt;tr&gt;
          &lt;td&gt;Manual&lt;/td&gt;
          &lt;td&gt;
              &lt;input type = "radio" name ="input_type" value="manual" checked/&gt;
  
          &lt;/td&gt;
      &lt;/tr&gt;
          &lt;tr&gt;
          &lt;td&gt;Final&lt;/td&gt;
          &lt;td&gt;
              &lt;input type = "radio" name ="input_type" value="final"/&gt;
  
          &lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
  
  &lt;/div&gt;
  
  &lt;div style="margin-left: 20px"&gt;
      &lt;input type ="submit" name="invoice" value="Submit" class="button"/&gt;
      &lt;input type="hidden"  name = "type" value="invoice"&gt;      
  &lt;/div&gt;
  
  &lt;/form&gt;
  </code></pre>
  
  <p>Request - doPost method</p>
  
  <pre><code>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&lt;FileItem&gt; fileItemsList = uploader.parseRequest(request);
                          Iterator&lt;FileItem&gt; 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");
                  }
  
  
  }
  </code></pre>
  
  <p>I tried googling for a possible solution. but didnot land with any answer.
  Any help appreciated.</p>