Resume Download not working in android

夙愿已清 提交于 2019-12-18 13:14:08

问题


This code for resuming download is not working properly in Android, although it works fine in a Java application. Here I am trying to download a zip file, and it will resume the download, but the net result is an invalid zip file.

 BufferedInputStream in = null;
        FileOutputStream fos = null;
        BufferedOutputStream bout=null;

        try {
            downloaded=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=" + downloaded + "-");
            connection.connect();
            size=connection.getContentLength();
            Dialog.setMax(size);
             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;
                 System.out.println(downloaded);
                 onProgressUpdate((int)(downloaded*100/size));
            }

            succes=true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
                bout.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

Thanks.


回答1:


HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int buf = 1024;

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 = new FileOutputStream(DESTINATION_PATH, downloaded == 0 ? false : true);
bout = new BufferedOutputStream(fos, buf);
byte[] data = new byte[buf];

while ((int x = in.read(data, 0, buf)) >= 0) {
    bout.write(data, 0, x);
    downloaded += x;
    progressBar.setProgress(downloaded);
}



回答2:


Your zip file is corrupted because you think that the stream resumes from the range byte that you specified. It actually streams from the beginning again, and so you have a file bigger than the original. Long story short, your server does not support the range property.



来源:https://stackoverflow.com/questions/4593275/resume-download-not-working-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!