Java實現斷點續傳

原理:java

斷點續傳的關鍵是斷點,因此在制定傳輸協議的時候要設計好,如上圖,我自定義了一個交互協議,每次下載請求都會帶上下載的起始點,這樣就能夠支持從斷點下載了,其實HTTP裏的斷點續傳也是這個原理,在HTTP的頭裏有個可選的字段RANGE,表示下載的範圍,下面是我用Java語言實現的下載斷點續傳示例。網絡

提供下載的服務端代碼:多線程

  

         String path = "文件地址";
         BufferedInputStream bis = null;
         try {
             File file = new File(path);
             if (file.exists()) {
                 long p = 0L;
                 long toLength = 0L;
                 long contentLength = 0L;
                 int rangeSwitch = 0; // 0,從頭開始的全文下載;1,從某字節開始的下載(bytes=27000-);2,從某字節開始到某字節結束的下載(bytes=27000-39000)
                 long fileLength;
                 String rangBytes = "";
                 fileLength = file.length();
       
                 // get file content 
                 InputStream ins = new FileInputStream(file);
                 bis = new BufferedInputStream(ins);
//                 String d = new SimpleDateFormat("EEE,d MMM yyyy hh:mm:ss 'GMT' ",Locale.US).format(new Date());
//                 System.out.println(d);
                 // tell the client to allow accept-ranges
                 response.reset();
                 response.setHeader("Accept-Ranges", "bytes");
                 /*DateTime dateTime = DateTime.now();
                 System.out.println(dateTime.toString("r"));*/
                 response.setHeader("Last-Modified","Sat, 27 Jul 2017 12:14:58 GMT");
                 /*response.setHeader("HTTP/1.0 206 Partial Content", "206");*/
                 // client requests a file block download start byte
          
                 String range = request.getHeader("Range");
                log.info("range : "+range);
                 
                 if (range != null && range.trim().length() > 0 && !"null".equals(range)) {
                     response.setStatus(javax.servlet.http.HttpServletResponse.SC_PARTIAL_CONTENT);
                     rangBytes = range.replaceAll("bytes=", "");
                     if (rangBytes.endsWith("-")) {  // bytes=270000-
                         rangeSwitch = 1;
                         p = Long.parseLong(rangBytes.substring(0, rangBytes.indexOf("-")));
                         contentLength = fileLength - p;  // 客戶端請求的是270000以後的字節(包括bytes下標索引爲270000的字節)
                     } else { // bytes=270000-320000
                         rangeSwitch = 2;
                         String temp1 = rangBytes.substring(0, rangBytes.indexOf("-"));
                         String temp2 = rangBytes.substring(rangBytes.indexOf("-") + 1, rangBytes.length());
                         p = Long.parseLong(temp1);
                         toLength = Long.parseLong(temp2);
                         contentLength = toLength - p + 1; // 客戶端請求的是 270000-320000 之間的字節
                     }
                 } else {
                     contentLength = fileLength;
                 }
                
                 // 若是設設置了Content-Length,則客戶端會自動進行多線程下載。若是不但願支持多線程,則不要設置這個參數。
                 // Content-Length: [文件的總大小] - [客戶端請求的下載的文件塊的開始字節]
                 response.setHeader("Content-Length", new Long(contentLength).toString());
       
                 // 斷點開始
                 // 響應的格式是:
                 // Content-Range: bytes [文件塊的開始字節]-[文件的總大小 - 1]/[文件的總大小]
                 if (rangeSwitch == 1) {
                     String contentRange = new StringBuffer("bytes ").append(new Long(p).toString()).append("-")
                             .append(new Long(fileLength - 1).toString()).append("/")
                             .append(new Long(fileLength).toString()).toString();
                     response.setHeader("Content-Range", contentRange);
                     bis.skip(p);
                 } else if (rangeSwitch == 2) {
                     String contentRange = range.replace("=", " ") + "/" + new Long(fileLength).toString();
                     response.setHeader("Content-Range", contentRange);
                     bis.skip(p);
                 } else {
                     String contentRange = new StringBuffer("bytes ").append("0-")
                             .append(fileLength - 1).append("/")
                             .append(fileLength).toString();
                     response.setHeader("Content-Range", contentRange);
                 }
       
                 String fileName = file.getName();
                 response.setContentType("application/octet-stream");
                 response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
       
                 OutputStream out = response.getOutputStream();
                 
                 BufferedOutputStream buf=new BufferedOutputStream(out);
                 int n = 0;
                 long readLength = 0;
                 int bsize = 1024;
                 byte[] bytes = new byte[bsize];
                 if (rangeSwitch == 2) {
                     // 針對 bytes=27000-39000 的請求,從27000開始寫數據                    
                     while (readLength <= contentLength - bsize) {
                         n = bis.read(bytes);
                         readLength += n;
                         buf.write(bytes, 0, n);
                     }
                     if (readLength <= contentLength) {
                         n = bis.read(bytes, 0, (int) (contentLength - readLength));
                         buf.write(bytes, 0, n);
                     }                   
                 } else {
                     while ((n = bis.read(bytes)) != -1) {
                         buf.write(bytes,0,n);                                                      
                     }                   
                 }
                 buf.flush();
                 buf.close();
                 out.close();
                 bis.close();
             } else {
                 if (log.isDebugEnabled()) {
                     log.debug("Error: file " + path + " not found.");
                 }                
             }
         } catch (IOException ie) {
             // 忽略 ClientAbortException 之類的異常
         } catch (Exception e) {
             log.error(e.getMessage());
             e.printStackTrace();
         }

