2021-11-04

多线程下载文件

package cn.hp;

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

/**
 * 多线程下载
 * @author wyl
 * 通过URL和下载资源建立联系
 * 设置参数,读取的范围
 * 开始进行文件的读取和写入
 * 关闭流
 *
 */
public class DownLoad2 extends Thread {
	//多线程下载需要的信息
	private long startIndex;
	private long endIndex;
	private int threadId;
	private String name;
	private static String path="D:\\upload\\";

	static {
		File file  = new File(path);
		if(!file.exists()){
			file.mkdirs();
		}
	}

	public DownLoad2(long startIndex, long endIndex, int threadId,String name) {
		this.startIndex = startIndex;
		this.endIndex = endIndex;
		this.threadId = threadId;
		this.name=name;
	}

	@Override
	public void run() {
		long start =System.currentTimeMillis();
		try {
			//统一资源管理器,可以直接打开网络地址
			URL url = new URL(DownLoadTest2.path);//获取资源地址
			//因为是基于Http请求,获取httpUrlConnection,获取网络连接对象,和服务器建立联系
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			//设置参数
			conn.setRequestMethod("GET");
			conn.setConnectTimeout(50000);//连接超时
			conn.setReadTimeout(100000);//读取超时
			conn.addRequestProperty("Range", "bytes="+startIndex+"-"+endIndex);//设置读取范围
			
			if(conn.getResponseCode()==206) {//部分数据ok
				//取出连接中的数据
				InputStream is = conn.getInputStream();
				//提供一个接收的地方(本地接收)

				File file = new File(path+name);
				//输出流,开始写数据,随机读写流
				RandomAccessFile raf = new RandomAccessFile(file, "rwd");
				raf.seek(startIndex);//跳转到线程开始的位置
				System.out.println("线程:"+this.getName()+"  "+threadId+" 开始的位置:"+startIndex+"---"+" 结束位置"+endIndex);
		          
				int i=0;
				byte [] buf= new byte[8192];//字节数组,提高读取速度
				int len=0;//字节数组读取的长度
				while((len=is.read(buf))!=-1) {
					System.out.println(this.getName()+"  "+(i++));
					raf.write(buf, 0, len);
				}
				raf.close();
				is.close();
				
				DownLoadTest2.okThread++;
				
				synchronized (DownLoadTest2.path) {
					System.out.println("线程:"+this.getName()+threadId+"下载完毕...   "+DownLoadTest2.okThread);
					if(DownLoadTest2.okThread==DownLoadTest2.countThread) {
						DownLoadTest2.okThread=0;
					}
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		long end =System.currentTimeMillis();
		System.out.println("线程:"+this.getName()+threadId+"运行时间"+(end - start));
	}
	
	
	
	
}
package cn.hp;

import com.entity.DownLoad2;

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

/**
 * 多线程下载测试类
 */
public class DownLoadTest2 {
	
	static String path = "https://plugins.jetbrains.com/files/14662/92372/gitee-idea-plugin-1.4-SNAPSHOT.zip?updateId=92372&pluginId=14662&family=INTELLIJ";
	static int countThread=2;//线程数
	static int okThread=0;//和线程数有关,记录线程完成的个数
	
	public static void main(String[] args) throws Exception {
		long start =System.currentTimeMillis();
		//统一资源管理器,可以直接打开网络地址
		URL url = new URL(DownLoadTest2.path);
		//因为是基于Http请求,获取httpUrlConnection,获取网络连接对象,和服务器建立联系
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		//设置参数
		conn.setRequestMethod("GET");
		conn.setConnectTimeout(50000);//连接超时
		conn.setReadTimeout(200000);//读取超时
		//判断资源链接是否建立成功,成功进行多线程下载分配
		if(conn.getResponseCode()==200) {//成功
			//获取文件总长度
			long count = conn.getContentLengthLong();
			//每个线程下载的大小
			long size = count/countThread;
			//开启线程进行下载
			for (int i = 0; i < 1; i++) {
				long startIndex=size*i;//开始位置     0  		102.4
				long endIndex=size*(i+1);//结束位置   102.4		204.8
				DownLoad2 thread = new DownLoad2(startIndex,endIndex , i,"gitee-idea-plugin-1.4-SNAPSHOT.zip");
				thread.start();
			}
		}
		long end =System.currentTimeMillis();
		System.out.println(end-start);
		
	}
	
	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值