多線程斷點下載文件


package cn.itcast.download;

import java.io.File;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;

public class MulThreadDownloader {

    public static void main(String[] args) throws Exception {
        String path = "http://192.168.1.100:8080/web/QQWubiSetup.exe";
        int threadsize = 3;
        new MulThreadDownloader().download(path, threadsize);

    }

    private void download(String path, int threadsize) throws Exception {
        URL downpath = new URL(path);
        HttpURLConnection conn = (HttpURLConnection) downpath.openConnection();
        conn.setConnectTimeout(5000);
        conn.setRequestMethod("GET");
        if(conn.getResponseCode() == 200){
            int length = conn.getContentLength();//獲取網絡文件的長度
            File file = new File(getFileName(path));
            RandomAccessFile accessFile = new RandomAccessFile(file, "rwd");//生成本地文件
            accessFile.setLength(length);
            accessFile.close();
            //計算每條線程負責下載的數據量
            int block = length % threadsize == 0 ? length / threadsize : length / threadsize +1;
            for(int threadid = 0 ; threadid < threadsize ; threadid++){
                new DownloadThread(threadid, downpath, block, file).start();
            }
        }
    }
    //負責下載操作
    private final class DownloadThread extends Thread{
        private int threadid;
        private URL downpath;
        private int block;
        private File file;
        
        public DownloadThread(int threadid, URL downpath, int block, File file) {
            this.threadid = threadid;
            this.downpath = downpath;
            this.block = block;
            this.file = file;
        }
        public void run() {
            int startposition = threadid * block;//從網絡文件的什麼位置開始下載數據
            int endposition = (threadid+1) * block - 1;//下載到網絡文件的什麼位置結束
            //指示該線程要從網絡文件的startposition位置開始下載,下載到endposition位置結束
            //Range:bytes=startposition-endposition
            try{
                RandomAccessFile accessFile = new RandomAccessFile(file, "rwd");
                accessFile.seek(startposition);//移動指針到文件的某個位置
                HttpURLConnection conn = (HttpURLConnection) downpath.openConnection();
                conn.setConnectTimeout(5000);
                conn.setRequestMethod("GET");
                conn.setRequestProperty("Range", "bytes="+ startposition+ "-"+ endposition);
                InputStream inStream = conn.getInputStream();
                byte[] buffer = new byte[1024];
                int len = 0;
                while( (len = inStream.read(buffer)) != -1 ){
                    accessFile.write(buffer, 0, len);
                }
                accessFile.close();
                inStream.close();
                System.out.println("第"+ (threadid+1)+ "線程下載完成");
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * 獲取文件名稱
     * @param path 下載路徑
     * @return
     */
    private static String getFileName(String path) {
        return path.substring(path.lastIndexOf("/")+ 1);
    }

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章