how to resume an interrupted download
Asked Answered
G

2

23

I'm trying to download a large file from my Yahoo! web site server which apparently is setup (not by me) to disconnect downloads if they are not completed within 100 seconds. The file is small enough to usually successfully transfer. On the occasions when the data rate is slow and the download gets disconnected, is there a way to resume the URLConnection at the file offset where the disconnection occurred? Here's the code:

// Setup connection.
URL url = new URL(strUrl[0]);
URLConnection cx = url.openConnection();
cx.connect();

// Setup streams and buffers.
int lengthFile = cx.getContentLength();
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(strUrl[1]);
byte data[] = new byte[1024];

// Download file.
for (total=0; (count=input.read(data, 0, 1024)) != -1; total+=count) {
    publishProgress((int)(total*100/lengthFile));
    output.write(data, 0, count);
    Log.d("AsyncDownloadFile", "bytes: " + total);
}

// Close streams.
output.flush();
output.close();
input.close();
Genera answered 5/8, 2010 at 3:12 Comment(1)
Perhaps use something other than Yahoo web site server :) Does not Android have scp capabilities? What is the big picture here?Allocation
S
31

Try using a "Range" request header:

// Open connection to URL.
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

// Specify what portion of file to download.
connection.setRequestProperty("Range", "bytes=" + downloaded + "-");
// here "downloaded" is the data length already previously downloaded.

// Connect to server.
connection.connect();

Having done that, you can seek at a given point (just before the length of your download data, say X) and start writing the newly downloaded data there. Be sure to use the same value X for the range header.

Details about 14.35.2 Range Retrieval Requests

More details and source code can be found here

Sadyesaechao answered 5/8, 2010 at 3:33 Comment(3)
Nice example, you should probably check that the response code form the server is 206 "Partial Content", to make sure that it supported the byte range request.Ormiston
naikus, thanks for the help. I'm still reading the w3 document and hoping to find the reason that the Yahoo! server appears to always transmit from the beginning of the file rather than from the beginning of the specified range. Further attempts are described in <a href="#3428602 2</a> of this question.Genera
I received a reply from Yahoo! tech support saying that the Yahoo! servers do not support byte range requests: "Yahoo! Web Hosting does not support Accept-range header since we work with a pool of servers and each request potentially reaches a different server. You will see connection=[close] in the response header indicating this."Genera
O
2

Here's an example code that you can use:

import java.io.*;
import java.net.*;


public class HttpUrlDownload {

    public static void main(String[] args) {
       String strUrl = "http://VRSDLSCEN001:80//DLS//lib//clics.jar";
       String DESTINATION_PATH = "clics.jar";

       int count = 0;
       while (true) {
          count++;
          if (download(strUrl, DESTINATION_PATH) == true || count > 20) {
             break;
          }         
       }
    }

    public static boolean download(String strUrl, String DESTINATION_PATH) {
        BufferedInputStream in = null;
        FileOutputStream fos = null;
        BufferedOutputStream bout = null;
        URLConnection connection = null;

        int downloaded = 0;

        try {
           System.out.println("mark ... download start");
           URL url = new URL(strUrl);

           connection = url.openConnection();

           File file=new File(DESTINATION_PATH);
           if(file.exists()){
               downloaded = (int) file.length();
           }
           if (downloaded == 0) {
               connection.connect();
           }
           else {
               connection.setRequestProperty("Range", "bytes=" + downloaded + "-");
               connection.connect();
           }

           try {
               in = new BufferedInputStream(connection.getInputStream());
           } catch (IOException e) {
               int responseCode = 0;
               try {
                   responseCode = ((HttpURLConnection)connection).getResponseCode();
               } catch (IOException e1) {
                 e1.printStackTrace();
               }

               if (responseCode == 416) {         
                   return true;
               } else {
                   e.printStackTrace();
                   return false;
               }
           }

           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);
           }

           in.close();
           bout.flush();
           bout.close();
           return false;

       } catch (IOException e) {
           e.printStackTrace();
           return false;
       } finally {
           if (in != null) {
               try {
                   in.close();
               } catch (IOException e) {
               }
           }
           if (fos != null) {
               try {
                   fos.close();
               } catch (IOException e) {
               }
           }
           if (bout != null) {
               try {
                   bout.close();
               } catch (IOException e) {
               }
           }

           if (connection != null) {
              ((HttpURLConnection)connection).disconnect();
           }
       }
    } 
}    
Oaten answered 21/8, 2017 at 8:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.