package httpclient; import com.ning.http.client.AsyncHttpProvider; import com.ning.http.client.Response; import java.io.*; import java.util.concurrent.Future; /** * User: zhenghui * Date: 13-8-26 * Time: 上午10:41 */ public class AsyncHttpClientTest { public static void main(String[] args) throws Exception{ com.ning.http.client.AsyncHttpClient asyncHttpClient = new com.ning.http.client.AsyncHttpClient(); Future<Response> f = asyncHttpClient.prepareGet("http://gju2.alicdn.com/bao/uploaded/i1/T13yOwFldXXXb1upjX.jpg").execute(); Response response = f.get(); BufferedInputStream bufferedInputStream = new BufferedInputStream(response.getResponseBodyAsStream()); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("d:/test.jpg")); int i = 0; //缓冲区的大小 byte[] buffer = new byte[512]; while(true) { if(bufferedInputStream.available() < 512) { while(i != -1) { i = bufferedInputStream.read(); bufferedOutputStream.write(i); } break; } else { //当文件的大小还大于512字节时 bufferedInputStream.read(buffer); bufferedOutputStream.write(buffer); } } //强制清空缓冲区的内容 bufferedOutputStream.flush(); //流的关闭 bufferedInputStream.close(); bufferedOutputStream.close(); asyncHttpClient.close(); } }
顺便注意一下
AHC(AsyncHttpClient)文件描述符泄漏
相关推荐
AsyncHttpClient-master
标签:aether-connector-asynchttpclient-1.11.jar.zip,aether,connector,asynchttpclient,1.11,jar.zip包下载,依赖包
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用