斷點續傳 Java實現文件斷點續傳
package com.chinacick.yummy.Httpftp.test;
import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.URL; public class DownloadFile { /** * @param args */ static int len;//線程平均下載文件長度 static int bn ;//每個線程寫入文件的字節數 static int tn; //線程數 static String urlt;//下載地址 static String fileName; static RandomAccessFile osf; //文件操作 public static void main(String[] args) { // TODO Auto-generated method stub try { urlt = "http://192.168.16.77:8080/Stargreat/html/baidu player"; fileName = "C:\" + urlt.split("http://")[1].split("/")[urlt.split("http://")[1].split("/").length-1]; System.out.println(fileName); URL url = new URL(urlt); HttpURLConnection http = (HttpURLConnection) url.openConnection(); /** * 此處設定5個線程下載一個文件tn = 5; * 判斷平均每個線程需下載文件長度: */ System.out.println("file size:" + http.getContentLength()); tn = 3; len = http.getContentLength() / tn ;//舍去余數(余數自動舍去)計算每個線程應下載平均長度,最后一個線程再加上余數,則是整個文件的長度, File f = new File(fileName); if (f.exists()){ f.delete(); osf = new RandomAccessFile(f, "rw"); osf.seek(http.getContentLength()-1); osf.write(0); }else{ osf = new RandomAccessFile(f, "rw"); osf.seek(http.getContentLength()-1); osf.write(0); } System.out.println("temp 文件長度:" + f.length()); Thread t;//下載子線程, for (int j = 0; j < tn; j++) { if(j == tn - 1){//如果最后一個線程則加上余數長度字節 bn = len + (http.getContentLength() % tn); }else{ bn = len; } System.out.println("t"+ j + "線程下載長度:" + bn + "起始字節:" + len*j); t = new DT( j, urlt, fileName, len*j, bn ); t.start(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
package com.chinacick.yummy.Httpftp.test;
import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.Date; public class DT extends Thread { String urlt;//下載地址 int startl;//子線程讀取/寫入起始字節 int end;//子線程寫入結束字節長度 String fileName; RandomAccessFile osf; public DT(int i ,String url,String fileName,int start,int end){ this.setName("t"+i); //子線程名稱 this.urlt = url; //下載地址 this.fileName = fileName; this.startl = start; //子線程讀取/寫入起始字節 this.end = end;//子線程寫入結束字節長度 } public void run(){ try { osf = new RandomAccessFile(fileName, "rw"); URL url = new URL(urlt); HttpURLConnection http2 = (HttpURLConnection) url.openConnection(); http2.setRequestProperty("User-Agent","NetFox"); /* * 斷點續傳和多線程下載的關鍵代碼關鍵位置:即設置斷點 * http2.setRequestProperty("RANGE", "bytes="+startl+"-");//設置斷點位置,向服務器請求從文件的哪個字節開始讀取。 * osf.seek(startl);//設置本地文件從哪個字節開始寫入 * 如果是單線程,則首先要判斷下載文件是否已經存在 及DownloadFile.java 里的 fileName = "C:\eclipse.zip"; * 如果存在則開始斷點續傳,方法同多線程: * 因為斷點續傳是從上次傳輸中斷的字節開始,則首先要得到上次中斷的位置,既是文件長度(針對單線程)f.length() * 然后設置HTTP請求頭屬性RANGE,該屬性告知服務器從哪個自己開始讀取文件。 * 設置本地文件寫入起始字節,及接從上次傳輸斷點繼續寫入(斷點續傳) * osf.seek(offset) 該方法設定從offset后一個字節開始寫入文件 * 注意:多線程不能用文件長度做為寫文件起始字節,需有配置文件記錄上次讀寫的位置,迅雷下載既是使用該種方法。 * */ http2.setRequestProperty("RANGE", "bytes="+startl+"-");//設置斷點位置,向服務器請求從文件的哪個字節開始讀取。 osf.seek(startl);//設置本地文件從哪個字節開始寫入 InputStream input = http2.getInputStream(); byte b[] = new byte[1024];//設置緩沖池,每次只讀1024字節 Date d = new Date();//子線程開始下載時間 int l;//計算子線程讀取和寫入的文件長度,當長度大于每個子線程平均下載長度則終止線程 int i; l = 0; System.out.println(this.getName() + " 開始下載。。。"); while ( (i = input.read(b,0,1024)) != -1 && l < end){ //線程下載字節長度控制誤差小于緩沖池大小,本示例為緩沖池1024字節 osf.write(b, 0, i); b = new byte[1024];//重新賦值,避免重新讀入舊內容 l += i; } Date d2 = new Date();//子線程結束下載時間 System.out.println(this.getName() + " 線程耗時: " + (d2.getTime() - d.getTime())/1000 + " 秒,實際共下載:" + l + "字節");//子線程下載耗時(秒) } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
本文由用戶 SimoneNewco 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!