Apache HttpClient制作多部分表单帖子

时间:2010-02-21 03:35:39

标签: java multipartform-data apache-httpclient-4.x

我对HttpClient非常环保,而且我发现缺少(和或者说明显不正确)的文档非常令人沮丧。我正在尝试使用Apache Http Client实现以下帖子(如下所列),但不知道如何实际执行此操作。我将在下周的文档中埋葬自己,但也许更有经验的HttpClient程序员可以尽快给我一个答案。

发表:

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

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

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


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

agree
-----------------------------1294919323195--

3 个答案:

答案 0 :(得分:88)

使用HttpMime library中的MultipartEntityBuilder执行所需的请求。

在我的项目中,我这样做:

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

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

希望这会有所帮助。

(更新此帖子以使用MultipartEntityBuilder而不是弃用的MultipartEntity,使用@mtomy代码作为示例)

答案 1 :(得分:14)

  

现在,MultipartEntity显示为已弃用。我正在使用apache   httpclient 4.3.3 - 有谁知道我们应该使用什么   代替?我发现谷歌搜索充满了MultipartEntity   例子我找不到任何东西。 - vextorspace 2014年3月31日20:36

以下是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);

要使用MultipartEntityBuilder类,您需要 httpmime ,这是 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

答案 2 :(得分:1)

如果使用org.apache.commons.httpclient.HttpClient包,也许可以帮到你!

    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());
    postMethod.setRequestEntity(multipartRequestEntity);
    httpClient.executeMethod(postMethod);
    String responseStr = postMethod.getResponseBodyAsString();