2

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?

damian
  • 1,419
  • 1
  • 22
  • 41
CQM
  • 42,592
  • 75
  • 224
  • 366

2 Answers2

1

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.
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • 1
    are there any caveats to doing this? what kinds of server/protocols are not able to initiate downloads from certain bytes. Also, how do I get the byte length of a request, because I need to know that before I make assumptions about the offset – CQM Jul 26 '13 at 15:54
  • For instace if you store a partial file, offset will be te number of bytes stored – Blackbelt Jul 26 '13 at 16:43
1

As Kyle commented on your question, you can check out this answer

The basic principle is that along with your (http) request for the file you add the header

 "Range", "bytes="+(file_length_to_start_from) +"-" + (file_length_to_stop)

or you can omit the file_length_to_stop (in the linked answer example) to get the rest of the file from that point on.

Community
  • 1
  • 1
Makis Tsantekidis
  • 2,698
  • 23
  • 38