断点续传的简单实现

断点续传主要是使用http协议中range的属性来取得资源的部分内容,由于一般服务是不对外直接提供url访问的,一般都是通过id,在servlet中输出byte[]来实现,所以要想实现断点续传一般要自己实现一个服务端。

一个简单实现:
服务端:主要是分析了range属性,利用RandomAccessFile读取内容输出字节流
Java代码   收藏代码
  1. public class Download extends HttpServlet {  
  2.   
  3.     @Override  
  4.     protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
  5.             throws ServletException, IOException {  
  6.   
  7.         int id = Integer.parseInt(req.getParameter("id"));  
  8.         String filename ;  
  9.         if (id == 1)  
  10.             filename = "E:/JDK_API_1_5_zh_CN.CHM";  
  11.         else if (id == 2)  
  12.             filename = "E;/JDK_API_1_6_zh_CN.CHM";  
  13.         else if (id == 3)  
  14.             filename = "E:/apache-tomcat-5.5.26/webapps/ROOT/tomcat.gif";  
  15.         else  
  16.             filename = "c.rar";  
  17.   
  18.         RandomAccessFile raFile = new RandomAccessFile(filename, "r");  
  19.           
  20.         String range = req.getHeader("RANGE");  
  21.         int start=0, end=0;  
  22.         if(null!=range && range.startsWith("bytes=")){  
  23.             String[] values =range.split("=")[1].split("-");  
  24.             start = Integer.parseInt(values[0]);  
  25.             end = Integer.parseInt(values[1]);  
  26.         }  
  27.         int requestSize=0;  
  28.         if(end!=0 && end > start){  
  29.             requestSize = end - start + 1;  
  30.             resp.addHeader("content-length"""+(requestSize));  
  31.         } else {  
  32.             requestSize = Integer.MAX_VALUE;  
  33.         }  
  34.           
  35.         byte[] buffer = new byte[4096];  
  36.           
  37.         resp.setContentType("application/x-download");  
  38.         resp.addHeader("Content-Disposition""attachment;filename=a.chm");  
  39.         ServletOutputStream os = resp.getOutputStream();  
  40.           
  41.         int needSize = requestSize;  
  42.           
  43.         raFile.seek(start);  
  44.         while(needSize > 0){  
  45.             int len = raFile.read(buffer);  
  46.             if(needSize < buffer.length){  
  47.                 os.write(buffer,0,needSize);  
  48.             } else {  
  49.                 os.write(buffer,0,len);  
  50.                 if(len < buffer.length){  
  51.                     break;  
  52.                 }  
  53.             }  
  54.             needSize -= buffer.length;  
  55.         }  
  56.               
  57.         raFile.close();  
  58.         os.close();  
  59.     }  
  60. }  


客户端:分两次取得部分内容,输出到RandomAccessFile中
Java代码   收藏代码
  1. public static void main(String[] args) throws MalformedURLException, FileNotFoundException {  
  2.         test1(0,1000);  
  3.         test1(1001,0);  
  4.     }  
  5.       
  6.     public static void test1(int start, int end) throws MalformedURLException, FileNotFoundException{  
  7.           
  8.         String endpoint = "http://localhost:8080/Hello/download?id=1";  
  9.           
  10.         RandomAccessFile raFile = new RandomAccessFile("E:/temp/test.chm""rw");  
  11.           
  12.         URL url = new URL(endpoint);  
  13.         try {  
  14.             HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
  15.             //conn.addRequestProperty("location", "/tomcat.gif");  
  16.             conn.setRequestProperty("Content-Type","text/html; charset=UTF-8");   
  17.             conn.setRequestProperty("RANGE","bytes="+start+"-"+end);   
  18.               
  19.             conn.connect();  
  20.             System.out.println(conn.getResponseCode());  
  21.             System.out.println(conn.getContentLength());  
  22.             System.out.println(conn.getContentType());  
  23.               
  24.             InputStream ins = (InputStream)conn.getContent();  
  25.               
  26.             raFile.seek(start);  
  27.               
  28.             byte[] buffer = new byte[4096];  
  29.             int len = -1;  
  30.             while((len = ins.read(buffer))!=-1){  
  31.                 raFile.write(buffer,0,len);  
  32.             }  
  33.             raFile.close();  
  34.             conn.disconnect();  
  35.         } catch (IOException e) {  
  36.             e.printStackTrace();  
  37.         }  
  38.     } 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值