Android HttpUrlConnection 斷點(diǎn)下載

  • 斷點(diǎn)下載返回碼為206,而不是200,不同的網(wǎng)站可能有區(qū)別,大家注意一下,斷點(diǎn)請求失敗的返回碼為416
public void download(View view) {
        new Thread(new MyTask()).start();
    }


class MyTask implements Runnable {

        @Override
        public void run() {
            HttpURLConnection connection = null;
            try {
                URL url = new URL(MainActivity.this.url);
                connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.setDoInput(true);
                connection.setRequestProperty("Range", "bytes=" + start + "-" + end);
                contentLength = connection.getContentLength();
                totalTime = contentLength / 1024*1024;
                int responseCode = connection.getResponseCode();

                File file = new File(path, "aaa.apk");
                if (!file.exists()) {
                    file.createNewFile();
                }
                RandomAccessFile raf = new RandomAccessFile(file, "rw");
                InputStream is = null;
                if (responseCode == 206) {

                    raf.seek(start);

                    is = connection.getInputStream();
                    int len;
                    byte[] buffer = new byte[1024];
                    while ((len = is.read(buffer)) != -1) {
                        raf.write(buffer, 0, len);
                    }

                    start = end + 1;
                    end += 1024 * 1024;   // 每次下載1M

                    if (raf != null) {
                        raf.close();
                    }

                    if (is != null) {
                        is.close();
                    }

                    result += "文件" + times + "下載成功" + ":" + start + "---" + end + "\n";
                    Message message = Message.obtain();
                    message.what = 0;
                    message.obj = result;
                    mHandler.sendMessage(message);
                }

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                connection.disconnect();
            }
        }
    }
private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                if (times > totalTime) {
                    Message msg1 = Message.obtain();
                    msg1.what = 1;
                    mHandler.sendMessage(msg1);
                } else {
                    new Thread(new MyTask()).start();
                    times += 1;
                }
                tv.setText(result);
            } else if (msg.what == 1) {
                tv.setText(result);
            }
        }


    };
String url = "http://ftp-apk.pconline.com.cn/ef19af4e28462271af1117efaf868bc2/pub/download/201010/renshengrili_v4.0.04.05.apk";
    String path = Environment.getExternalStorageDirectory().getPath();
    String result = "";
    long times = 0;
    long start = 0;
    long end = 1024 * 1024;
    private int contentLength;
    private int totalTime;

    private TextView tv;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容