视频库之断点下载

本文介绍了一种基于HTTP协议的断点续传技术实现方案,包括服务端处理Range请求和客户端分段下载的详细步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

一个简单实现:
服务端:主要是分析了range属性,利用RandomAccessFile读取内容输出字节流

    public class Download extends HttpServlet {  
      
        @Override  
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
                throws ServletException, IOException {  
      
            int id = Integer.parseInt(req.getParameter("id"));  
            String filename ;  
            if (id == 1)  
                filename = "E:/JDK_API_1_5_zh_CN.CHM";  
            else if (id == 2)  
                filename = "E;/JDK_API_1_6_zh_CN.CHM";  
            else if (id == 3)  
                filename = "E:/apache-tomcat-5.5.26/webapps/ROOT/tomcat.gif";  
            else  
                filename = "c.rar";  
      
            RandomAccessFile raFile = new RandomAccessFile(filename, "r");  
              
            String range = req.getHeader("RANGE");  
            int start=0, end=0;  
            if(null!=range && range.startsWith("bytes=")){  
                String[] values =range.split("=")[1].split("-");  
                start = Integer.parseInt(values[0]);  
                end = Integer.parseInt(values[1]);  
            }  
            int requestSize=0;  
            if(end!=0 && end > start){  
                requestSize = end - start + 1;  
                resp.addHeader("content-length", ""+(requestSize));  
            } else {  
                requestSize = Integer.MAX_VALUE;  
            }  
              
            byte[] buffer = new byte[4096];  
              
            resp.setContentType("application/x-download");  
            resp.addHeader("Content-Disposition", "attachment;filename=a.chm");  
            ServletOutputStream os = resp.getOutputStream();  
              
            int needSize = requestSize;  
              
            raFile.seek(start);  
            while(needSize > 0){  
                int len = raFile.read(buffer);  
                if(needSize < buffer.length){  
                    os.write(buffer,0,needSize);  
                } else {  
                    os.write(buffer,0,len);  
                    if(len < buffer.length){  
                        break;  
                    }  
                }  
                needSize -= buffer.length;  
            }  
                  
            raFile.close();  
            os.close();  
        }  
    }  
 



客户端:分两次取得部分内容,输出到RandomAccessFile中

 
    public static void main(String[] args) throws MalformedURLException, FileNotFoundException {  
            test1(0,1000);  
            test1(1001,0);  
        }  
          
        public static void test1(int start, int end) throws MalformedURLException, FileNotFoundException{  
              
            String endpoint = "http://localhost:8080/Hello/download?id=1";  
              
            RandomAccessFile raFile = new RandomAccessFile("E:/temp/test.chm", "rw");  
              
            URL url = new URL(endpoint);  
            try {  
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
                //conn.addRequestProperty("location", "/tomcat.gif");  
                conn.setRequestProperty("Content-Type","text/html; charset=UTF-8");   
                conn.setRequestProperty("RANGE","bytes="+start+"-"+end);   
                  
                conn.connect();  
                System.out.println(conn.getResponseCode());  
                System.out.println(conn.getContentLength());  
                System.out.println(conn.getContentType());  
                  
                InputStream ins = (InputStream)conn.getContent();  
                  
                raFile.seek(start);  
                  
                byte[] buffer = new byte[4096];  
                int len = -1;  
                while((len = ins.read(buffer))!=-1){  
                    raFile.write(buffer,0,len);  
                }  
                raFile.close();  
                conn.disconnect();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  

 

接下来的工作:实现客户端的并发,多线程,即多个下载任务同时进行,连接的复用,实现暂停,显示进度条,下载完成的事件处理等,不涉及具体业务,搭建整个架构。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值