Android2.2以上的版本HttpURLConnection.getContentLength()获取的size跟下载下来的file的legth不相等

2.2以上的版本下载网络资源不完整无法更新。HttpURLConnection.getContentLength()获取的size跟下载下来的file的legth不等。

原因是:HttpURLConnection跟服务交互采用了"gzip"压缩。所以下载的fileLegth > HttpURLConnection.getContentLength().

参考api:


By default, this implementation of HttpURLConnection requests that servers use gzip compression. Since getContentLength() returns the number of bytes transmitted, you cannot use that method to predict how many bytes can be read from getInputStream(). Instead, read that stream until it is exhausted: whenread() returns -1. 

     api上也不推荐是用该方法来验证文件的完整性。
     gzip压缩方式是可以取消,在http request的head中设置如下参数:
    
urlConnection.setRequestProperty("Accept-Encoding", "identity"); 

 2.2以上的版本默认都是采用压缩优化,文件的完整性验证最好还是用md5。