如何使用HttpURLConnection获取gZip启用文件的内容长度?

时间:2019-02-14 10:52:10

标签: android gzip

一些标头参数应该有助于检测正确的内容长度。

1 个答案:

答案 0 :(得分:0)

/* Get download file size returned from http server header. */
public static long getDownloadUrlFileSize(String downloadUrl) {
    long ret = 0;
    HttpURLConnection con = null;
    try {
        if (downloadUrl != null && !TextUtils.isEmpty(downloadUrl)) {
            URL url = new URL(downloadUrl);
            con = (HttpURLConnection) url.openConnection();
            con.setRequestProperty("Accept-Encoding", "identity;q=1, *;q=0");
            System.out.println("Length : " + con.getContentLength());
            ret = con.getContentLength();
        }
    } catch (Exception ex) {
        Log.e(TAG_DOWNLOAD_MANAGER, ex.getMessage(), ex);
    } finally {
        if (con != null)
            con.disconnect();
        return ret;

    }
}