Java-->實現斷點續傳(下載),java--斷點續傳

來源:互聯網
上載者:User

Java-->實現斷點續傳(下載),java--斷點續傳

--> 斷點續傳: 就像迅雷下載檔案一樣,停止下載或關閉程式,下次下載時是從上次下載的地方開始繼續進行,而不是重頭開始...

--> RandomAccessFile --> pointer(檔案指標) --> seek(移動檔案指標) --> 斷點續傳

package com.dragon.java.downloadfile;import java.io.File;import java.io.IOException;import java.io.RandomAccessFile;/* 斷點續傳:對下載軟體非常重要! --> 第一次下載 100 位元組 --> 第二次下載 101 位元組...想辦法知道上次從哪個地方斷掉的。  上次已經下載到了什麼位置。  記下斷點的位置 ------> 需要第三方的檔案專門記住斷點的位置 */public class Test {    public static void main(String args[]) {        File srcFile = new File("D:/Java4Android/01_Java考古學/01_Java考古學.mp4");        File desDir = new File("f:/vidio");        copyFileToDir(srcFile, desDir);    }    public static void copyFileToDir(File srcFile, File desDir) {        desDir.mkdirs();        // 建立設定檔        File configFile = new File(desDir, srcFile.getName().split("\\.")[0]                + ".config");        // 建立目標檔案        File desFile = new File(desDir, srcFile.getName());        if (!configFile.exists() && desFile.exists()) {            System.out.println("已下載過該檔案!");            return;        }        RandomAccessFile rafSrc = null;        RandomAccessFile rafDes = null;        RandomAccessFile rafConfig = null;        try {            rafSrc = new RandomAccessFile(srcFile, "r");            rafDes = new RandomAccessFile(desFile, "rw");            rafConfig = new RandomAccessFile(configFile, "rw");            // 設定目標檔案和源檔案一樣長            rafDes.setLength(srcFile.length());            // 設定配置的檔案長度為8,防止第一次下載是出現EOF 異常            rafConfig.setLength(8);            // 從上次下載的位置開始繼續下載!            long pointer = rafConfig.readLong();            System.out.println("已下載:" + ((float) pointer / srcFile.length())                    * 100 + "%");            rafSrc.seek(pointer);            rafDes.seek(pointer);            // 單次傳輸長度設定小點,好觀察是否斷點續傳            byte[] buffer = new byte[32];            int len = -1;            // 每次複製的開始,必須把源檔案的指標和目標檔案的指標從上次斷開的位置去讀            while ((len = rafSrc.read(buffer)) != -1) {                rafDes.write(buffer, 0, len);                // 在設定檔寫的時候,每次使檔案指標移動到最初的位置 --> 這樣永遠對只會儲存前8個位元組                rafConfig.seek(0);                // 每複製一次之和,趕緊記錄下檔案指標的位置,以備斷點續傳使用。                rafConfig.writeLong(rafSrc.getFilePointer());            }        } catch (IOException e) {            System.out.println(e);        } finally {            try {                rafSrc.close();                rafDes.close();                rafConfig.close();            } catch (IOException e) {                System.out.println(e);            }            // 在流關閉之後刪除設定檔            System.out.println("下載成功!");            configFile.delete();        }    }}

--> 通過複製來類比簡單的斷點續傳...

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.