RandomAccessFile threadfile = new RandomAccessFile("QQWubiSetup
(Android application source code realizes breakpoint continuous download through HTTP protocol on Android platform
We are writing a multithreaded breakpoint download application for andorid's HTTP protocol. Downloading HTTP files directly using single thread is a very simple thing for us. So, what functions do multithreaded breakpoints need?
1. Multi thread download,
2. Support breakpoints.
Benefits of using multithreading: using multithreaded download will improve the speed of file download. The process of downloading files through multiple threads is as follows:
(1) First obtain the length of the downloaded file, and then set the length of the local file.
HttpURLConnection. getContentLength();// Gets the length of the downloaded file
RandomAccessFile file = new RandomAccessFile(“QQWubiSetup.exe”,“rwd”);
file. setLength(filesize);// Set the length of the local file
(2) Calculate the data length and download location of each thread according to the file length and the number of threads.
For example, if the file length is 6m and the number of threads is 3, the data downloaded by each thread is 2m,
For example, 10m size, Download with three threads,
The data length downloaded by the thread (10% 3 = = 0 × 10 / 3:10 / 3 + 1), the download length of the first and second threads is 4m, and the download length of the third thread is 2m
Download start position: thread ID * data length downloaded by each thread =?
Download end location: (thread ID + 1) * data length downloaded by each thread - 1 =?
(3) Use the range header field of HTTP to specify where each thread starts downloading from files and where to download from.
For example: specify to download from 2m location of the file and download to this location (4m-1byte)
The code is as follows: httpurlconnection setRequestProperty("Range", "bytes=2097152-4194303");
(4) Save the file and use the RandomAccessFile class to specify where each thread writes data from the local file.
RandomAccessFile threadfile = new RandomAccessFile("QQWubiSetup)