How the breakpoint continues to pass

Source: Internet
Author: User

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 the browser requests a file on the server, the request is as follows: Assume that the server domain name is wwww.sjtu.edu.cn and the filename is down.zip. Get/down.zip http/1.1 accept:image/gif, Image/x-xbitmap, Image/jpeg, Image/pjpeg, Application/vnd.ms-excel, Applicati On/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 information, and returns it to the browser, returning the following information:

content-length=106786028 accept-ranges=bytes Date=mon, APR 2001 12:56:11 GMT etag=w/"02ca57e173c11:95b" Conten T-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, the file has been downloaded from the place to continue to download.  So add a piece of information when the client browser passes to the WEB server-where to start.  The following is a "browser" made by yourself to pass 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, Image/jpeg, *; Q=.2, */*; q=.2

A closer look will find a line range:bytes=2000070-this line means to tell the server down.zip this file from 2000070 bytes, the previous byte does not pass. After the server receives this request, the following information is returned: 206 content-length=106786028 content-range=bytes 2000070-106786027/106786028 Date=mon, 200 APR  1 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 with the information returned by the previous server, you will find an additional line: The code returned by Content-range=bytes 2000070-106786027/106786028 is changed to 206, not 200.

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

Java enterprise-Class generic rights security framework source SPRINGMVC MyBatis or Hibernate+ehcache Shiro Druid Bootstrap HTML5

"Java Framework source code download"

The key points for Java to implement the breakpoint continuous transmission
  1. (1) What method to implement the submission range:bytes=2000070-. Of course, with the most original Socket is certainly able to complete, but that is too much trouble, in fact, Java's 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 for the continuation of the breakpoint Httpconnection.setrequestproperty ("RANGE", "bytes=2000070"); Get input stream InputStream input = Httpconnection.getinputstream ();

    The byte stream removed 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 implementation is still very simple it. The next thing to do is how to save the obtained stream to the file.

  2. The method used to save the file.  I am using the Randaccessfile class in the IO package.  The operation is fairly straightforward, assuming that the file is saved from 2000070, the code is as follows: randomaccess osavedfile = new Randomaccessfile ("Down.zip", "RW");  Long NPos = 2000070;  Position the file pointer to NPos position osavedfile.seek (NPos);  Byte[] B = new byte[1024];  int nread;  Reads a byte stream from the input stream and writes to the file while ((Nread=input.read (b,0,1024)) > 0) {osavedfile.write (b,0,nread); }

How the breakpoint continues to pass

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.