Apache HttpClient作multipart形ポスト
-
21-09-2019 - |
質問
思緑HttpClientんの不足(または露骨に誤トでは非常に悔しいです。ようにしている実施のため次の投稿(下)Apache Http顧客がいない方はアイデアを実際にいます。私は地下に埋設する自分の翌週間も経験した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--
解決
使用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();
・ホープ、この意志の助けを借りています。
(例として@mtomyコードを使用して、代わりに非推奨MultipartEntityのMultipartEntityBuilderを使用するには、このポストを更新)
他のヒント
MultipartEntity現するとして推奨されていません。を使ってapache httpclient4.3.3-なんだろうけど、日本人にしか利用 ょうか?それはgoogle検索するこれら全てがそろっているMultipartEntity 例においてあります。–vextorspace月31'14 20:36
こちらはサンプルコードHttpClient4.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);
利用のクラスMultipartEntityBuilderが必要 httpmime, であるサブプロジェクトの HttpClient
HttpClient4.3.x:
http://hc.apache.org/httpcomponents-client-4.3.x/index.html
httpmime4.3.x:
http://hc.apache.org/httpcomponents-client-4.3.x/httpmime/dependency-info.html
使用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();