package com.wutianfa.experience;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.concurrent.FutureCallback;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.util.Args;
import org.junit.Test;
import java.io.*;
import java.util.concurrent.CountDownLatch;
/**
* Created by Samuel on 2019/1/28 21:16
*/
public class ChunkTest {
private static final String uri = "https://vscode.cdn.azure.cn/stable/61122f88f0bf01e2ac16bdb9e1bc4571755f5bd8" +
"/VSCode-win32-x64-1.30.2.zip";
private static String path = "E:\\test.zip";
@Test
public void test() {
CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault();
httpClient.start();
final CountDownLatch latch = new CountDownLatch(1);
final HttpGet request = new HttpGet(uri);
//设置代理
//HttpHost proxy = new HttpHost("10.10.10.10", 8080, "http");
//RequestConfig requestConfig = RequestConfig.custom()
// .setProxy(proxy).build();
//request.setConfig(requestConfig);
System.out.println(" caller thread id is : " + Thread.currentThread().getId());
httpClient.execute(request, new FutureCallback<HttpResponse>() {
public void completed(final HttpResponse response) {
latch.countDown();
System.out.println(" callback thread id is : " + Thread.currentThread().getId());
System.out.println(request.getRequestLine() + "->" + response.getStatusLine());
BufferedOutputStream bout = null;
OutputStream outputStream = null;
InputStream isstream = null;
try {
HttpEntity entity = response.getEntity();
Args.notNull(entity, "Entity");
isstream = entity.getContent();
Args.notNull(isstream, "isstream");
Args.check(entity.getContentLength() <= 2147483647L, "HTTP entity too large to be " +
"buffered in memory");
byte[] tmp = new byte[4096];
outputStream = new FileOutputStream(path);
bout = new BufferedOutputStream(outputStream);
int l;
while ((l = isstream.read(tmp)) != -1) {
bout.write(tmp, 0, l);
bout.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
assert bout != null;
bout.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
outputStream.close();
isstream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void failed(final Exception ex) {
latch.countDown();
System.out.println(request.getRequestLine() + "->" + ex);
System.out.println(" callback thread id is : " + Thread.currentThread().getId());
}
public void cancelled() {
latch.countDown();
System.out.println(request.getRequestLine() + " cancelled");
System.out.println(" callback thread id is : " + Thread.currentThread().getId());
}
});
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
HttpAsyncClient
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 通过之前的两篇我们能在本地搭建单一和集群两种方式的dubbo服务,这篇我们来看 springmvc+spring+...