Search code examples
javahttpdownloadrangerandom-access

Need help using java threads to download file parts


I am trying to download a file from a server in a user specified number of parts (n). So there is a file of x bytes divided into n parts with each part downloading a piece of the whole file at the same time. I am using threads to implement this, but I have not worked with http before and do not really understand how downloading a file really works. I have read up on it and it seems "Range" needs to be used, but I do not know how to download different parts and being able to append them without corrupting the data.


Solution

  • (Since it's a homework assignment I will only give you a hint)

    Appending to a single file will not help you at all, since this will mess up the data. You have two alternatives:

    • Download from each thread to a separate temporary file and then merge the temporary files in the right order to create the final file. This is probably easier to conceive, but a rather ugly and inefficient approach.

    • Do not stick to the usual stream-style semantics - use random access (1, 2) to write data from each thread straight to the right location within the output file.