1

This question is very similar to How to upload a file using Java HttpClient library working with PHP, but even MultipartEntity is not uploading the file correctly. Here is an MWE on the client side:

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();
    }
}

The contents of /tmp/HelloWorld' areHELLO WORLD`.

Here is what index.php looks like:

<?php
echo empty($_FILES); 
print_r($_REQUEST); 
print_r($_POST); 
print_r($_GET); 
print_r($_FILES); 
>?

The output looks like this, which seems to imply that the file contents are sent into $_REQUEST and $_POST, rather than $_FILES

1
Array
(
    [file] => HELLO WORLD

)
Array
(
    [file] => HELLO WORLD

)
Array
(
)
Array
(
)

My guess is that I am doing something stupid in the client code, but I am not sure what it is.

Community
  • 1
  • 1
merlin2011
  • 71,677
  • 44
  • 195
  • 329

1 Answers1

2

I dug into the PHP source code, and apparently a filename is required on the Content-Disposition line. Therefore, adding the following code, from this answer, in the client solves the problem.

    FormBodyPart customBodyPart = new FormBodyPart("file", body) {
            @Override
            protected void generateContentDisp(final ContentBody body) {
                StringBuilder buffer = new StringBuilder();
                buffer.append("form-data; name=\"");
                buffer.append(getName());
                buffer.append("\"");
                buffer.append("; filename=\"-\"");
                addField(MIME.CONTENT_DISPOSITION, buffer.toString());
            }
    };
    entity.addPart(customBodyPart);
Community
  • 1
  • 1
merlin2011
  • 71,677
  • 44
  • 195
  • 329