[JAVA file multi-threaded download]

This operation of file download often occurs. When the file is large, if the multi-threaded breakpoint download is not used, then when the download is half-way wrong or suspended, it needs to be downloaded from the beginning. It is unnecessary. Yes, because we can continue the download from the place where the last error occurred, just like Thunderbolt, when we download the file, it is impossible to download the file from the beginning of the entire file when an error occurs in the middle. If you usually download carefully, you will find There is a temporary file in the process of Thunder download. This temporary file stores which byte of the file you have downloaded, the download time and other information so that you can resume the download from the breakpoint after you pause or make an error.



 

 

 

 

Multi-threaded breakpoint download: As the name implies, it is implemented with multi-threading. When a third-party factor (power failure, network disconnection, etc.) interrupts the download, the next download can continue to the place where the last download was made.

 

1. The size of the file to be downloaded can be obtained through getContentLength, so that a file of the same size can be created on the local machine for download.

int fileLength = connection.getContentLength();

2. Since it is multi-threaded, it is necessary to equally distribute the location to be downloaded to each thread.

for(int i = 0; i < threadCount; i ++) {
    int startThread = i * blockSize;
    int endThread = (i + 1) * blockSize - 1;
    if( i == blockSize - 1) endThread = fileLength -1;
    new DownloadThread(i, startThread, endThread).start();
                    
}
 

3. When starting each thread to download, the request header needs the Range parameter, and the value is bytes:xxx-xxx something. For example, "Range:0-10100" means the location to be downloaded is from 0 to 10100.

connection.setRequestProperty("Range", "bytes:"+startThred+"-" + endThread);

4. Then use RandomAccessFile to write data to the local file each time.

while((length = inputStream.read(buffer)) != -1) {
    randomAccessFile.write(buffer, 0, length);
}

5. Of course, each time you download, you need to record how much this thread has downloaded, so that when you breakpoint, you can download from the next download place.

total += length;
int currentThreadPostion = startThred + total;
RandomAccessFile randomAccessFile2 = new RandomAccessFile(file, "rwd");
randomAccessFile2.write(String.valueOf(currentThreadPostion).getBytes());
randomAccessFile2.close();

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326181452&siteId=291194637