下载网络文件HttpURLConnection.getConte
2017-10-16 本文已影响57人
泅渡者
AsyncTask+HttpURLConnection进行文件下载
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.connect();
int length = conn.getContentLength();
当测试时会报错。经过Debug才发现是 length=-1,后来几番搜获后发现在默认情况下,HttpURLConnection 使用 gzip方式获取。
我们只需要设置
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn .setRequestProperty("Accept-Encoding", "identity");
conn.connect();
int length = conn.getContentLength();
InputStream is = conn.getInputStream();
改变接收文件的格式便可。