9

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.

Jens Zalzala
  • 2,546
  • 1
  • 22
  • 28
Lijo Joy
  • 261
  • 2
  • 8
  • Please add some details in your question : what is exactly the problem you're facing ? Is there any errors ? If so, can you post thetacktrace ? We need more details if we want to answer you. – Valentin Rocher Jan 04 '11 at 11:57
  • Actually here i am trying to download a zip file...and it will resume download also..but net result is invalid zip file.. – Lijo Joy Jan 04 '11 at 12:01

2 Answers2

10
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);
}
pevik
  • 4,523
  • 3
  • 33
  • 44
Lijo Joy
  • 261
  • 2
  • 8
  • 1
    I would also add a `connection.setReadTimeout()` and a `connection.setConnectionTimeout()` – Someone Somewhere Jan 21 '12 at 01:28
  • Hello Lijo, 'ISSUE_DOWNLOAD_STATUS.intValue() ' i am not understand about this value please give me the reference for this. – DynamicMind Apr 04 '12 at 09:05
  • 2
    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 + "-"); } – Lijo Joy Apr 12 '12 at 04:51
3

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.

Hannele
  • 9,301
  • 6
  • 48
  • 68
dogan
  • 31
  • 1