我们可以在Android下载之前拆分文件吗?

时间:2016-05-11 09:21:22

标签: android

是否有可能在android下载之前拆分文件,就像我们可以通过cURL命令在linux中一样?

curl --range 200000000-399999999 -o ubuntu-iso.part2 http://mirror.pnl.gov/releases/15.04/ubuntu-15.04-desktop-amd64.iso
curl --range 400000000-599999999 -o ubuntu-iso.part3 http://mirror.pnl.gov/releases/15.04/ubuntu-15.04-desktop-amd64.iso
curl --range 600000000-799999999 -o ubuntu-iso.part4 http://mirror.pnl.gov/releases/15.04/ubuntu-15.04-desktop-amd64.iso

1 个答案:

答案 0 :(得分:0)

获得文件大小后,将其分成大致相等大小的块,并为每个块生成下载线程。完成所有操作后,按正确的顺序编写文件块。

  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        if(ISSUE_DOWNLOAD_STATUS.intValue()==ECMConstant.ECM_DOWNLOADING){
            File file=new File(DESTINATION_PATH);
            if(file.exists()){
                 downloaded = (int) file.length();
                 connection.setRequestProperty("Range", "bytes="+(file.length())+"-");
            }
        }else{
            connection.setRequestProperty("Range", "bytes=" + downloaded + "-");
        }
        connection.setDoInput(true);
        connection.setDoOutput(true);
        progressBar.setMax(connection.getContentLength());
         in = new BufferedInputStream(connection.getInputStream());
         fos=(downloaded==0)? new FileOutputStream(DESTINATION_PATH): new FileOutputStream(DESTINATION_PATH,true);
         bout = new BufferedOutputStream(fos, 1024);
        byte[] data = new byte[1024];
        int x = 0;
        while ((x = in.read(data, 0, 1024)) >= 0) {
            bout.write(data, 0, x);
             downloaded += x;
             progressBar.setProgress(downloaded);
        }

更多anwers:     https://stackoverflow.com/a/6323043/4014891