tags:

views:

135

answers:

1

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();
+4  A: 

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

naikus
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
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="http://stackoverflow.com/questions/3428102/how-to-resume-an-interrupted-download-part-2">part 2</a> of this question.
gregS
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
@gregS Ouch, that sucks!
naikus