title: Http MultiPart request
body: <p>I am trying to upload an image (multi-part/form-data) using httpClient library. I am able to upload the image using httpPost Method and a byteArrayRequestEntity. Following is the code I used:</p>
  
  <pre><code> File file = new File(imageFilePath);
  
   HttpClient client = new HttpClient();
  
   PostMethod method = new PostMethod("https://domain/link/folderId/files?access_token="+accessToken);
  
  
   method.addRequestHeader("Content-Type","multipart/form-data;boundary=AaB03x");
  
   String boundary = "AaB03x";
  
   StringBuilder builder = new StringBuilder();
   builder.append("--");
   builder.append(boundary+"\r\n");
   builder.append("Content-Disposition: form-data; name=\"file\"; filename=\"photo.jpg\"");
   builder.append("\r\n");
   builder.append("Content-Type: image/jpeg");
   builder.append("\r\n");
   builder.append("\r\n");
  
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   baos.write(builder.toString().getBytes("utf-8"));
   builder.setLength(0);
  
   InputStream is = new FileInputStream(file);
   byte[] buffer = new byte[4096];
   int nbRead = is.read(buffer);
   while(nbRead &gt; 0) {
       baos.write(buffer, 0, nbRead);
       nbRead = is.read(buffer);
   }
  
   is.close();
   builder.append("\r\n");
   builder.append("--");
   builder.append(boundary);
   builder.append("--");
   builder.append("\r\n");
  
   baos.write(builder.toString().getBytes("utf-8"));
  
   method.setRequestEntity(new ByteArrayRequestEntity(baos.toByteArray(), "multipart/form-data; boundary=\"" + boundary + "\""));
  
  
   System.out.println(method.getRequestEntity().toString());
   client.executeMethod(method);
  </code></pre>
  
  <p>But the project i am working on requires me to use an httpRequest and not Http PostMethod.
  I tried with basicHttpEntityEnclosingRequest, but the setEntity method for the same accepts only a httpEntity (i was using ByteArrayRequestEntity). </p>
  
  <p>Could anyone help me with how to modify the code so that it uses a HttpRequest (or its subtype) instead of a PostMethod?</p>