多线程下载

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

public class DownLoad {

    //定义线程数
    static int threadCount = 3;
    //下载地址
    static String path = "http://127.0.0.1/test.avi";

    public static void main(String[] args) {

        try {
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            conn.setRequestMethod("GET");
            conn.setReadTimeout(5000);
            conn.setConnectTimeout(5000);

            if (conn.getResponseCode() == 200) {
                //获取文件总的字节数
                int len = conn.getContentLength();
                //计算每个线程下载的字节数
                int size = len / threadCount;
                //指定临时文件的路径和文件名
                File file = new File(getFileName(path));
                RandomAccessFile raf = new RandomAccessFile(file, "rw");
                //设置临时文件的大小,跟服务器文件一模一样
                raf.setLength(len);

                for (int i = 0; i < threadCount; i++) {
                    //开始位置
                    int startIndex = i * size;
                    //结束位置
                    int endIndex = (i + 1) * size - 1;
                    //设置最后一个线程的结束位置为总的字节数减去1
                    if (i == threadCount - 1) {
                        endIndex = len - 1;
                    }
                    System.out.println("线程" + i + "," + startIndex + "--->" + endIndex);
                    //启动线程开始下载
                    new MyThread(i, size, startIndex, endIndex).start();


                }

            }

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static String getFileName(String path) {
        int index = path.lastIndexOf("/");
        return path.substring(index + 1);
    }

    static class MyThread extends Thread {
        @Override
        public void run() {

            try {
                int total = startIndex;
                URL url = new URL(path);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();

                conn.setRequestMethod("GET");
                conn.setReadTimeout(5000);
                conn.setConnectTimeout(5000);
                //设置请求的数据的范围bytes=10-100表示请求第10到100的字节
                conn.setRequestProperty("Range", "bytes=" + startIndex + "-" + endIndex);

                //这里的请求206为成功
                if (conn.getResponseCode() == 206) {
                    InputStream is = conn.getInputStream();
                    File file = new File(getFileName(path));
                    RandomAccessFile raf = new RandomAccessFile(file, "rw");
                    raf.seek(startIndex);

                    int len = 0;
                    byte[] b = new byte[1024];
                    while ((len = is.read(b)) != -1) {
                        total +=len;
                        System.out.println("线程" + threadNum + "----->已下载" + total);
                        raf.write(b, 0, len);
                    }
                    raf.close();
                    System.out.println("线程" + threadNum + "下载完成");
                }

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        int size;
        int startIndex;
        int endIndex;
        int threadNum;

        public MyThread(int threadNum, int size, int startIndex, int endIndex) {
            super();
            this.size = size;
            this.startIndex = startIndex;
            this.endIndex = endIndex;
            this.threadNum = threadNum;
        }

        public int getSize() {
            return size;
        }

        public void setSize(int size) {
            this.size = size;
        }

        public int getStartIndex() {
            return startIndex;
        }

        public void setStartIndex(int startIndex) {
            this.startIndex = startIndex;
        }

        public int getEndIndex() {
            return endIndex;
        }

        public void setEndIndex(int endIndex) {
            this.endIndex = endIndex;
        }

        public MyThread(int size, int startIndex, int endIndex) {
            this.size = size;
            this.startIndex = startIndex;
            this.endIndex = endIndex;
        }

        public MyThread() {

        }

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值