use of org.apache.hc.core5.concurrent.FutureCallback in project weicoder by wdcode.
the class HttpAsyncClient method download.
/**
* 下载文件
*
* @param url get提交地址
* @param callback 回调结果
*/
public static void download(String url, final CallbackVoid<byte[]> callback) {
// 声明HttpGet对象
HttpGet get = null;
try {
// 获得HttpGet对象
get = new HttpGet(url);
get.addHeader(new BasicHeader(HttpConstants.CONTENT_TYPE_KEY, HttpConstants.CONTENT_TYPE_VAL));
// 执行get
CLIENT.execute(SimpleRequestBuilder.copy(get).build(), new FutureCallback<SimpleHttpResponse>() {
@Override
public void failed(Exception ex) {
LOG.error(ex);
}
@Override
public void completed(SimpleHttpResponse result) {
if (callback != null)
callback.callback(result.getBodyBytes());
}
@Override
public void cancelled() {
}
});
} catch (Exception e) {
LOG.error(e);
}
}
Aggregations