使用多线程实现一个对百度网盘的下载

1.创建download类实现Thread实现Thread的run方法

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 extends Thread {
    private int startIndex;//开始的位置
    private int endIndex;//结束的位置
    private int threadId;//线程的id

    public DownLoad(int threadId,int startIndex, int endIndex) {
        this.startIndex = startIndex;//线程开始下载的位置
        this.endIndex = endIndex;//线程结束下载的位置
        this.threadId = threadId;//线程的id
    }

    @Override
    public void run() {
        try {
            //统一资源管理器,可以直接打开 网络地址
            URL url=new URL(DowmLoadTest.path);
            //因为基于http请求, 获取HttpURLConnection,获取网络连接对象,就可以和服务器建立链接
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            //给conn设置参数
            conn.setRequestMethod("GET");//设置get请求
            conn.setConnectTimeout(5000);//设置链接超时
            conn.setReadTimeout(5000);//设置读取超时
            conn.setRequestProperty("Range","bytes="+startIndex+"-"+endIndex);//设置 范围 请求参数 获取 线程下载范围

            if(conn.getResponseCode()==206){//部分数据ok
                //取出 链接中的数据
                InputStream is = conn.getInputStream();
                //提供一个接收地方(本地接收)
                File file=new File("BaiduNetdisk_6.9.7.4.exe");
                //输出流,开始写数据,随机读写 流
                RandomAccessFile raf=new RandomAccessFile(file,"rwd");
                raf.seek(startIndex);//跳转到 每个线程 开始读取的位置
                System.out.println("线程id:"+threadId+",开始的位置:"+startIndex+"-------"+"结束的位置"+endIndex);

                int len=0;
                byte[] by=new byte[1024];
                if((len=raf.read(by))!=-1){
                    raf.write(by,0,len);
                }
                System.out.println("线程:"+threadId+"下载完毕...");
                raf.close();
                DowmLoadTest.finishedThread++;
                //如果所有的线程下载完毕,让finishedThread=0;
                synchronized (DowmLoadTest.path){
                    DowmLoadTest.finishedThread=0;
                }
            }


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

测试类

import java.net.HttpURLConnection;
import java.net.URL;

public class DowmLoadTest {
    static String path = "http://softforspeed.51xiazai.cn/down/BaiduNetdisk_6.9.7.4.exe";//百度网盘的下载路径
    static int threadCount=3;//线程的数量
    static int finishedThread=0;//和线程有关,记录每一个线程时候执行完毕

    public static void main(String[] args) throws Exception {
        //统一资源管理器,可以直接打开 网络地址
        URL url=new URL(DowmLoadTest.path);
        //因为基于http请求, 获取HttpURLConnection,获取网络连接对象,就可以和服务器建立链接
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        //给conn设置参数
        conn.setRequestMethod("GET");//设置get请求
        conn.setConnectTimeout(5000);//设置链接超时
        conn.setReadTimeout(5000);//设置读取超时

        if(conn.getResponseCode()==200){
            //1.拿到文件的总长
            int contentlength=conn.getContentLength();
            //2.每个线程下载的大小
            int size=contentlength/threadCount;
            for (int i = 0; i < 3; i++) {
                int startindex=i*size;
                int endindex=(i+1)*size;
                DownLoad downLoad=new DownLoad(i,startindex,endindex);
                downLoad.start();
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值