Enable the custom connection for file download to support resumable multi-thread link download of flashget! JSP/Servlet implementation!

Source: Internet
Author: User
<%
/*
The file name can be saved as: Download. jsp
For HTTP requests and responses, you can use flashget to download http: // connection process monitoring:
The blue part is: client request
Purple: Server Response
:
Http://blog.csdn.net/images/blog_csdn_net/playyuer/30110/o_FlashGet.gif
For more information, see the following flashget session List:

*/
// You can use the file on your server and its path
String S = "I: // setupres // sun // j2re-1_4_2_05-windows-i586-p.exe ";
// String S = "E: // tree. mdb ";

// Randomaccessfile can also be implemented after testing. If you are interested, remove the annotation and comment out the fileinputstream statement.
// Java. Io. randomaccessfile RAF = new java. Io. randomaccessfile (S, "R ");

Java. Io. File F = new java. Io. file (s );
Java. Io. fileinputstream FCM = new java. Io. fileinputstream (f );

Response. Reset ();

Response. setheader ("server", "playyuer@Microshaoft.com ");

// Tell the client to allow multi‑thread download of resumable Data Transfer
// The response format is:
// Accept-ranges: bytes
Response. setheader ("Accept-ranges", "bytes ");

Long p = 0;
Long L = 0;
// L = Raf. Length ();
L = f. Length ();

// If resumable data transfer is not enabled for the first time, the status is 200 by default, and no explicit settings are required.
// The response format is:
// HTTP/1.1 200 OK

If (request. getheader ("range ")! = NULL) // start byte of the file block requested by the client
{
// If the range of the downloaded file is not all, declare to the client to support and start file block download
// Set the status
// The response format is:
// HTTP/1.1 206 partial content
Response. setstatus (javax. servlet. http. httpservletresponse. SC _partial_content); // 206

// Get the starting byte from the request
// The request format is:
// Range: bytes = [start byte of the file block]-
P = long. parselong (request. getheader ("range"). replaceall ("bytes =", ""). replaceall ("-",""));
}

// The length of the downloaded file (or block)
// The response format is:
// Content-Length: [total file size]-[start byte of the downloaded file block requested by the client]
Response. setheader ("Content-Length", new long (L-P). tostring ());

If (P! = 0)
{
// Not the first download,
// The response format is:
// Content-range: bytes [start byte of the file block]-[total file size-1]/[total file size]
Response. setheader ("content-range", "bytes" + new long (P ). tostring () + "-" + new long (L-1 ). tostring () + "/" + new long (l ). tostring ());
}

// Response. setheader ("connection", "close"); // if this sentence exists, ie cannot be used for direct download.

// Make the client download directly
// The response format is:
// Content-Type: Application/octet-stream
Response. setcontenttype ("application/octet-stream ");

// Specify the default download file name for the client download
// The response format is:
// Content-Disposition: attachment; filename = "[file name]"
// Response. setheader ("content-disposition", "attachment; filename =/" "+ S. substring (S. lastindexof ("//") + 1) + "/"); // The randomaccessfile can also be tested. If you are interested, remove the annotation and comment out the fileinputstream statement.
Response. setheader ("content-disposition", "attachment; filename =/" "+ F. getname () + "/"");

// Raf. Seek (P );
FS. Skip (P );

Byte [] B = new byte [1024];
Int I;

// While (I = Raf. Read (B ))! =-1) // tested randomaccessfile can also be implemented. If you are interested, remove the annotation and comment out the fileinputstream statement.
While (I = Fi. Read (B ))! =-1)
{
Response. getoutputstream (). Write (B, 0, I );
}
// Raf. Close (); // The randomaccessfile can also be tested. If you are interested, remove the annotation and comment out the fileinputstream statement.
FCM. Close ();
%>

========================================================== ========================================

