为什么httpclient老是卡住
来源:互联网 发布:路由器上面的访客网络 编辑:程序博客网 时间:2024/06/09 21:06
最近在修改快门下载功能时,发现同一文件只能下载成功2次,此后再点下载就没反应了。
我的httpclient是如下方式创建的,使用了连接池。
private static CloseableHttpClient httpclient = HttpClientBuilder.create().build();
我的调用方式如下:
public static byte[] executeQuery(String url, String chartSet) throws ClientProtocolException, IOException { if (StringUtil.isBlank(chartSet)) { chartSet = "UTF-8"; } byte[] bytes = null; HttpGet httpget = new HttpGet(url); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); if (entity != null) {InputStream instream = entity.getContent();bytes = FileUtil.readAsByteArray(instream); } return bytes;}
尝试几次后发现,下载成功的次数和maxConnPerRoute相同,该参数默认值就是2,并且可以采用以下方式修改:
private static CloseableHttpClient httpclient = HttpClientBuilder.create().setMaxConPerRoute(5).build();
于是,我怀疑是连接没有释放。但我又不可能调用httpclient.close(),否则连接池就失效了。
最终发现,错误原因是我既没有释放instream,也没有释放httpget(http://www.oschina.net/question/925814_131284)。
那么,可以通过释放instream或httpget的方法来解决。
方案一:释放instream
public static byte[] executeQuery(String url, String chartSet) throws ClientProtocolException, IOException { if (StringUtil.isBlank(chartSet)) { chartSet = "UTF-8"; } byte[] bytes = null; HttpGet httpget = new HttpGet(url); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); if (entity != null) {InputStream instream = entity.getContent();bytes = FileUtil.readAsByteArray(instream); // Closing the input stream will trigger connection release instream.close(); }return bytes;}
方案二:释放httpget
public static byte[] executeQuery(String url, String chartSet) throws ClientProtocolException, IOException { if (StringUtil.isBlank(chartSet)) { chartSet = "UTF-8"; } byte[] bytes = null; HttpGet httpget = new HttpGet(url); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); if (entity != null) {InputStream instream = entity.getContent();bytes = FileUtil.readAsByteArray(instream); }// release linkshttpget.releaseConnection();return bytes;}
0 0
- 为什么httpclient老是卡住
- Linux为什么卡住了?
- 为什么老是IOException
- 为什么李莫愁老是杀人?
- 为什么我们需要老是加班?
- google为什么老是访问不了
- 为什么老是要审核呢
- 为什么运行结果老是错误
- 搜狗搜微信链接为什么老是失效?
- 告诉你中国队为什么老是输
- 烦,为什么老是得不到老婆的尊重!
- 为什么闪讯老是断啊???
- Altium Designer 为什么老是出现绿色警告?
- 显示屏为什么老是要做老化测试
- 学历,你为什么老是挡住我的发展?!
- 打开网页,IE7为什么老是弹出“是否允许下载文件”
- 为什么我们在做软件项目时,老是反反复复,反反复复?
- 请高人指教为什么jquery语句老是提示错误呢?
- 代理模式
- 日本称俄罗斯6架轰炸机绕日本列岛飞行一周
- 3-搭建OMAPL138的Linux开发环境
- C和C++中的new , malloc 的区别
- <算法>递归实现阶乘运算
- 为什么httpclient老是卡住
- linux tar 命令 解压文件到指定目录
- 遍历字典方法:HASH_ITER
- jQuery 请指出'$'和'$.fn'的区别?或者说出'$.fn'的用途。
- 泛型,Quick Sort 的C/C++两种实现方式
- POJ 2993:Emag eht htiw Em Pleh:模拟题
- opencv实践程序6——简单(otsu)的背景差分法
- 加措活佛:幸福在心里
- monkeyrunner学习笔记四(录制和回放)