gpt4 book ai didi

apache-httpcomponents - 使用多部分实体实现 HttpComponent 的 HttpAsyncClient

转载 作者:行者123 更新时间:2023-12-05 07:55:57 29 4
gpt4 key购买 nike

我需要一些帮助,澄清如何将 HttpAsyncClient 与多部分实体一起使用。基本上,我正在尝试使用 HttpAsyncClient 将文件 (byte[]) 的内容上传为 MultiPart Body。我如何使用它的示例:

    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: Multipart form entity does not implement #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();

如果您能帮助确定原因,我将不胜感激。

引用资料:

http://mail-archives.apache.org/mod_mbox/hc-httpclient-users/201311.mbox/%3c13034392.132.1384195516775.JavaMail.Hal@Harold-Rosenbergs-MacBook-Pro.local%3e

http://mail-archives.apache.org/mod_mbox/hc-httpclient-users/201206.mbox/%3C1339240012.2405.12.camel@ubuntu%3E

最佳答案

MultipartFormEntity 不能异步使用。其中一个选项是将实体写出到 ByteArrayOutputStream 并从中创建一个 NByteArrayEntity。

    File file = new File(filePath);
ByteArrayBody body;
body = new ByteArrayBody(FileUtils.readFileToByteArray(file), file.getName());

HttpEntity mEntity = MultipartEntityBuilder.create()
.setBoundary("-------------" + boundary)
.addPart("file", body)
.build();

ByteArrayOutputStream baoStream = new ByteArrayOutputStream();
mEntity.writeTo(baoStream);

HttpEntity nByteEntity = new NByteArrayEntity(baoStream.toByteArray(), ContentType.MULTIPART_FORM_DATA);
httppost.setEntity(nByteEntity );

关于apache-httpcomponents - 使用多部分实体实现 HttpComponent 的 HttpAsyncClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29104778/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com