Introduction to the principle of realizing breakpoint continuation under Linux

Source: Internet
Author: User

Breakpoint continuation is a combination of local storage and networked storage technology, mainly used to solve the problem of video loss when the network failure. DVS usually does not have the video storage function, but must by the backend NVR to realize the video storage, therefore for the network stability request is very high, the network connection failure, loses the packet serious, jitter and so on various factors all may cause the video data the loss. A breakpoint continuation supports the transfer of data from the point where the file was last interrupted, rather than from the beginning of the file. This is the definition of the continuation of the breakpoint. The system can be broken down by default, but we rarely know his principle, below to see the introduction of small series.

The principle of continuous transmission of breakpoints

In fact, the principle of the continuation of the breakpoint is very simple, is the Http request and the general download is different.

For example, when a browser requests a text on the server, the request is made as follows:

Suppose the server domain name is wwww.sjtu.edu.cn and 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 the server receives the request, it looks for the requested file, extracts the file's information, and returns it to the browser, returning the following information:

200

content-length=106786028

Accept-ranges=bytes

Date=mon, APR 2001 12:56:11 GMT

etag=w/"02ca57e173c11:95b"

Content-type=application/octet-stream

server=microsoft-iis/5.0

Last-modified=mon, APR 2001 12:56:11 GMT

The so-called breakpoint continuation, that is, from the file has been downloaded from the place to continue to download. So when the client browser passes to the WEB server, add a message-where to start.

The following is a "browser" of your own to pass the request information to the WEB server, which requires starting from 2000070 bytes.

Get/down.zip http/1.0

User-agent:netfox

range:bytes=2000070-

Accept:text/html, Image/gif, Image/jpeg, *; Q=.2, */*; q=.2

Take a closer look and you'll find one more line range:bytes=2000070-

This line is meant to tell the server that the file is down.zip from 2000070 bytes and that the preceding byte is not to be transmitted.

After the server receives this request, the information returned is as follows:

206

content-length=106786028

Content-range=bytes 2000070-106786027/106786028

Date=mon, APR 2001 12:55:20 GMT

etag=w/"02ca57e173c11:95b"

Content-type=application/octet-stream

server=microsoft-iis/5.0

Last-modified=mon, APR 2001 12:55:20 GMT

Compared to the information returned by the previous server, you will see an added line:

Content-range=bytes 2000070-106786027/106786028

The returned code is also changed to 206, and is no longer 200.

Knowing the above principles, you can proceed to the programming of the continuation of the breakpoint.

Key points in Java implementation of breakpoint continuation

(1) What method to implement the submission of range:bytes=2000070-.

Of course, with the most original Socket is certainly able to complete, but it is too much trouble, in fact, Java NET package provides this functionality. The code is as follows:

URL url = new URL ("Http://www.sjtu.edu.cn/down.zip");

HttpURLConnection httpconnection = (httpurlconnection) url.openconnection ();

Set User-agent

Httpconnection.setrequestproperty ("User-agent", "Netfox");

Set the starting position of a breakpoint continuation

Httpconnection.setrequestproperty ("RANGE", "bytes=2000070");

Get input stream

InputStream input = Httpconnection.getinputstream ();

The byte stream that is fetched from the input stream is the byte stream that the Down.zip file starts with 2000070. You see, in fact, the continuation of the breakpoint in Java to achieve a very simple bar. The next thing to do is how to save the obtained stream into the file.

The method used to save the file.

I am using the Randaccessfile class in the IO package.

The operation is fairly simple, assuming that the file is saved from 2000070 and the code is as follows:

Randomaccess osavedfile = new Randomaccessfile ("Down.zip", "RW");

Long NPOs = 2000070;

Locate file pointer to NPOs location

Osavedfile.seek (NPOs);

Byte[] B = new byte[1024];

int nread;

Reads a byte stream from the input stream and writes it to a file

while ((Nread=input.read (b,0,1024)) "0"

{

Osavedfile.write (B,0,nread);

}

The above is the principle of the extension of the breakpoint, know these principles, I believe we can be programmed it, this technology in the current computer technology is also not what, hit the home should have noticed that we download is a breakpoint continue to pass it

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.