Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果下载量很大,InputStream.close速度非常慢_Java - Fatal编程技术网

Java 如果下载量很大,InputStream.close速度非常慢

Java 如果下载量很大,InputStream.close速度非常慢,java,Java,我有以下代码: HttpGet downloadRequest = new HttpGet("url?size=10000000"); HttpResponse response = this.httpClient.execute(downloadRequest); HttpEntity entity = response.getEntity(); InputStream inputStream = entity.getContent(); while ((length = (

我有以下代码:

HttpGet downloadRequest = new HttpGet("url?size=10000000");
HttpResponse response = this.httpClient.execute(downloadRequest);
HttpEntity entity = response.getEntity();        
InputStream inputStream = entity.getContent();

while ((length = (int)inputStream.skip((long)BUFFER_SIZE)) != 0) {

 ...
}

inputStream.close();
这是一个从url下载数据的非常基本的程序。我的问题是,如果它完成了所有数据的下载,那么它工作正常;但是,如果我试图中途强制停止下载进度(例如,在1秒后中断while循环)

关闭inputStream需要很长时间。下载的数据越多,关闭的时间就越长


当我关闭inputStream时,我是否做错了什么?是否有一种方法可以立即关闭inputStream,而不管“inputStream”中还有多少数据?

我也有同样的问题,帮助我的是在关闭
inputStream之前和发出
EntityUtils.consume(entity)
之前发出
httpget.abort()


httpget.reset()
应该具有相同的效果。

我在处理Post请求时遇到了相同的问题。我追踪并发现了导致HttpResponse挂起的原因是它仍在传输大量数据。 修复方法是从一开始就中止请求。类似下面这样的东西

HttpPost httpPost = new HttpPost("request/endpoint");
httpPost.abort();
// do the rest down here

您确定这与InputStream本身有关吗?我无法在本地复制这种行为。这可能与此有关吗?“重要提示:请注意,所有实体实现都必须确保在调用InputStream.close()方法后正确释放所有分配的资源。”请看这里:也许您应该调用EntityUtils.consume(HttpEntity)?我尝试了EntityUtils.consume(HttpEntity)之前,得到了与close inputStream相同的问题
HttpPost httpPost = new HttpPost("request/endpoint");
httpPost.abort();
// do the rest down here