title: Multiple files upload using REST
body: <p>I have a input type=file tag in my html which allows user to select multiple files. The action for the form is a REST web service:</p>
  
  <pre><code>@POST
  @Path("savefile")
  @Produces ({MediaType.TEXT_PLAIN})
  public String createObjects(
          @FormDataParam("datafile") FormDataMultiPart file,
          @FormParam("username") String unm
          //@Context HttpServletRequest request
          ){....}
  </code></pre>
  
  <p>Initially I used the request object to retrieve all FileItems in the request and then saved it to the server. No problem with that. Now I want to send a string data along with the files. For that I read that the parameters need to be of type FormDataParam. Hence I have added that parameter. This is my client code :</p>
  
  <pre><code>&lt;form id="form1" action="http://comp1:8080/RestWSGS/jersey/UploadFiles/savefile"
          enctype="multipart/form-data" method="post"&gt;
          &lt;input name="username" type="text" style="display:none" value="abc"/&gt;
    &lt;input id="loadId" multiple="multiple" 
          type="file" name="datafile" required="required" autofocus="autofocus"
          onchange="selectFiles(this)"/&gt;
    &lt;div&gt;
      &lt;input style="display: none;" type="submit" value="Send"&gt;
    &lt;/div&gt;
   &lt;/form&gt;  
  </code></pre>
  
  <p>I am not sure what must be the type of the file parameter to allow multiple files in it????
  Either the file parameter gives me multiple files OR do I have to revert back to the @Context injection?? If so how will I retrieve the string parameter?</p>
  
  <p>Any help is welcome!</p>
  
  <p>EDIT:
  I have modified my REST ws to the following:</p>
  
  <pre><code>@POST
  @Path("savefile")
  //@Consumes (MediaType.MULTIPART_FORM_DATA)
  public void createObjects(
          //@FormDataParam("datafile") FormDataMultiPart file,
          //@FormParam("username") String unm
          @Context HttpServletRequest request
          )
  {
      try
      {
          FileHandler f;
          f = new FileHandler(new File (getClass().getResource("/" +getClass().getName().substring(
                  0, getClass().getName().indexOf("."))).getPath()).getParent().replaceAll("\\\\", "\\\\\\\\")  + "/mylog.log");
          logger.addHandler(f);
  
      }
      catch (SecurityException e1)
      {
          logger.info(e1.getMessage());
  
      }
      catch (IOException e1)
      {
          logger.info(e1.getMessage());
          //e1.printStackTrace();
      }
  
  
      ApplicationConstants.ROOTPATH = new File (getClass().getResource("/" +getClass().getName().substring(
              0, getClass().getName().indexOf("."))).getPath()).getParent().replaceAll("\\\\", "\\\\\\\\") ;
      ApplicationConstants.ROOTPATH = ApplicationConstants.ROOTPATH.substring
      (0, ApplicationConstants.ROOTPATH.indexOf("\\") + 2);
      String user = request.getParameter("username");
      logger.info("ApplicationConstants.ROOTPATH" + ApplicationConstants.ROOTPATH);
  
  
      try
      {
          for (Part part : request.getParts())
          {
                  try
                  {
                      logger.info("part = " + part.getName());
                      if (!part.getName().equalsIgnoreCase("username"))
                      {
                          String fileName = processFileName(part.getName());
                          part.write(new File(ApplicationConstants.ROOTPATH + user + "\\" + fileName).getPath());
                      }
  
                      else
                      {
                           user = request.getParameter("username");
                           logger.info("user = " + user);
                      }
                  }
                  catch (IOException e)
                  {
                      logger.info(e.getMessage());
  
                  }
          }
      }
      catch (IOException e)
      {
  
      }
      catch (ServletException e)
      {
  
      }
  }
  </code></pre>
  
  <p>But I am always getting the value from request.getParameter("username") as null. I dont know what is going wrong!! Is it illegal to send other data as well in a multipart/form-data form?? I need some pointers here. Please suggest any improvements in this code.</p>