Java Apache HttpClient 制作多部分表单发布
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2304663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Apache HttpClient making multipart form post
提问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.
我对 HttpClient 很不友好,我发现缺少(和/或明显不正确)文档非常令人沮丧。我正在尝试使用 Apache Http Client 实现以下帖子(如下所列),但不知道如何实际执行。下周我将把自己埋在文档中,但也许更有经验的 HttpClient 编码人员可以更快地给我答案。
Post:
邮政:
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--
采纳答案by mtomy
Use MultipartEntityBuilder from the HttpMime libraryto perform the request you want.
使用HttpMime 库中的MultipartEntityBuilder来执行您想要的请求。
In my project I do that this way:
在我的项目中,我这样做:
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();
Hope this will help.
希望这会有所帮助。
(Updated this post to use MultipartEntityBuilder instead of deprecated MultipartEntity, using @mtomy code as the example)
(更新此帖子以使用 MultipartEntityBuilder 而不是弃用的 MultipartEntity,以@mtomy 代码为例)
回答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
MultipartEntity 现在显示为已弃用。我正在使用 apache httpclient 4.3.3 - 有谁知道我们应该使用什么?我发现谷歌搜索充满了 MultipartEntity 示例,我找不到任何东西。– vextorspace 2014 年 3 月 31 日 20:36
Here is the sample code in HttpClient 4.3.x
这是 HttpClient 4.3.x 中的示例代码
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
要使用 MultipartEntityBuilder 类,您需要httpmime,它是HttpClient的子项目
HttpClient 4.3.x:
HttpClient 4.3.x:
http://hc.apache.org/httpcomponents-client-4.3.x/index.html
http://hc.apache.org/httpcomponents-client-4.3.x/index.html
httpmime 4.3.x:
httpmime 4.3.x:
http://hc.apache.org/httpcomponents-client-4.3.x/httpmime/dependency-info.html
http://hc.apache.org/httpcomponents-client-4.3.x/httpmime/dependency-info.html
回答by Alps1992
if use org.apache.commons.httpclient.HttpClient package, maybe that can help you!
如果使用 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();