2013-05-16 41 views
0

我将使用Apache HTTP客户端从Android上的url获取文件流,我想检查流的标题来确定是否应该加载整个文件,以避免网络资源的无谓成本。
现在,我用下面的代码获得进入数据流,并从它刚刚看了前几个字节:当我刚刚读取前几个字节使用Apache HTTP客户端,Java,Android

response = mClient.execute(method, localcontext); 
final HttpEntity entity = response.getEntity(); 
if (entity != null) { 
    InputStream instream = entity.getContent(); 
    byte[] ints = new byte[2]; 
    instream.read(ints); 
} 

我想知道如果整个文件将在上述过程中被加载。我应该明确关闭这个流。怎么样?
谢谢〜

回答

0

更好的方法是使用HEAD方法。它只是返回http标头,并没有实际的内容或主体。

例(from official site

HeadMethod head = new HeadMethod("http://jakarta.apache.org"); 
     // execute the method and handle any error responses. 
     ... 
     // Retrieve all the headers. 
     Header[] headers = head.getResponseHeaders(); 

一旦你的头,你可以检查它们,然后作出相应的另一个GET/POST调用实际下载的内容。

+0

对不起,但是当我说流的标头时,我实际上是指实体内容的前面部分:P – Bolton

+0

我只想知道在阅读了我想要的数据后是否应该关闭流。 – Bolton

+0

是的。它是一条小溪。所以数据不断流动,当你关闭它时,它就会停止。 – Santosh