Android Development notes — about file breakpoint continuation

What is breakpoint continuation?

Breakpoint continuation of client software means that when downloading or uploading, Tasks will be downloaded or uploaded (a file or a compressed package) is artificially divided into several parts. Each part is uploaded or downloaded by one thread. In case of network failure, you can continue to upload and download the unfinished part from the part that has been uploaded or downloaded, instead of uploading and downloading from the head. Users can save time, traffic and speed.

I wrote a small demo. Let's take a look at the implementation effect diagram:

The principle of breakpoint continuation?

In fact, breakpoint continuation is not so mysterious. It just uses the difference of request header in HTTP transport protocol to carry out breakpoint continuation.

The following is the HTTP request header I intercepted with firebug when downloading MP3 from Baidu Music:

This is the information returned to me by Baidu music server:

This is a general download request. The server will return the content length of the file we requested to download. The success status code is 200.

The breakpoint continuation is to continue the last transmission from the file location we downloaded before. As the name suggests, we need to tell the server where we downloaded last time, so we need to include an additional message in our HTTP request header, which is the secret of breakpoint continuation.

This message is: (range is the request header attribute, X is where to start, y is where to end)

For example:

Range: used for client-side to server-side requests. This field can be used to specify the size of a section of downloaded files and its unit. Typical formats are as follows: range: bytes = 0-499 download the content in the range of 0-499 bytes range: bytes = 500-999 download the content in the range of 500-999 bytes range: bytes = - 500 download the content in the last 500 bytes range: bytes = 500 download the content from the beginning of the 500th byte to the end of the file

These are the points that need attention in http. Next, let's talk about several classes that need attention in Java

For this HTTP request, we have encapsulated a series of classes under the Java net package, and we can use it directly.

This is Java net. A method under the urlconnection class, which is used to set the HTTP request header. The key corresponds to the HTTP request header property, and the value corresponds to the value of the request header property.

This is Java io. RandomAccessFile class. The instance of this class supports reading and writing random access files. There is a very important seek (long POS) method in this class,

This method can be written at the next position of the length you set, such as seek (599), so the next writing position of the system is the 600 position of the file.

Since it is a breakpoint continuation, we must also record the breakpoint location, so the database is also essential here.

Specific implementation:

Well, after understanding the above knowledge points, you can start writing programs.

The following is a small demo I wrote, which uses a single thread to realize the continuous transmission of breakpoints. The comments are very complete.

This is the structure diagram of Demo: (I really can't draw pictures, just make do with it)

Main class (activity):

Background service class (service)

Download task class:

Database help class:

Database table interface class:

Database table interface implementation class:

Entity class: (file, thread)

File entity class:

Thread entity class:

In fact, the principle of multi-threaded breakpoint continuation is the same. Divide a file into different areas, and then use a thread in each area to perform the download task, and then merge the files. Let's go to a demo another day.

Author: Balla_ Rabbit origin: http://www.cnblogs.com/lichenwei/ The copyright of this article belongs to the author and the blog park. Reprint is welcome, but this statement must be retained without the consent of the author, and the original link must be given in an obvious position on the article page. I'm reading the children's shoes on my blog. I think you have extraordinary bearing. There's a king's spirit in your conversation. You must do something in the future! Next to the word "recommendation", you can easily point it. It's accurate. I won't accept a penny; If you're not sure, you can come back to me!

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
分享
二维码
< <上一篇
下一篇>>