【发布时间】:2015-03-17 16:39:11
【问题描述】:
我需要一些帮助,说明如何将 HttpAsyncClient 与多部分实体一起使用。基本上我正在尝试使用 HttpAsyncClient 将文件(字节 [])的内容作为多部分正文上传。 我如何使用它的示例:
CloseableHttpAsyncClient httpAsyncClient = HttpAsyncClients.createDefault();
httpAsyncClient.start();
MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create();
reqEntity.addPart("file",new ByteArrayBody(Base64.decodeBase64(fileContent),"");
reqEntity.addPart("fileSize",new StringBody("1234"));
reqEntity.addPart("fileName",new StringBody("xyz.txt"));
reqEntity.addPart("fileType",new StringBody("text/plain"));
reqEntity.addPart("SEQ_NUM",new StringBody("23432"));
HttpPost httppost = new HttpPost(endpointUrl);
httppost.setEntity(reqEntity.build());
Future<HttpResponse> future = httpAsyncClient.execute(httppost, null);
httpAsyncClient.close();
以下是为此实施添加到项目中的 Pom 依赖项:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpasyncclient</artifactId>
<version>4.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.3.6</version>
</dependency>
现在,我被阻止在抛出以下异常的地方:
java.util.concurrent.ExecutionException:java.lang.UnsupportedOperationException:多部分表单实体未实现#getContent()
我在 Internet 上进行了大量搜索,但几乎找不到有关新 HttpAsyncClient 实现的详细信息。我发现一些用户被问过类似的问题,但没有一个解决方案对我有用。
[HttpAsyncClient 多部分实体不起作用?] 在下面的链接中,用户建议使用 BufferedHttpEntity 包装 MultipartEntity 实例。所以,我更新了代码如下(不确定它是否正确)但仍然是同样的异常。
CloseableHttpAsyncClient httpAsyncClient = HttpAsyncClients.createDefault();
httpAsyncClient.start();
MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create();
reqEntity.addPart("file",new ByteArrayBody(Base64.decodeBase64(fileContent),"");
reqEntity.addPart("fileSize",new StringBody("1234"));
reqEntity.addPart("fileName",new StringBody("xyz.txt"));
reqEntity.addPart("fileType",new StringBody("text/plain"));
reqEntity.addPart("SEQ_NUM",new StringBody("23432"));
HttpPost httppost = new HttpPost(endpointUrl);
httppost.setEntity(new BufferedHttpEntity(reqEntity.build()));
Future<HttpResponse> future = httpAsyncClient.execute(httppost, null);
httpAsyncClient.close();
感谢您帮助确定原因。
参考资料:
【问题讨论】: