zoukankan      html  css  js  c++  java
  • 使文件下载的自定义连接支持 FlashGet 的断点续传多线程链接下载! JSP/Servlet 实现!

     <%
    /*
      文件名可存为: Download.jsp
      HTTP 协议的请求与响应的会话过程可通过使用 FlashGet 下载 Http:// 连接的过程监视:
      蓝色部分为: 客户端请求
      紫色部分为: 服务器端响应
      如图:
      http://blog.csdn.net/images/blog_csdn_net/playyuer/30110/o_FlashGet.gif
      或参阅,后面的 FlashGet 会话列表:
      
    */
      //你可以使用你服务器上的文件及其路径
      String s = "I://SetupRes//Sun//j2re-1_4_2_05-windows-i586-p.exe";
      //String s = "e://tree.mdb";

      //经测试 RandomAccessFile 也可以实现,有兴趣可将注释去掉,并注释掉 FileInputStream 版本的语句
      //java.io.RandomAccessFile raf = new java.io.RandomAccessFile(s,"r");

      java.io.File f = new java.io.File(s);
      java.io.FileInputStream fis = new java.io.FileInputStream(f);

      response.reset();

      response.setHeader("Server", "playyuer@Microshaoft.com");

      //告诉客户端允许断点续传多线程连接下载
      //响应的格式是:
      //Accept-Ranges: bytes
      response.setHeader("Accept-Ranges", "bytes");

      long p = 0;
      long l = 0;
      //l = raf.length();
      l = f.length();

      //如果是第一次下,还没有断点续传,状态是默认的 200,无需显式设置
      //响应的格式是:
      //HTTP/1.1 200 OK

      if (request.getHeader("Range") != null) //客户端请求的下载的文件块的开始字节
      {
       //如果是下载文件的范围而不是全部,向客户端声明支持并开始文件块下载
       //要设置状态
       //响应的格式是:
       //HTTP/1.1 206 Partial Content
       response.setStatus(javax.servlet.http.HttpServletResponse.SC_PARTIAL_CONTENT);//206

       //从请求中得到开始的字节
       //请求的格式是:
       //Range: bytes=[文件块的开始字节]-
       p = Long.parseLong(request.getHeader("Range").replaceAll("bytes=","").replaceAll("-",""));
      }

      //下载的文件(或块)长度
      //响应的格式是:
      //Content-Length: [文件的总大小] - [客户端请求的下载的文件块的开始字节]
      response.setHeader("Content-Length", new Long(l - p).toString());

      if (p != 0)
      {
       //不是从最开始下载,
       //响应的格式是:
       //Content-Range: bytes [文件块的开始字节]-[文件的总大小 - 1]/[文件的总大小]
       response.setHeader("Content-Range","bytes " + new Long(p).toString() + "-" + new Long(l -1).toString() + "/" + new Long(l).toString());
      }

      //response.setHeader("Connection", "Close"); //如果有此句话不能用 IE 直接下载

      //使客户端直接下载
      //响应的格式是:
      //Content-Type: application/octet-stream
      response.setContentType("application/octet-stream");

      //为客户端下载指定默认的下载文件名称
      //响应的格式是:
      //Content-Disposition: attachment;filename="[文件名]"
      //response.setHeader("Content-Disposition", "attachment;filename=/"" + s.substring(s.lastIndexOf("//") + 1) + "/""); //经测试 RandomAccessFile 也可以实现,有兴趣可将注释去掉,并注释掉 FileInputStream 版本的语句
      response.setHeader("Content-Disposition", "attachment;filename=/"" + f.getName() + "/"");

      //raf.seek(p);
      fis.skip(p);

      byte[] b = new byte[1024];
      int i;


      //while ( (i = raf.read(b)) != -1 ) //经测试 RandomAccessFile 也可以实现,有兴趣可将注释去掉,并注释掉 FileInputStream 版本的语句
      while ( (i = fis.read(b)) != -1 )
      {
       response.getOutputStream().write(b,0,i);
      }
      //raf.close();//经测试 RandomAccessFile 也可以实现,有兴趣可将注释去掉,并注释掉 FileInputStream 版本的语句
      fis.close();
    %>

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

    在 FlashGet 中
    一组第一次直接下载,还没有断点续传 HTTP 会话过程:
    客户端请求:
    Mon Aug 02 05:46:36 2004 正在连接 download2.flashfxp.com:80
    Mon Aug 02 05:46:36 2004 正在连接 download2.flashfxp.com [IP=66.98.228.125:80]
    Mon Aug 02 05:46:37 2004 已连接.
    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

    服务器端响应:
    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-410a964e"
    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 会话过程:
    客户端请求:
    Mon Aug 02 05:27:05 2004 正在连接 download2.flashfxp.com:80
    Mon Aug 02 05:27:05 2004 正在连接 download2.flashfxp.com [IP=66.98.228.125:80]
    Mon Aug 02 05:27:05 2004 已连接.
    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

    服务器端响应:
    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-410a964e"
    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

  • 相关阅读:
    CentOS Linux 7 安装教程
    计算机网络概念
    计算机操作系统概念简介
    基础-计算机及操作系统和应用程序的概念
    Spring mvc注解说明
    kaptcha Spring 整合
    Spring常用注解
    Spring bean的自动装配属性
    HQL(Hibernate Query language)语言
    JDBC、Hibernate、Java类型对照表
  • 原文地址:https://www.cnblogs.com/Microshaoft/p/2485776.html
Copyright © 2011-2022 走看看