title: How to upload a file using Java HttpClient library working with PHP
body: <p>I want to write Java application that will upload a file to the Apache server with PHP. The Java code uses Jakarta HttpClient library version 4.0 beta2:</p>
  
  <pre><code>import java.io.File;
  import org.apache.http.HttpEntity;
  import org.apache.http.HttpResponse;
  import org.apache.http.HttpVersion;
  import org.apache.http.client.HttpClient;
  import org.apache.http.client.methods.HttpPost;
  import org.apache.http.entity.FileEntity;
  import org.apache.http.impl.client.DefaultHttpClient;
  import org.apache.http.params.CoreProtocolPNames;
  import org.apache.http.util.EntityUtils;
  
  
  public class PostFile {
    public static void main(String[] args) throws Exception {
      HttpClient httpclient = new DefaultHttpClient();
      httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
  
      HttpPost httppost = new HttpPost("http://localhost:9002/upload.php");
      File file = new File("c:/TRASH/zaba_1.jpg");
  
      FileEntity reqEntity = new FileEntity(file, "binary/octet-stream");
  
      httppost.setEntity(reqEntity);
      reqEntity.setContentType("binary/octet-stream");
      System.out.println("executing request " + httppost.getRequestLine());
      HttpResponse response = httpclient.execute(httppost);
      HttpEntity resEntity = response.getEntity();
  
      System.out.println(response.getStatusLine());
      if (resEntity != null) {
        System.out.println(EntityUtils.toString(resEntity));
      }
      if (resEntity != null) {
        resEntity.consumeContent();
      }
  
      httpclient.getConnectionManager().shutdown();
    }
  }
  </code></pre>
  
  <p>The PHP file <code>upload.php</code> is very simple:</p>
  
  <pre><code>&lt;?php
  if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
    echo "File ". $_FILES['userfile']['name'] ." uploaded successfully.\n";
    move_uploaded_file ($_FILES['userfile'] ['tmp_name'], $_FILES['userfile'] ['name']);
  } else {
    echo "Possible file upload attack: ";
    echo "filename '". $_FILES['userfile']['tmp_name'] . "'.";
    print_r($_FILES);
  }
  ?&gt;
  </code></pre>
  
  <p>Reading the response I get the following result:</p>
  
  <pre><code>executing request POST http://localhost:9002/upload.php HTTP/1.1
  </code></pre>
  
  <pre>
  HTTP/1.1 200 OK
  Possible file upload attack: filename ''.
  Array
  (
  )
  </pre>
  
  <p>So the request was successful, I was able to communicate with server, however PHP didn't notice the file - the method <code>is_uploaded_file</code> returned <code>false</code> and <code>$_FILES</code> variable is empty. I have no idea why this might happend. I have tracked HTTP response and request and they look ok:<br>
  request is:</p>
  
  <pre>
  POST /upload.php HTTP/1.1
  Content-Length: 13091
  Content-Type: binary/octet-stream
  Host: localhost:9002
  Connection: Keep-Alive
  User-Agent: Apache-HttpClient/4.0-beta2 (java 1.5)
  Expect: 100-Continue
  
  ˙Ř˙ŕ..... the rest of the binary file...
  </pre>
  
  <p>and response:</p>
  
  <pre>
  HTTP/1.1 100 Continue
  
  HTTP/1.1 200 OK
  Date: Wed, 01 Jul 2009 06:51:57 GMT
  Server: Apache/2.2.8 (Win32) DAV/2 mod_ssl/2.2.8 OpenSSL/0.9.8g mod_autoindex_color PHP/5.2.5 mod_jk/1.2.26
  X-Powered-By: PHP/5.2.5
  Content-Length: 51
  Keep-Alive: timeout=5, max=100
  Connection: Keep-Alive
  Content-Type: text/html
  
  Possible file upload attack: filename ''.Array
  (
  )
  </pre>
  
  <p>I was testing this both on the local windows xp with xampp and remote Linux server. I have also tried to use previous version of HttpClient - version 3.1 - and the result was even more unclear, <code>is_uploaded_file</code> returned <code>false</code>, however <code>$_FILES</code> array was filled with proper data.</p>