首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将httpclient响应作为流获得

如何将httpclient响应作为流获得
EN

Stack Overflow用户
提问于 2018-03-18 10:43:35
回答 3查看 13.2K关注 0票数 6

我正在使用httpclient 4.5.5作为响应,我希望获得高达1GB的大型文件。但看上去

CloseableHttpResponse response = httpClient.execute(httpGet); HttpEntity entity = response.getEntity();

这将返回整个响应,因此在内存中拥有整个响应并不好。有什么方法可以作为流得到响应吗?

EN

回答 3

Stack Overflow用户

发布于 2018-03-20 11:10:46

ApacheHTTP4.0版(以及)支持传入和传出HttpClient消息的完整内容流。使用HttpEntity访问下面的内容输入流

代码语言:javascript
复制
CloseableHttpClient client = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://myhost/tons-of-stuff");
try (CloseableHttpResponse response1 = client.execute(httpGet)) {
    final HttpEntity entity = response1.getEntity();
    if (entity != null) {
        try (InputStream inputStream = entity.getContent()) {
            // do something useful with the stream
        }
    }
}
票数 14
EN

Stack Overflow用户

发布于 2018-03-18 12:41:07

您需要Apache异步客户端。

HttpAsyncClient是Apache HttpClient的ASYNC版本。Apache在内存中构造整个响应,而使用HttpAsyncClient,您可以定义一个Handler (使用者)来处理接收数据时的响应。

代码语言:javascript
复制
https://hc.apache.org/httpcomponents-asyncclient-4.1.x/index.html

下面是他们官方示例代码中的一个示例

代码语言:javascript
复制
package org.apache.http.examples.nio.client;

import java.io.IOException;
import java.nio.CharBuffer;
import java.util.concurrent.Future;

import org.apache.http.HttpResponse;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.nio.IOControl;
import org.apache.http.nio.client.methods.AsyncCharConsumer;
import org.apache.http.nio.client.methods.HttpAsyncMethods;
import org.apache.http.protocol.HttpContext;

/**
 * This example demonstrates an asynchronous HTTP request / response exchange with
 * a full content streaming.
 */
public class AsyncClientHttpExchangeStreaming {

    public static void main(final String[] args) throws Exception {
        CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
        try {
            httpclient.start();
            Future<Boolean> future = httpclient.execute(
                    HttpAsyncMethods.createGet("http://httpbin.org/"),
                    new MyResponseConsumer(), null);
            Boolean result = future.get();
            if (result != null && result.booleanValue()) {
                System.out.println("Request successfully executed");
            } else {
                System.out.println("Request failed");
            }
            System.out.println("Shutting down");
        } finally {
            httpclient.close();
        }
        System.out.println("Done");
    }

    static class MyResponseConsumer extends AsyncCharConsumer<Boolean> {

        @Override
        protected void onResponseReceived(final HttpResponse response) {
        }

        @Override
        protected void onCharReceived(final CharBuffer buf, final IOControl ioctrl) throws IOException {
            while (buf.hasRemaining()) {
                System.out.print(buf.get());
            }
        }

        @Override
        protected void releaseResources() {
        }

        @Override
        protected Boolean buildResult(final HttpContext context) {
            return Boolean.TRUE;
        }

    }

}
票数 4
EN

Stack Overflow用户

发布于 2018-03-18 12:16:26

使用HttpURLConnection而不是httpClient

代码语言:javascript
复制
final HttpURLConnection conn = (HttpURLConnection)url.openConnection();
final int bufferSize = 1024 * 1024;
conn.setChunkedStreamingMode(bufferSize);
final OutputStream out = conn.getOutputStream();
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49346948

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档