goback add

Android文件下载之断点续传

3929 点击·0 回帖
灯火互联
楼主


HttpURLConnection con = null;
long mOffset = 0;
InputStream is = null;
URL url = null;
url = new URL("xxxxxxxxxxx url");
  
    con = (HttpURLConnection) url.openConnection();
    con.setRequestMethod("GET");
    con.setDoInput(true);
    //断点续传
    con.setRequestProperty("RANGE", "bytes="+ mOffset +"-");
    con.setReadTimeout(COM.UPGRADE_TIME_OUT);
    con.setConnectTimeout(COM.UPGRADE_TIME_OUT);
is = con.getInputStream();
int len = con.getContentLength();

断点续传的关键在 con.setRequestProperty("RANGE", "bytes="+ mOffset +"-");
当mOffset =0 时获取的文件大小会是原文件大小,若mOffset增加,则返回的文件大小为剩下的字节数。


喜欢0 评分0