Resume Download not working in android

后端 未结 2 1580
不思量自难忘°
不思量自难忘° 2020-12-31 13:58

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

相关标签:
2条回答
  • 2020-12-31 14:42
    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);
    }
    
    0 讨论(0)
  • 2020-12-31 15:03

    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.

    0 讨论(0)
提交回复
热议问题