This blog can be considered as the use of Java implementation of HTTP multi-threaded download of the article re-interpretation.
First, from a macro perspective,
Java implementation Multi-threaded download the implementation of this feature consists of the following parts:
1 Create multiple threads to download part of the file separately.
2 files that are downloaded by multiple threads (also in memory) are written to a file on the hard disk.
3 Breakpoint Continuation
Get/path/filename http/1.0
Host:www.server.com:80
Accept: */*
User-agent:generaldownloadapplication
Connection:close
This is a request for requests
For example, Host,accept is the field part of request requests (for more information about Ruquest, please refer to HTTP in-depth HTTP request
First we say 1th:
If a file has 1000 bytes
In the fields section of the request, add a range
The range:bytes=500-represents a total of 500 bytes of 500-999 bytes of the file read.
The range:bytes=500-599 represents a total of 100 bytes of 500-599 bytes of the file read.
So the first problem is solved.
There's actually a small problem here.
If a file size is 123kB
Download with 10 threads
What to do? 123 divided by 10 apart.
We can get the first 9 threads to download 12KB of data
The 10th thread download 15KB not OK!!
How do you write this part of the code?
The second problem involves writing data at a given position in a file.
There is this one class: Randomaccessfile
Random randomly
Access accesses
This class everyone guess, all know is random access to the file.
Let's look at some of its methods.
public native void Seek (long pos) throws IOException * @param b The data. * @param off the start offset in the data. * @param len the number of bytes to write. * @exception IOException If an I/O error occurs.public void write (byte b[], int off, int len) throws IOException
The first method is positioning, POS is a location of the file.
The second way is: Read the instructions yourself.
For an example of Randomaccessfile, refer to Java randomaccessfile usage
How to solve the third problem?
There is a class in the system: Dltask, which represents a download task (one download task will download the file via multiple threads)
The system will have a separate thread to monitor the download task, and every 3 seconds, the download task (Java Class) is serialized on the hard disk.
This stops the download even when the download is incomplete. The next time, we can still get the original download task by deserializing. (For the knowledge of serialization, see Java Basic Learning Summary--java object serialization and deserialization
At the same time, the process that specifically monitors the download task can also count the download rate.
Resources
Http://www.cnblogs.com/rqnet/p/4319587.html
http://calmness.iteye.com/blog/220075
Http://www.cnblogs.com/yin-jingyu/archive/2011/08/01/2123548.html
http://blog.csdn.net/akon_vm/article/details/7429245
Http://www.cnblogs.com/xdp-gacl/p/3777987.html
Java implementation multi-threaded download