Implementation code of Android multithreaded breakpoint continuous download function

principle

In fact, the principle of breakpoint continuation is very simple. Literally, the so-called breakpoint continuation is to download again from the place where it stopped. Breakpoint: where the thread stops. Resume: download again from the stopped location.

Code analysis is: breakpoint: the length of data downloaded by the current thread. Resume: request data from the server after the last thread stop position.

The principle is known, and the function is simple to realize. Whenever the thread stops, the downloaded data length is written into the record file. When downloading again, the downloaded length is read from the record file. And this length is the required breakpoint.

The implementation of continuous transmission is also simple. You can request the server to read data from the specified location by setting network request parameters. To realize these two functions, you only need to use the setrequestproperty method in httpurlconnection

As shown below, request 500 bytes between 500 and 1000 from the server:

The above is only a part of the requirements for continuous transmission. When we get the downloaded data, we also need to write the data to the file. The ordinary file object does not provide the function of writing data from the specified location. At this time, we need to use RandomAccessFile to realize the function of writing data to the file from the specified location.

As shown below, data is written after the 100th byte of the file.

Another method in RandomAccessFile needs to be used when writing data

This method is as like as two peas in OutputStream's write.

The above is the principle of breakpoint continuation.

Multithreaded breakpoint continuation

The multi-threaded breakpoint continuation extends on the single thread breakpoint continuation, and the multi-threaded breakpoint continuation divides the whole file into several parts, each part is downloaded by a thread, and each download thread should realize the breakpoint continuation function. In order to realize the file segmentation function, we need to use another method of httpurlconnection:

When the request is successful, the total length of the file can be obtained through this method.

Download size of each thread = filelength / thread_ NUM

As shown in the following figure, the multi-threaded download model is described:

In the multi-threaded breakpoint continuous download, there is one thing to pay special attention to:

Because the file is divided into multiple parts and downloaded by different threads at the same time, each thread needs to have a breakpoint record and a thread completion state record respectively;

As shown in the figure below:

Only when the download status of all threads is complete can it indicate that the file has been downloaded. There are many ways to realize recording. I use the properties class of JDK to record download parameters.

Breakpoint continuation structure

Through the understanding of the principle, we can quickly design the basic structure diagram of the breakpoint continuation tool class

IDownloadListener.java

This class is a download listening interface

DownloadListener.java

Download parameter entity

This class is the download information configuration class. Each sub thread needs a download entity to configure the download information.

Download task thread

This is the download task class of each download sub thread. The sub thread downloads and configures each thread through the download entity. In the concept of multi breakpoint continuation, stop means pause, and resume means that the thread downloads again from the recorded breakpoint. Therefore, the record file cannot be deleted when the thread is in the stopped state.

Download portal

In fact, there is nothing to say. The comments are complete. Two points need to be noted: 1. When resuming the download: the size of the downloaded file = the location of the last breakpoint of the thread - the starting download location of the thread; 2. In order to ensure the integrity of the downloaded file, you need to download it again as long as the record file does not exist;

Final effect:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>