title: How to upload a file using Apache Commons file upload from a servlet?
body: <p>I need to upload a xml file from server side, where the contents of a file is in a string. How can I make this file content to be uploaded (basically save) on the server?</p>
  
  <p>This is what I am trying, which works fine, If i give a file directly to <code>FileBody</code> but how to trick it to have filecontents going to another servlet as multipart request?</p>
  
  <pre><code>private def createConfiguration(def sessiontoken)
  {
      def xmlString=""
      HttpClient httpclient = new DefaultHttpClient();
      try {
  
          HttpPost httppost = new HttpPost(fileParams.create);
  
          //FileBody bin = new FileBody(new File("C:\\Simon\\myxml.xml"));
          StringBody st = new StringBody(sessiontoken);
          StringBody cfgname = new StringBody(reqParams.c_Cfgname[0]);
          StringBody cfgdesc = new StringBody(reqParams.c_Cfgdesc[0]);
          StringBody cfgtype = new StringBody(reqParams.c_Cfgtype[0]);
          StringBody cfgfile = new StringBody(reqParams.CFGFILE[0]);
  
          MultipartEntity reqEntity = new MultipartEntity();
          reqEntity.addPart("sessiontoken", st);
          reqEntity.addPart("cfgname", cfgname);
          reqEntity.addPart("cfgdesc", cfgdesc);
          reqEntity.addPart("cfgenv", cfgtype);
          //reqEntity.addPart("cfgfile", bin);
          reqEntity.addPart("cfgfile", cfgfile);
  
          httppost.setEntity(reqEntity);
  
          System.out.println("executing request " + httppost.getRequestLine());
          HttpResponse response = httpclient.execute(httppost);
          HttpEntity resEntity = response.getEntity();
  
          System.out.println("----------------------------------------");
          //System.out.println(response.getStatusLine());
          if (resEntity != null) {
              //System.out.println("Response content length: " + resEntity.getContentLength());
              xmlString=resEntity.getContent().getText()
          }
          EntityUtils.consume(resEntity);
      } finally {
          try { httpclient.getConnectionManager().shutdown(); } catch (Exception ignore) {}
      }
      xmlString
  }
  </code></pre>
  
  <p>If I use the above code I get the below Exception </p>
  
  <pre><code>----------------------------------------
  
  
  
  Exception while processing your Request.
  No result defined for action com.abc.dc.actions.CreateConfiguration and
   result input
  </code></pre>
  
  <p><strong>Update</strong></p>
  
  <p>So now after checking the tomcat logs &amp; the other server side code, I came to know that that internally <code>dc</code> is getting <code>cfgfile</code> and setting it to </p>
  
  <pre><code>public void setCfgfile(File cfgfile)
  {
      this.cfgfile = cfgfile
  }
  </code></pre>
  
  <p>which gives me </p>
  
  <pre><code>java.lang.NoSuchMethodException: com.abc.dc.actions.CreateConfiguration.setCfgfile([Ljava.lang.String;)
  </code></pre>
  
  <p>So how can I overload <code>setCfgfile</code> method with <code>public void setCfgfile(String cfgfile)</code> and convert <code>cfgfile</code> into a <code>File</code> object here?</p>
  
  <p>or Even better, </p>
  
  <p>How can I convert this <code>cfgfile</code> string variable into a <code>FileBody</code> object?</p>