我有一个java控制器,它必须向我发送一些文本数据和不同的字节数组.所以我正在构建n多部分请求并将其写入来自HttpServletResponse的流.
现在我的问题是如何在客户端解析响应并提取多个部分.
服务器代码链: -
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);
Run Code Online (Sandbox Code Playgroud)
客户代码SNIPPET: -
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());
Run Code Online (Sandbox Code Playgroud)
我检查了CloseableHttpResponse和HttpEntity,但它们都没有提供解析多部分请求的方法.
编辑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 …Run Code Online (Sandbox Code Playgroud)