多线程分段下载——附java代码注解

当时想着获取百度云盘真实下载地址,然后通过多线程分段下载来避开下载限速问题。记录一下线程分段下载

有哪位大神可以获取百度云真实下载地址,望留言

package test;

import java.util.concurrent.CountDownLatch;

public class Client {
	public static void main(String[] args) {
	//线程开启数
        int threadSize = 5;
        //文件真实下载地址
        String serverPath = "http://static.kunlun.com/bb/coc/Clash_of_Clans-11.49.9-kunlunLandingpage-release.apk";
        //文件下载本地存放地址
        String localPath = "E:/测试/部落冲突.apk";
        CountDownLatch latch = new CountDownLatch(threadSize);
        //线程每次读取字节数
        int numberOfBytes = 1024;
        MutiThreadDownLoad m = new MutiThreadDownLoad(threadSize, serverPath, localPath, latch, numberOfBytes);
        long startTime = System.currentTimeMillis();
        try {
            m.executeDownLoad();
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("全部下载结束,共耗时" + (endTime - startTime) / 1000 + "s");
    }
}

 

 

package test;

import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.CountDownLatch;
 
/**
 * 多线程下载模型
 *
 * @author bridge
 */
public class MutiThreadDownLoad {
    /**
     * 同时下载的线程数
     */
    private int threadCount;
    /**
     * 服务器请求路径
     */
    private String serverPath;
    /**
     * 本地路径
     */
    private String localPath;
    /**
     * 线程计数同步辅助
     */
    private CountDownLatch latch;
    
    /**
     * 多线程读取字节计数变量
     */
    private int count = 0;
    /**
     * 每次读取字节数
     */
    private int numberOfBytes;
 
    public MutiThreadDownLoad(int threadCount, String serverPath, String localPath, CountDownLatch latch,int numberOfBytes) {
        this.threadCount = threadCount;
        this.serverPath = serverPath;
        this.localPath = localPath;
        this.latch = latch;
        this.numberOfBytes = numberOfBytes;
    }
 
    public void executeDownLoad() {
 
        try {
            URL url = new URL(serverPath);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();//返回URLConnection表示与其引用的远程对象的连接的实例 URL。
            //设置在打开与此URLConnection引用的资源的通信链接时要使用的指定超时值(以毫秒为单位)。如果超时在可以建立连接之前到期,则引发java.net.SocketTimeoutException。超时为零被解释为无限超时。
            conn.setConnectTimeout(5000);
            conn.setRequestMethod("GET");
            //它将分别返回200和401。如果无法从响应中识别出代码,则返回-1(即,响应不是有效的HTTP)。
            int code = conn.getResponseCode();
            if (code == 200) {
                //服务器返回的数据的长度,实际上就是文件的长度,单位是字节
                int length = conn.getContentLength();
                System.out.println("文件总长度:" + length + "字节(B)");
                //创建随机访问文件流,以便从File参数指定的文件中读取,也可以选择写入。RandomAccessFile(File file, String mode)
                RandomAccessFile raf = new RandomAccessFile(localPath, "rwd");
                //指定创建的文件的长度
                raf.setLength(length);
                raf.close();
                //分割文件
                int blockSize = length / threadCount;
                
                
                for (int threadId = 1; threadId <= threadCount; threadId++) {
                	
                	//线程第一次启动时调用守护线程进行下载速度监控
                	if (threadId==1) {
                		Thread detection = new Thread(){
                            public void run() {
                              while(true){
                                 try {
                            	   System.out.println("当前下载速度: "+count/(1024*1024)+" MB/S");
                                   //清空当前这秒读取到的字节数
                            	   count = 0;
                                   Thread.sleep(1000);//阻塞1秒
                                 } catch (InterruptedException e) {
                                   e.printStackTrace();
                                 }
                              }
                            }
                          };
                          detection.setDaemon(true);//设置成守护线程,一定要在启动之前设置
                          detection.start();//启动守护线程
					}
                	
                	
                    //第一个线程下载的开始位置
                    int startIndex = (threadId - 1) * blockSize;
                    int endIndex = startIndex + blockSize - 1;
                    if (threadId == threadCount) {
                        //最后一个线程下载的长度稍微长一点
                        endIndex = length;
                    }
                    System.out.println("线程" + threadId + "下载:" + startIndex + "字节~" + endIndex + "字节");
                    new DownLoadThread(threadId, startIndex, endIndex).start();//启动下载线程
                }
 
            }
 
        } catch (Exception e) {
            e.printStackTrace();
        }
 
 
    }
 
 
    /**
     * 内部类用于实现下载
     */
    public class DownLoadThread extends Thread {
        /**
         * 线程ID
         */
        private int threadId;
        /**
         * 下载起始位置
         */
        private int startIndex;
        /**
         * 下载结束位置
         */
        private int endIndex;
        
        public DownLoadThread(int threadId, int startIndex, int endIndex) {
            this.threadId = threadId;
            this.startIndex = startIndex;
            this.endIndex = endIndex;
        }
 
 
        @Override
        public void run() {
            try {
                System.out.println("线程" + threadId + "正在下载...");
                URL url = new URL(serverPath);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");
                //请求服务器下载部分的文件的指定位置
                conn.setRequestProperty("Range", "bytes=" + startIndex + "-" + endIndex);
                conn.setConnectTimeout(5000);
                int code = conn.getResponseCode();
 
                System.out.println("线程" + threadId + "请求返回code=" + code);
 
                InputStream is = conn.getInputStream();//返回资源
                RandomAccessFile raf = new RandomAccessFile(localPath, "rwd");
                //随机写文件的时候从哪个位置开始写
                raf.seek(startIndex);//定位文件
 
                int len = 0;
                byte[] buffer = new byte[numberOfBytes];
                while ((len = is.read(buffer)) != -1) {
                    raf.write(buffer, 0, len);
                    count+=len;
                }
                is.close();
                raf.close();
                System.out.println("线程" + threadId + "下载完毕");
                //计数值减一
                latch.countDown();
 
            } catch (Exception e) {
                e.printStackTrace();
            }
 
        }
        
       
        
    }
}
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值