In flashget
A set of first direct downloads without resumable http session processes:
client requests:
Mon Aug 02 05:46:36 2004 connection download2.flashfxp.com: 80
Mon Aug 02 05:46:36 2004 connection download2.flashfxp.com [IP = 66.98.228.125: 80]
Mon Aug 02 05:46:37 2004 connected.
Mon Aug 02 05:46:37 2004 get/zip/flashfxp_30_setup.exe HTTP/1.1
Mon Aug 02 05:46:37 2004 HOST: download2.flashfxp.com
Mon Aug 02 05:46:37 2004 accept: */*
Mon Aug 02 05:46:37 2004 Referer: http://playyuer.microshaoft.com
Mon Aug 02 05:46:37 2004 User-Agent: Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)
Mon Aug 02 05:46:37 2004 Pragma: No-Cache
Mon Aug 02 05:46:37 2004 cache-control: No-Cache
Mon Aug 02 05:46:37 2004 authorization: basic mgrhetpjy2zfzg93bmxvywq =
Mon Aug 02 05:46:37 2004 connection: Close

Server Response:
Mon Aug 02 05:46:37 2004 HTTP/1.1 200 OK
Mon Aug 02 05:46:37 2004 date: Sun, 01 Aug 2004 21:46:29 GMT
Mon Aug 02 05:46:37 2004 server: Apache/1.3.27 (UNIX) (red-hat/Linux) mod_fastcgi/2.2.12 mod_gzip/1.3.19.1a mod_jk/1.2.0 mod_perl/1.26 PHP/4.3.3 FrontPage/5.0.2 mod_ssl/2.8.12 OpenSSL/0.9.6b
Mon Aug 02 05:46:37 2004 last-modified: Fri, 30 Jul 2004 18:41:18 GMT
Mon Aug 02 05:46:37 2004 etag: "4f80fa-1ecf20-450a964e"
Mon Aug 02 05:46:37 2004 accept-ranges: bytes
Mon Aug 02 05:46:37 2004 Content-Length: 2019104
Mon Aug 02 05:46:37 2004 connection: Close
Mon Aug 02 05:46:37 2004 Content-Type: Application/octet-stream

 

HTTP session process for resumable data transfer:
client request:
Mon Aug 02 05:27:05 2004 is connecting download2.flashfxp.com: 80
Mon Aug 02 05:27:05 2004 is connecting download2.flashfxp.com [IP = 66.98.228.125: 80]
Mon Aug 02 05:27:05 2004 connected.
Mon Aug 02 05:27:05 2004 get/zip/flashfxp_30_setup.exe HTTP/1.1
Mon Aug 02 05:27:05 2004 HOST: download2.flashfxp.com
Mon Aug 02 05:27:05 2004 accept: */*
Mon Aug 02 05:27:05 2004 Referer: http://playyuer.microshaoft.com
Mon Aug 02 05:27:05 2004 User-Agent: Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)
Mon Aug 02 05:27:05 2004 range: bytes = 191621-
Mon Aug 02 05:27:05 2004 Pragma: No-Cache
Mon Aug 02 05:27:05 2004 cache-control: no-Cache
Mon Aug 02 05:27:05 2004 authorization: Basic mgrhetpjy2zfzg93bmxvywq =
Mon Aug 02 05:27:05 2004 connection: Close

Server Response:
Mon Aug 02 05:27:06 2004 HTTP/1.1 206 partial content
Mon Aug 02 05:27:06 2004 date: Sun, 01 Aug 2004 21:26:57 GMT
Mon Aug 02 05:27:06 2004 server: Apache/1.3.27 (UNIX) (red-hat/Linux) mod_fastcgi/2.2.12 mod_gzip/1.3.19.1a mod_jk/1.2.0 mod_perl/1.26 PHP/4.3.3 FrontPage/5.0.2 mod_ssl/2.8.12 OpenSSL/0.9.6b
Mon Aug 02 05:27:06 2004 last-modified: Fri, 30 Jul 2004 18:41:18 GMT
Mon Aug 02 05:27:06 2004 etag: "4f80fa-1ecf20-450a964e"
Mon Aug 02 05:27:06 2004 accept-ranges: bytes
Mon Aug 02 05:27:06 2004 Content-Length: 1827483
Mon Aug 02 05:27:06 2004 content-range: bytes 191621-2019103/2019104
Mon Aug 02 05:27:06 2004 connection: Close
Mon Aug 02 05:27:06 2004 Content-Type: Application/octet-stream

Related Article

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.