下載的客戶端代碼:app

  

    /** 
     *  request:get0startIndex0 
     *  response:fileLength0fileBinaryStream 
     *   
     * @param filepath 
     * @throws Exception 
     */  
    public void Get(String filepath) throws Exception {  
        Socket socket new Socket();  
        // 創建鏈接  
        socket.connect(new InetSocketAddress("127.0.0.1", 8888));  
        // 獲取網絡流  
        OutputStream out = socket.getOutputStream();  
        InputStream in = socket.getInputStream();  
        // 文件傳輸協定命令  
        byte[] cmd = "get".getBytes();  
        out.write(cmd);  
        out.write(0);// 分隔符  
        int startIndex = 0;  
        // 要發送的文件  
        File file = new File(filepath);  
        if(file.exists()){  
            startIndex = (int) file.length();  
        }  
        System.out.println("Client startIndex : " + startIndex);  
        // 文件寫出流  
        RandomAccessFile access = new RandomAccessFile(file,"rw");  
        // 斷點  
        out.write(String.valueOf(startIndex).getBytes());  
        out.write(0);  
        out.flush();  
        // 文件長度  
        int temp = 0;  
        StringWriter sw new StringWriter();  
        while((temp = in.read()) != 0){  
            sw.write(temp);  
            sw.flush();  
        }  
        int length = Integer.parseInt(sw.toString());  
        System.out.println("Client fileLength : " + length);  
        // 二進制文件緩衝區  
        byte[] buffer = new byte[1024*10];  
        // 剩餘要讀取的長度  
        int tatol = length - startIndex;  
        //  
        access.skipBytes(startIndex);  
        while (true) {  
            // 若是剩餘長度爲0則結束  
            if (tatol == 0) {  
                break;  
            }  
            // 本次要讀取的長度假設爲剩餘長度  
            int len = tatol;  
            // 若是本次要讀取的長度大於緩衝區的容量  
            if (len > buffer.length) {  
                // 修改本次要讀取的長度爲緩衝區的容量  
                len = buffer.length;  
            }  
            // 讀取文件,返回真正讀取的長度  
            int rlength = in.read(buffer, 0, len);  
            // 將剩餘要讀取的長度減去本次已經讀取的  
            tatol -= rlength;  
            // 若是本次讀取個數不爲0則寫入輸出流,不然結束  
            if (rlength > 0) {  
                // 將本次讀取的寫入輸出流中  
                access.write(buffer, 0, rlength);  
            } else {  
                break;  
            }  
            System.out.println("finish : " + ((float)(length -tatol) / length) *100 + " %");  
        }  
        System.out.println("finished!");  
        // 關閉流  
        access.close();  
        out.close();  
        in.close();  
    }  
  
    public static void main(String[] args) {  
        FTPClient client new FTPClient();  
        try {  
            client.Get("E:\\ceshi\\test\\mm.pdf");  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  

代碼直接copy過去就能夠了  服務端的代碼須要改下urldom

相關文章
相關標籤/搜索