7

What is the equivalent in java for the following curl command:

curl -X POST -F "file=@$File_PATH"

The request I want to execute using Java is :

curl -X POST -F 'file=@file_path' http://localhost/files/ 

I was trying :

            HttpClient httpClient = new DefaultHttpClient();        

    HttpPost httpPost = new HttpPost(_URL);

    File file = new File(PATH);

            MultipartEntity mpEntity = new MultipartEntity();
        ContentBody cbFile = new FileBody(file, "bin");
        mpEntity.addPart("userfile", cbFile);

        httpPost.setEntity(mpEntity);

    HttpResponse response = httpClient.execute(httpPost);
    InputStream instream = response.getEntity().getContent();
amine
  • 181
  • 1
  • 2
  • 9
  • What exactly is your problem? And a bit mroe code would be helpfull, what is `httpPost` e.g.? – Angel O'Sphere Oct 10 '11 at 13:30
  • I'm trying to send the curl command (already a Linux terminal command) using a java program. I've tried multipart but I don't need to upload or download the file, it's rather a transfer between distant repository. – amine Oct 10 '11 at 13:32
  • Well, your Java code is incomplete. And we don't know why it does not work. So post more code please (and yes, we all know what `curl` is .... sigh). E.g. you don't call any post-method, so that fragment above can't work, obviously. You need at least an HttpURLConnection ... – Angel O'Sphere Oct 10 '11 at 13:40
  • I'm using org.apache.http.client to communicate with a REST server. I've already made requests using cURL. What I'm trying to do now is to execute these requests from a java program. The problem is that in one of the requests need to make a file transfer, with curl It's made using -F option, the question is how to do it in java ? – amine Oct 10 '11 at 14:10
  • By calling the correct method of your connection object. AGAIN: why can't you simply edit your question and add ALL your code? http://hc.apache.org/httpclient-3.x/methods/post.html – Angel O'Sphere Oct 10 '11 at 14:13
  • AGAIN : The problem is just with the -F option, I've tested the program with multiple requests and it's fine – amine Oct 10 '11 at 14:30
  • 1
    This should work: [How to upload a file using Java HttpClient](http://stackoverflow.com/questions/1067655/how-to-upload-a-file-using-java-httpclient-library-working-with-php-strange-pro) – tolitius Oct 10 '11 at 15:06

1 Answers1

3

I ran across this problem yesterday. Here is a solution that uses Apache http libraries.

package curldashf;

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.fluent.Request;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.util.EntityUtils;

public class CurlDashF
{
    public static void main(String[] args) throws ClientProtocolException, IOException
    {
        String filePath = "file_path";
        String url = "http://localhost/files";
        File file = new File(filePath);
        MultipartEntity entity = new MultipartEntity();
        entity.addPart("file", new FileBody(file));
        HttpResponse returnResponse = Request.Post(url)
            .body(entity)
            .execute().returnResponse();
        System.out.println("Response status: " + returnResponse.getStatusLine().getStatusCode());
        System.out.println(EntityUtils.toString(returnResponse.getEntity()));
    }
}

Set filePath and url as necessary. If you are using something other than a file, you can substitute FileBody with ByteArrayBody, InputStreamBody or StringBody. My particular situation called for ByteArrayBody but the code above works for a file.

tjmorrison
  • 31
  • 2