2.2以上的版本下载网络资源不完整没法更新。HttpURLConnection.getContentLength()获取的size跟下载下来的file的legth不等。html
缘由是:HttpURLConnection跟服务交互采用了"gzip"压缩。因此下载的fileLegth > HttpURLConnection.getContentLength().java
参考api:android
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
urlConnection.setRequestProperty("Accept-Encoding", "identity");
2.2以上的版本默认都是采用压缩优化,文件的完整性验证最好仍是用md5。网络