默认情况下,HttpURLConnection使用gzip方式获取,要取得长度则要求http请求不要gzip压缩;
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Accept-Encoding", "identity"); // 添加这行代码
conn.connect();
int length = conn.getContentLength();
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Accept-Encoding", "identity"); // 添加这行代码
conn.connect();
int length = conn.getContentLength();