Java單線程檔案下載,支援斷點續傳功能

來源:互聯網
上載者:User

標籤:exists   strong   timeout   參數   version   access   inf   檔案   目標   

前言:

       程式下載檔案時,有時會因為各種各樣的原因下載中斷,對於小檔案來說影響不大,可以快速重新下載,但是下載大檔案時,就會耗費很長時間,所以斷點續傳功能對於大檔案很有必要。

檔案下載的斷點續傳:

  1、先下載臨時檔案,用於記錄已下載大小:

        2、http請求時設定Range參數

        3、下載此次請求的資料;

直接上代碼:

  1 package com.test.service;  2   3 import java.io.File;  4 import java.io.InputStream;  5 import java.io.RandomAccessFile;  6 import java.net.HttpURLConnection;  7 import java.net.URL;  8 import java.text.NumberFormat;  9  10 import org.slf4j.Logger; 11 import org.slf4j.LoggerFactory; 12 import org.springframework.beans.factory.annotation.Value; 13 import org.springframework.stereotype.Component; 14  15 /** 16  * <p> 17  *     檔案下載,可以支援斷點續傳 18  *     暫未使用 19  * </p> 20  *  @author  21  *  @version 1.0 22  * */ 23 @Component 24 public class DownloadOnly { 25  26     private static final Logger logger = LoggerFactory.getLogger(DownloadOnly.class); 27      28     @Value("${onair.download.ddxc:true}") 29     boolean ddxc = true; 30      31     int startIndex = 0; 32      33     long downloadSize = 0; 34      35     boolean downloadFinish = false; 36      37     int totleSize = 0; 38      39     public boolean download(String url,String file_path,int downloadTimeout){ 40  41         //起一個線程 檢測下載進度 42         new Thread(new Runnable() { 43              44             @Override 45             public void run() { 46                 try { 47                      NumberFormat nt = NumberFormat.getPercentInstance();   48                       //設定百分數精確度3即保留三位小數   49                      nt.setMinimumFractionDigits(1);   50                     while(!downloadFinish){ 51                         Thread.sleep(30000); 52                         logger.debug("已下載大小{},進度{}",getDownloadSize(),nt.format(getDownloadSize()* 1.0 /totleSize)); 53                     } 54                 } catch (InterruptedException e) { 55                     e.printStackTrace(); 56                 } 57                  58             } 59         }).start(); 60          61         logger.info("下載檔案:源路徑{},目標路徑:{}",url,file_path); 62         RandomAccessFile raf = null; 63         InputStream in = null; 64          65         try { 66             URL file_url = new URL(url); 67             HttpURLConnection conn = (HttpURLConnection)file_url.openConnection(); 68             conn.setConnectTimeout(downloadTimeout); 69             conn.setRequestMethod("GET"); 70             File tmpFile = new File(file_path+"_tmp"); 71             if(ddxc){ 72                 if(tmpFile.exists() && tmpFile.isFile()){ 73                     downloadSize = tmpFile.length(); 74                     startIndex = (int)downloadSize; 75                 } 76                 conn.setRequestProperty("Range", "bytes=" + startIndex + "-"); 77             }else{ 78                 if(tmpFile.exists() && tmpFile.isFile()) 79                     tmpFile.delete(); 80             } 81             int status = conn.getResponseCode(); 82             totleSize = (int)downloadSize + conn.getContentLength(); 83             logger.info("檔案總大小{},下載請求獲得的返回狀態代碼:{},需要下載的大小{}",totleSize,status,totleSize-downloadSize); 84             if(status== 200 || status == 206 ){ 85                 raf = new RandomAccessFile(tmpFile, "rwd"); 86                 raf.seek(startIndex); 87                 in = conn.getInputStream(); 88                 byte[] buffer = new byte[1024]; 89                 int size = 0; 90                 while((size=in.read(buffer)) !=-1 ){ 91                     raf.write(buffer, 0, size); 92                     downloadSize += size; 93                 } 94                 raf.close(); 95                 in.close(); 96                 File dest = new File(file_path); 97                 return tmpFile.renameTo(dest); 98             } 99         } catch (Throwable e) {100             logger.error("檔案下載失敗:{}",e.getMessage(),e);101         }finally {102             downloadFinish = true; //下載完成或中斷103         }104         return false;105     }106 107     public long getDownloadSize() {108         return downloadSize;109     }110     111     public static void main(String[] args) {112         DownloadOnly downloadOnly = new DownloadOnly();113     }114     115 }

Java單線程檔案下載,支援斷點續傳功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.