Skip to content

Initiate download from certain byte

I would like to implement a distributed download manager on android that initiates downloads at certain byte lengths. So this way portions of files can be downloaded instead of only from the beginning to the end of the http request.

Nothing innovative about it, I just don’t know how to do it. (It’s not bitorrent either)

In java http apache library, how would I get the length of the file from the server, and then initiate a download from the middle of the file to the end?

Answer

With the HttpClient:

if (offset > 0) {
    String startFrom = "bytes= " + offset + "-";
    if (expected >= 0) {
        startFrom += expected;
    }
    mHttpGet.addHeader("Range", startFrom);
    expectedStatusCode = HttpStatus.SC_PARTIAL_CONTENT;
}

where mHttpGet is an instance of HttpGet, offset is the number of bytes you had already downloaded and expected is the size of the file.

about the Range field of the HttpGet header:

Request only part of an entity. Bytes are numbered from 0.