【问题标题】:Implementing HttpComponent's HttpAsyncClient with Multipart Entity使用多部分实体实现 HttpComponent 的 HttpAsyncClient
【发布时间】: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();

感谢您帮助确定原因。

参考资料:

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

【问题讨论】:

    标签: apache-httpcomponents


    【解决方案1】:

    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 );
    

    【讨论】:

    • 我试过了,但还是没有运气。好吧,我竭尽全力寻找解决方案,但一无所获。因此,现在我将代码更改为使用 Normal HttpClient 参考。
    猜你喜欢
    • 1970-01-01
    • 2022-01-19
    • 2011-01-01
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多