Apache HttpClient making multipart form post

0 votes
asked Feb 21, 2010 by russ

I'm pretty green to HttpClient and I'm finding the lack of (and or blatantly incorrect) documentation extremely frustrating. I'm trying to implement the following post (listed below) with Apache Http Client, but have no idea how to actually do it. I'm going to bury myself in documentation for the next week, but perhaps more experienced HttpClient coders could get me an answer sooner.


Content-Type: multipart/form-data; boundary=---------------------------1294919323195
Content-Length: 502
Content-Disposition: form-data; name="number"

Content-Disposition: form-data; name="clip"

Content-Disposition: form-data; name="upload_file"; filename=""
Content-Type: application/octet-stream

Content-Disposition: form-data; name="tos"


3 Answers

0 votes
answered Feb 5, 2010 by mtomy

Use MultipartEntityBuilder from the HttpMime library to perform the request you want.

In my project I do that this way:

HttpEntity entity = MultipartEntityBuilder
    .addTextBody("number", "5555555555")
    .addTextBody("clip", "rickroll")
    .addBinaryBody("upload_file", new File(filePath), ContentType.create("application/octet-stream"), "filename")
    .addTextBody("tos", "agree")

HttpPost httpPost = new HttpPost("http://some-web-site");
HttpResponse response = httpClient.execute(httpPost);
HttpEntity result = response.getEntity();

Hope this will help.

(Updated this post to use MultipartEntityBuilder instead of deprecated MultipartEntity, using @mtomy code as the example)

0 votes
answered Feb 22, 2015 by li-ying

MultipartEntity now shows up as deprecated. I am using apache httpclient 4.3.3 - does anyone know what we are supposed to use instead? I find the google searches to be so full of MultipartEntity examples I can't find anything. – vextorspace Mar 31 '14 at 20:36

Here is the sample code in HttpClient 4.3.x


import org.apache.http.entity.mime.MultipartEntityBuilder;

HttpPost httppost = new HttpPost("http://localhost:8080" +

FileBody bin = new FileBody(new File(args[0]));
StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);

HttpEntity reqEntity = MultipartEntityBuilder.create()
        .addPart("bin", bin)
        .addPart("comment", comment)


To use the class MultipartEntityBuilder, you need httpmime, which is a sub project of HttpClient

HttpClient 4.3.x:


httpmime 4.3.x:


0 votes
answered Sep 15, 2017 by alps1992

if use org.apache.commons.httpclient.HttpClient package, maybe that can help you!

    HttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager();
    //here should set HttpConnectionManagerParams but not important for you
    HttpClient httpClient = new HttpClient(httpConnectionManager);

    PostMethod postMethod = new PostMethod("http://localhost/media");

    FilePart filePart = new FilePart("file", new File(filepath));
    StringPart typePart = new StringPart("type", fileContent.getType(), "utf-8");
    StringPart fileNamePart = new StringPart("fileName", fileContent.getFileName(), "utf-8");
    StringPart timestampPart = new StringPart("timestamp", ""+fileContent.getTimestamp(),"utf-8");
    Part[] parts = { typePart, fileNamePart, timestampPart, filePart };

    MultipartRequestEntity multipartRequestEntity = new MultipartRequestEntity(parts, postMethod.getParams());
    String responseStr = postMethod.getResponseBodyAsString();
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter