Implement resumable Data Transfer Based on the Principle of resumable Data Transfer

Source: Internet
Author: User
Tags microsoft iis

Are you worried about resumable upload? I will explain the details of resumable data transfer. Principle Help you solve this problem.

(1) Principle of resumable Data Transfer

In fact, the principle of resumable upload is very simple, that is, the HTTP request is different from the general download. For example, when a 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 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 searches for the requested file, 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-Microsoft IIS/5.0

Last-modified = Mon, 30 Apr 2001 12:56:11 GMT

The so-called resumable upload, that is, the file has Download To continue downloading. Therefore, when the client browser sends a message to the Web server, you need to add one more entry-where to start.

The following is a self-compiled "Browser" to transmit the request information to the Web server, starting from 2000070 bytes.

GET/down.zip HTTP/1.0

User-Agent: netfox

Value Range: bytes = 2000070-

Accept: text/html, image/GIF, image/JPEG, *; q =. 2, */*; q =. 2

Take a closer look and you will find that there is an additional line of "range: bytes00002000070-0000", which means that the file down.zip of the server starts to be uploaded from 2000070 bytes, and the previous bytes do not need to be uploaded.

After receiving the 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-Microsoft IIS/5.0

Last-modified = Mon, 30 Apr 2001 12:55:20 GMT

Compared with the information returned by the preceding server, a new row is added:

Content-range = bytes 2000070-106786027/106786028

The Returned Code is also changed to 206, instead of 200.

With the above principles, you can program resumable data transfer.

(2) Key Points for implementing resumable data transfer in Java

(1) method used to implement the submit range: bytes = 2000070 -.

Of course, it is certainly possible to use the most primitive socket, but it is too time-consuming. In fact, this function is provided in the Java net package. 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 start position of resumable upload

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

// Obtain the input stream

Inputstream input = httpconnection. getinputstream ();

The byte stream that starts from 2000070 in the down.zip file of the input stream. As you can see, it is quite easy to implement resumable upload in Java. The next thing to do is how to save the obtained stream to the file.

Method used to save files

Use the randaccessfile class in the IO package.

The operation is quite simple. Assume that the file is saved from 2000070. The Code is as follows:

Randomaccess osavedfile = new randomaccessfile ("down.zip", "RW ");

Long NPOs = 2000070;

// Locate the file pointer to the NPOs position

Osavedfile. Seek (NPOs );

Byte [] B = new byte [1024];

Int nread;

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.