java多线程加速下载

开启多个线程在一定程度上可以提高下载速度,单位时间内,服务器的cpu更多资源给你了,但是:

1. 不是线程开的越多 下载越快 

2. 受真实带宽的影响 
3. 受服务器带宽的影响  

javase实现多线程下载


  步骤:

1、在客户端创建一个与服务器端大小一样的空白文件
2、设置子线程的个数
    3、计算每个子线程下载的数据块大小和下载起始位置、结束位置
    4、创建子线程开始下载数据
5、得到每个子线程都下载完成的标记

代码:

MultiThreadDownLoader.java:


	import java.io.RandomAccessFile;
	import java.net.HttpURLConnection;
	import java.net.URL;
	public class MultiThreadDownLoader {


	//2、使用的子线程的个数
	private static int threadCount =3;
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			String path = "http://192.168.22.136:8080/sogou.exe";
			URL url = new URL(path);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("GET");
			conn.setConnectTimeout(3000);
			
			int code = conn.getResponseCode();
			if(code == 200){
			int length =	conn.getContentLength();
			//1、在客户端创建一个与服务端文件一样大小的文件
			RandomAccessFile file = new RandomAccessFile("temp.exe", "rw");
			file.setLength(length);
			//3、每个子线程下载数据块 ,下载的起始位置和结束位置
			int blockSize = length/threadCount;
			
			// threadId * blockSize  ---- (threadId+1)* blockSize -1
			for(int threadId =0; threadId < threadCount; threadId++){
				//下载的起始位置和结束位置
				int startIndex = threadId * blockSize;
				int endIndex = 0;
				
				if(threadId != (threadCount -1)){
					 endIndex = (threadId + 1) * blockSize - 1;
				}else{
					endIndex = length-1;
				}
				
				//开启子线程下载数据
				new ThreadDownLoader(path, startIndex, endIndex, threadId, threadCount).start();
				
			}
				
			}else{
				//抛出异常
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	}


ThreadDownLoader.java:


	import java.io.InputStream;
	import java.io.RandomAccessFile;
	import java.net.HttpURLConnection;
	import java.net.URL;




	public class ThreadDownLoader extends Thread {


	private String path;
	private int startIndex;
	private int endIndex;
	private int threadId;
	private int threadCount;
	//正在运行的线程个数
	private static  int runningThreadCount = 3; 
	ThreadDownLoader(String path,int startIndex,int endIndex,int threadId,int threadCount){
		this.path = path;
		this.startIndex = startIndex;
		this.endIndex = endIndex;
		this.threadId = threadId;
		this.threadCount = threadCount;
		
	}
	
	@Override
		public void run() {
		  downLoad(path,startIndex,endIndex,threadId,threadCount);
		}
	
	
	public void downLoad(String path,int startIndex,int endIndex,int threadId,int threadCount)
	 {
	  try {
		    URL url = new URL(path);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			
			conn.setRequestMethod("GET");
			conn.setConnectTimeout(3000);
			//设置子线程请求数据的范围
			conn.setRequestProperty("Range", "bytes="+startIndex+"-"+endIndex);
			int code = conn.getResponseCode();
			if(code == 206){//请求部分数据
				
			 InputStream is = conn.getInputStream();
			  RandomAccessFile file = new RandomAccessFile("temp.exe","rw");
			  //指定从哪个位置开始写数据
			  file.seek(startIndex);
			  
			   byte[] buffer = new byte[1024];
			  int len = -1;
			  while((len = is.read(buffer)) != -1){
				  file.write(buffer, 0, len);
			  }
			  
			  file.close();
			  System.out.println("线程"+threadId+"下载完成...............");
			  synchronized (ThreadDownLoader.this) {
				  runningThreadCount--;
				  if(runningThreadCount == 0){
					  System.out.println("文件下载完成...............");
				  }
			}
			}
	} catch (Exception e) {
		 System.out.println("线程"+threadId+"下载失败...............");
		e.printStackTrace();
	}
	  
	}


	}

多线程下载之断点续传

思路:

   1、实时记录线程下载的位置;

   2、接上一次下载的位置继续下载;

代码:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.channels.ReadableByteChannel;




public class ThreadDownLoader extends Thread {


	private String path;
	private int startIndex;
	private int endIndex;
	private int threadId;
	private int threadCount;
	//正在运行的线程个数
	private static  int runningThreadCount = 3; 
	ThreadDownLoader(String path,int startIndex,int endIndex,int threadId,int threadCount){
		this.path = path;
		this.startIndex = startIndex;
		this.endIndex = endIndex;
		this.threadId = threadId;
		this.threadCount = threadCount;
		
	}
	
	@Override
		public void run() {
		  downLoad(path,startIndex,endIndex,threadId,threadCount);
		}
	
	
  public void downLoad(String path,int startIndex,int endIndex,int threadId,int threadCount)
  {
	  try {
		    URL url = new URL(path);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			
			conn.setRequestMethod("GET");
			conn.setConnectTimeout(3000);
			
			//获取上一次下载的位置,接着下载
			File threadFile = new File(threadId+".txt");
			if(threadFile.exists() && threadFile.length() > 0){
				FileReader fr = new FileReader(threadFile);
				BufferedReader br = new BufferedReader(fr);
				String position = br.readLine();
				//设置子线程请求数据的范围
				conn.setRequestProperty("Range", "bytes="+position+"-"+endIndex);
				startIndex = Integer.valueOf(position);
			}else{
				//设置子线程请求数据的范围
				conn.setRequestProperty("Range", "bytes="+startIndex+"-"+endIndex);
			}
			
			
			int code = conn.getResponseCode();
			if(code == 206){//请求部分数据
				
			 InputStream is = conn.getInputStream();
			  RandomAccessFile file = new RandomAccessFile("temp.exe","rw");
			  //指定从哪个位置开始写数据
			  file.seek(startIndex);
			  
			   byte[] buffer = new byte[1024*1024];
			  int len = -1;
			  int total = 0;
			  while((len = is.read(buffer)) != -1){
				  file.write(buffer, 0, len);
				  total = total + len;
				  int currentPosition = startIndex + total;
				  //能够即时的把数据写到数据
				  RandomAccessFile f = new RandomAccessFile(threadId+".txt", "rwd");
				  f.write((currentPosition+"").getBytes());
				  f.close();
				  
			  }
			  
			  file.close();
			  System.out.println("线程"+threadId+"下载完成...............");
			  synchronized (ThreadDownLoader.this) {
				  runningThreadCount--;
				  if(runningThreadCount == 0){
					  System.out.println("文件下载完成...............");
				  }
			}
			}
	} catch (Exception e) {
		 System.out.println("线程"+threadId+"下载失败...............");
		e.printStackTrace();
	}
	  
  }


	}

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Barry__

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值