Implementation of continuous download based on breakpoint
Demand background
When downloading dynamically created files, you want the browser to display the download progress
Dynamically created files are expected to be downloaded in segments
HTTP breakpoint continuation message
To realize HTTP breakpoint continuation, you must simply understand the following messages.
Accept ranges tells the client (browser..) The server side supports breakpoint continuation and server side return
The range client tells the server to download resources from the specified location / range (here, the number of bytes) and the client sends
The content range server tells the client the data information of the response, and the byte position of this part in the whole return body is returned by the server
Etag resource ID must not be returned from the server
The last updated time of the last modified resource must not be returned from the server
Range format for range
Indicates the range of 0-499 bytes: range: bytes = 0-499
Indicates the range of the last 500 bytes: range: bytes = - 500
Indicates the start to end range of 500 bytes: range: bytes = 500-
Represents the first and last bytes: range: bytes = 0-0, - 1
Indicates that several ranges are specified at the same time: range: bytes = 500-600601-999
Data format of content range
Content range: bytes 0-499 / 22036: indicates that the data resource in the range of 0-499 bytes is returned, with a total of 22036 bytes
principle
The client initiates a request to set the range to specify the number of start bytes or end bytes. If it starts from 0, it may not be set.
The server checks that the range header of the client parses the number of start bytes and end bytes and returns the message header. Accept ranges indicates that it supports breakpoint continuation. The content range records the location information of the stream written to the client, and then writes the stream to the client.
The server can use Etag last modified to mark whether the resource has been modified. Do some verification work. If the verification fails, an error will be returned, which is not a required item.
Java implementation
For example, when downloading the Google browser, you can see the download progress, pause the download and resume the download operation, or you can set the range test for segmented download.
The implementation of the above article based on the breakpoint continuous download principle is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.