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。網絡