Java supports HTTP breakpoint continuation

Source: Internet
Author: User

Background these two days in the realization of a HTML5 on-line audio and video playback, because the file is stored in the Enterprise Network disk, HTTP unreachable, so you need to use the program to implement the file read and HTTP protocol download.

Java implementation of the file download does not have to say, read the file, through the binary stream of the way to the response write on the line. H5 player calls can also be played, but when I control the progress of the forward and backward, the problem came, incredibly little effect! No fast-forward player or player?

Analysis

First see the player can not get audio and video file length of time, it is natural to think of the content-length property, the background through file.length () to get the file length and set to Content-length (code as follows), The front-end player can display the length of the audio and video, and it can be fast-forward, but when I rewind, still invalid, and the background error.

Response.AddHeader ("Content-length", File.length ());



Replaced an HTTP file for comparison testing, and found that direct HTTP access to the normal fast forward and rewind. Careful analysis of both the request and the response head, found the difference, requests the parameters as shown in the properties, the attribute table name needs to get from the server side of the resource range, the default from the first byte to take, fast forward rewind is actually by specifying this Rangeproperty to determine the starting point you expect.

However, this attribute is on the request header, how does the client know to add this attribute? Keep looking and find out. accept-rangesThis property, the property value, is bytes, which indicates whether to accept a request to obtain a portion of its entity, such as a part of a file. Bytes: Accept, none: Indicates not accepted. Another attribute in the response corresponding to this Content-range, whose table name contains the part of the object that the response consists of. The full response header is as follows:


Solve

Based on the above analysis, we know what to do with the server, first adding accept-rangesto the response header.

Response.setheader ("accept-ranges", "bytes");

Next, determine whether a range attribute exists in the request, that is, whether the specified starting point, if present, jumps directly to the target starting point through the stream's skip, and finally adds Content-range property sheet Name the beginning and end of the current block, the complete code is as follows:

stream = new FileInputStream (file), if (Request.getheader ("Range") = NULL)//client-requested download file block start byte {//bytes from request to get started The requested format is: The start byte of the//range:bytes=[file Block-String Range = Stringutils.substringbetween (Request.getheader ("Range"), "    Bytes= ","-");       Long start = Long.parselong (range); The format of the downloaded file (or block) length//Response is://content-length: [Total file Size]-[Start byte of the downloaded file block of the client request] Response.setheader ("Content-len          Gth ", string.valueof (Filesize-start)); if (start! = 0) {//To set the status//Response format is://http/1.1 206 Partial Content Response.setstatu S (javax.servlet.http.HttpServletResponse.SC_PARTIAL_CONTENT);//206//not downloaded from the beginning,//Response format is://content -range:bytes [The total size of the file block's start byte]-[file-The total size of the 1]/[file] Response.setheader ("content-range", "bytes" + Start + "-" + String.             ValueOf (fileSize-1) + "/" + string.valueof (fileSize));      Stream.skip (start); }} responsebinarystream (response, This.getcontenttype (FilEnameutils.getextension (FileName)), stream); 




Java supports HTTP breakpoint continuation

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.