Apache HttpClient making multipart form post

后端 未结 3 865
暖寄归人
暖寄归人 2020-11-27 11:39

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 (

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-27 12:04

    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

    http://hc.apache.org/httpcomponents-client-4.3.x/httpmime/examples/org/apache/http/examples/entity/mime/ClientMultipartFormPost.java

    import org.apache.http.entity.mime.MultipartEntityBuilder;
    
    HttpPost httppost = new HttpPost("http://localhost:8080" +
            "/servlets-examples/servlet/RequestInfoExample");
    
    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)
            .build();
    
    
    httppost.setEntity(reqEntity);
    

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

    HttpClient 4.3.x:

    http://hc.apache.org/httpcomponents-client-4.3.x/index.html

    httpmime 4.3.x:

    http://hc.apache.org/httpcomponents-client-4.3.x/httpmime/dependency-info.html

提交回复
热议问题