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();
Josh Lee
  • 171,072
  • 38
  • 269
  • 275
gregS
  • 2,580
  • 5
  • 28
  • 33
  • 1
    Perhaps use something other than Yahoo web site server :) Does not Android have `scp` capabilities? What is the big picture here? – Hamish Grubijan Aug 05 '10 at 03:16

2 Answers2

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

Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
naikus
  • 24,302
  • 4
  • 42
  • 43
  • 4
    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. – Jörn Horstmann Aug 05 '10 at 09:08
  • 1
    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 part 2 of this question. – gregS Aug 07 '10 at 15:58
  • 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." – gregS Aug 11 '10 at 11:11
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();
           }
       }
    } 
}    
Mark Amabile
  • 152
  • 6