Principle analysis and example explanation of Android breakpoint continuation
This article is about the content of Android breakpoint continuation, which is introduced in detail in the form of examples.
1、 Principle of breakpoint continuation
In fact, the principle of breakpoint continuation is very simple, that is, the HTTP request is different from the general download.
For example, when the browser requests a document on the server, the request is as follows:
Assume that the server domain name is www.jizhuomi.com COM / Android, the file name is down zip。
get /down. zip http/1.1 accept: image/gif,image/x-xbitmap,image/jpeg,image/pjpeg,application/vnd. ms- excel,application/msword,application/vnd. ms-powerpoint,*/* accept-language: zh-cn accept-encoding: gzip,deflate user-agent: mozilla/4.0 (compatible; msie 5.01; windows nt 5.0) connection: keep-alive
After receiving the request, the server looks for the requested file as required, extracts the file information, and then returns it to the browser. The returned information is as follows:
200 content-length=106786028 accept-ranges=bytes date=mon,30 apr 2001 12:56:11 gmt etag=w/"02ca57e173c11:95b" content-type=application/octet-stream server=microsoft-iis/5.0 last-modified=mon,30 apr 2001 12:56:11 gmt
The so-called breakpoint continuation is to continue downloading from the place where the file has been downloaded. So when the client browser sends it to the web server, you should add an additional message -- where to start.
The following is a "browser" compiled by ourselves to transmit the request information to the web server, starting from 2000070 bytes.
get /down. zip http/1.0 user-agent: netfox range: bytes=2000070- accept: text/html,image/gif,*; q=.2,*/*; q=.2
If you take a closer look, you will find that there is an additional line of range: bytes = 2000070-
This line means to tell the server down The zip file starts from 2000070 bytes, and the previous bytes do not need to be transmitted.
After receiving this request, the server returns the following information:
206 content-length=106786028 content-range=bytes 2000070-106786027/106786028 date=mon,30 apr 2001 12:55:20 gmt etag=w/"02ca57e173c11:95b" content-type=application/octet-stream server=microsoft-iis/5.0 last-modified=mon,30 apr 2001 12:55:20 gmt
Compared with the information returned by the previous server, you will find that a line is added:
content-range=bytes 2000070-106786027/106786028
The return code is also changed to 206 instead of 200.
Knowing the above principles, you can program breakpoint continuation.
2、 Key points of realizing breakpoint continuation in Java
What method is used to submit range: bytes = 2000070 -?
Of course, using the most primitive socket can certainly be completed, but it's too cumbersome. In fact, this function is provided in the net package of Java. The code is as follows:
Java code
The above is the data sorting of Android breakpoint continuous transmission. We will continue to supplement relevant data in the future. Thank you for your support to this site!