package com.web.test2;
import java.io.BufferedInputStream;
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;
class MultiDownload{
    public static void main(String[] args) {
        MultiDownload down = new MultiDownload();
        down.downProcess("http://www.baidu.com/img/baidu_logo.gif","F:\\logo.gif",3);
    }
    private void downProcess(String url, String dest, int threadNum) {
        //计算文件大小
        long fileSize = getFileLength(url);
        //计算每个线程需要下载多少个字节
        long byteCount = fileSize/threadNum+1;
        File file = new File(dest);
        int i = 0;
        while (i<threadNum) {
            //计算每个线程需要下载的起始点和结尾点
            final long startPosition = byteCount*i;
            final long endPosition = byteCount*(i+1);
            /*最后一次下载,从startPosition开始到末尾*/
            if (i == threadNum-1) {
                TestDownThread fileThread = new TestDownThread(url, file, startPosition, 0);
                new Thread(fileThread).start();
            }else {
                TestDownThread fileThread = new TestDownThread(url, file, startPosition, endPosition);
                new Thread(fileThread).start();
            }
            i++;
        }
    }
    private long getFileLength(String url) {
        long size = 0;
        try {
            URL downUrl = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) downUrl.openConnection();
            int stateCode = connection.getResponseCode();
            if (stateCode == 200) {
                size = connection.getContentLength();
                connection.disconnect();
            }
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return size;
    }
     class TestDownThread implements Runnable{
        String url = "";
        File file;
        long startPosition;
        long endPosition;
        public TestDownThread(String url, File file, long startPosition,long endPosition) {
            super();
            this.url = url;
            this.file = file;
            this.startPosition = startPosition;
            this.endPosition = endPosition;
        }
        @Override
        public void run() {
            try {
                URL downUrl = new URL(url);
                HttpURLConnection connection = (HttpURLConnection) downUrl.openConnection();
                /*设置请求参数*/
                connection.setRequestProperty("User-Agent", "NetFox");
                String sProperty = "bytes="+startPosition+"-";
                if (endPosition > 0) {
                    sProperty = "bytes="+startPosition+"-"+endPosition;
                }
                connection.setRequestProperty("RANGE", sProperty);
                connection.connect();
                //将下载的信息写入本地文件
                RandomAccessFile dst = new RandomAccessFile(file, "rw");
                //从startPosition位置开始写入信息
                dst.seek(startPosition);
                //读取URL定位的文件信息
                InputStream in = connection.getInputStream();
                BufferedInputStream bis = new BufferedInputStream(in);
                int len = 0;
                byte[] b = new byte[2048];
                while ((len = bis.read(b)) != -1) {
                    dst.write(b,0,len);
                }
                dst.close();
                bis.close();
                connection.disconnect();
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("一个线程结束");
        }
    }
}                
                










