title: File upload using apache's httpclient library not working
body: <p>This question is very similar to <a href="https://stackoverflow.com/questions/1067655/how-to-upload-a-file-using-java-httpclient-library-working-with-php-strange-pr">How to upload a file using Java HttpClient library working with PHP</a>, but even  <code>MultipartEntity</code> is not uploading the file correctly. Here is an MWE on the client side:</p>
  
  <pre><code>import java.io.*;
  import java.util.*;
  
  import org.apache.http.entity.mime.*;
  import org.apache.http.client.*;
  import org.apache.http.message.*;
  import org.apache.http.*;
  import org.apache.http.client.methods.HttpPost;
  import org.apache.http.impl.client.DefaultHttpClient;
  import org.apache.http.client.entity.UrlEncodedFormEntity;
  import org.apache.http.client.*;
  import org.apache.http.impl.client.*;
  import org.apache.http.entity.mime.content.*;
  import org.apache.http.impl.cookie.BasicClientCookie;
  import org.apache.http.util.EntityUtils;
  
  
  
  // Emulate the post behavior of curl in java except post a string.
  // https://stackoverflow.com/questions/4205980/java-sending-http-parameters-via-post-method-easily
  
  public class Foo{
  
      static String convertStreamToString(java.io.InputStream is) {
          java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
          return s.hasNext() ? s.next() : "";
      }
      // TODO: Fix and test this method.
      private static void PostData() throws Exception {
          String url = "http://localhost/index.php";
          DefaultHttpClient httpclient = new DefaultHttpClient();
  
          // create the post request.
          HttpPost httppost = new HttpPost(url);
          MultipartEntity entity = new MultipartEntity();
  
          ContentBody body = new FileBody(new File("/tmp/HelloWorld"),
                  org.apache.http.entity.ContentType.APPLICATION_OCTET_STREAM);
          entity.addPart("file", body);
          httppost.setEntity(entity);
  
          // execute request
          HttpResponse response = httpclient.execute(httppost);
          HttpEntity resEntity = response.getEntity();
          InputStream stream = resEntity.getContent();
  
          System.out.println(response.getStatusLine());
          System.out.println(convertStreamToString(stream));
  
      }
      public static void main(String[] args) throws Exception {
          PostData();
      }
  }
  </code></pre>
  
  <p>The contents of <code>/tmp/HelloWorld' are</code>HELLO WORLD`.</p>
  
  <p>Here is what <code>index.php</code> looks like:</p>
  
  <pre><code>&lt;?php
  echo empty($_FILES); 
  print_r($_REQUEST); 
  print_r($_POST); 
  print_r($_GET); 
  print_r($_FILES); 
  &gt;?
  </code></pre>
  
  <p>The output looks like this, which seems to imply that the file contents are sent into <code>$_REQUEST</code> and <code>$_POST</code>, rather than <code>$_FILES</code></p>
  
  <pre><code>1
  Array
  (
      [file] =&gt; HELLO WORLD
  
  )
  Array
  (
      [file] =&gt; HELLO WORLD
  
  )
  Array
  (
  )
  Array
  (
  )
  </code></pre>
  
  <p>My guess is that I am doing something stupid in the client code, but I am not sure what it is.</p>