私には、テキストデータと異なるバイト配列を送ってくるJavaコントローラがあります。だから、私はn multipart要求を構築し、HttpServletResponseからストリームに書き込んでいます。クライアント側(ClosableHttpResponse)のマルチパートレスポンスを受信
私の問題は、クライアント側で応答を解析し、複数の部分を抽出する方法です。
サーバーコードスニペット: -
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
// Prepare payload
builder.addBinaryBody("document1", file);
builder.addBinaryBody("document2", file2);
builder.addPart("stringData", new StringBody(jsonData, ContentType.TEXT_PLAIN));
// Set to request body
HttpEntity entity = builder.build();
postRequest.setEntity(entity);
クライアントコードスニペット: -
HttpPost httpPost = new HttpPost(finalUrl);
StringEntity entity = new StringEntity(json);
httpPost.setEntity(entity);
httpPost.setHeader("Content-type", APPLICATION_JSON_TYPE);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
CloseableHttpResponse response = httpClient.execute(httpPost);
InputStream in = new BufferedInputStream(response.getEntity().getContent());
私はCloseableHttpResponseとHttpEntityをチェックするが、それらのどれもマルチパートリクエストを解析する方法を提供することはありません。
EDIT 1: これは私がクライアント側ストリームで受け付けております私のサンプル応答である: - アパッチから
--bvRi5oZum37DUldtLgQGSbc5RRVZxKpjZMO4SYDe
Content-Disposition: form-data; name="numeric"
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
01010110
--bvRi5oZum37DUldtLgQGSbc5RRVZxKpjZMO4SYDe
Content-Disposition: form-data; name="stringmessage"
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding:8bit
testmessage
--bvRi5oZum37DUldtLgQGSbc5RRVZxKpjZMO4SYDe
Content-Disposition: form-data; name="binarydata"; filename="file1"
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
HI, THIS IS MY BINARY DATA
--bvRi5oZum37DUldtLgQGSbc5RRVZxKpjZMO4SYDe
Content-Disposition: form-data; name="ending"
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
ending
--bvRi5oZum37DUldtLgQGSbc5RRVZxKpjZMO4SYDe--
私はこれがあなたを助けることができると信じています:http://stackoverflow.com/questions/3337056/convenient-way-to-parse-incoming-multipart-form-data-parameters-in-a-servlet – Boschi
@Boschi Fileupload APIメソッドはHTTPServletRequestを受け入れます。これは私の場合はサーブレットからの応答では不可能です。私はここにいくつかのポイントを逃している場合は教えてください。 –
クライアントで受け取ったサンプルメッセージの応答を追加しました。 –