美文网首页
Android HttpUrlConnection 断点下载

Android HttpUrlConnection 断点下载

作者: 耳_总 | 来源:发表于2017-05-16 09:33 被阅读58次
    • 断点下载返回码为206,而不是200,不同的网站可能有区别,大家注意一下,断点请求失败的返回码为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;
    

    相关文章

      网友评论

          本文标题:Android HttpUrlConnection 断点下载

          本文链接:https://www.haomeiwen.com/subject/mkuztxtx.html