多线程实现同时下载(未加断点续传)直接全部下载_有待完善

完整版传送门

自定义线程类:

package 多线程下载器;

import java.io.BufferedInputStream;
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;
import java.text.DecimalFormat;

public class MyDownLoading implements Runnable {
	private String STRURL = null;
	private String morenName = null;
	private static int MAX=1024000;
	@Override
	public void run() {
		// TODO Auto-generated method stub
		HttpURLConnection connection = null;
		
		try {
			URL url = new URL(STRURL);
			connection = (HttpURLConnection)url.openConnection();
			connection.addRequestProperty("Range", "bytes=0-");
			connection.connect();
			
			if(connection.getResponseCode()/100!=2) {
				System.out.println("未连接成功!,退出");
				return;
			}else {
				System.out.println(Thread.currentThread().getName()+"连接成功!");
			}
			
			if(this.morenName==null) {
				this.morenName = STRURL.substring(STRURL.lastIndexOf('/')+1);
			}else {
				this.morenName+=STRURL.substring(STRURL.lastIndexOf('.'));
			}
			DecimalFormat decimalFormat = new DecimalFormat("0.00");
			DecimalFormat format = new DecimalFormat("0.00%");
			int fileLength = connection.getContentLength();//文件长度
			System.out.printf("文件大小: %.2fMB\n",fileLength/1024.0/1024);
			long timeStart = System.currentTimeMillis();
			try(InputStream inputStream = connection.getInputStream();
				BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
				RandomAccessFile accessFilec = new RandomAccessFile(new File("taget/"+this.morenName), "rw");		
				){
				accessFilec.setLength(0);
				int sec =0;
				accessFilec.seek(sec);
				byte[] bs = null;
				int avg = 0;//计数循环了几次,来算平均下载速度
				double sum = 0;
				while(sec<fileLength) {
					long startTime = System.currentTimeMillis();
					if(sec+MAX>fileLength) {
						bs = new byte[fileLength-sec];
					}else {
						bs = new byte[MAX];
					}
					for (int i = 0; i < bs.length; i++) {
						bs[i] = (byte) bufferedInputStream.read();
					}
					accessFilec.seek(sec);
					accessFilec.write(bs);
					long endTime = System.currentTimeMillis();
					System.out.println(
							Thread.currentThread().getName()+"已下载: "+format.format((double)sec/fileLength)
					 		+",下载速度: "+decimalFormat.format((bs.length/1024.0/1024.0/((endTime-startTime)/1000.0)))+"MB/s"
					 		);
					sec+=MAX;
					avg++;
					sum+=(bs.length/1024.0/1024.0/((endTime-startTime)/1000.0));
				}
				long timeEnd = System.currentTimeMillis();
				if((timeEnd-timeStart)/1000.0<60) {
					System.out.println(Thread.currentThread().getName()+" 已下载完成!您本次的平均下载速度是: "+decimalFormat.format(sum/avg)+"MB/s"+",耗时: "+(timeEnd-timeStart)/1000.0+"s");
				}
				else {
					System.out.println(Thread.currentThread().getName()+" 已下载完成!您本次的平均下载速度是: "+decimalFormat.format(sum/avg)+"MB/s"+",耗时: "+(timeEnd-timeStart)/1000.0/1000.0+"min");
				}
			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public MyDownLoading() {
		
	}
	
	public MyDownLoading(String URL) {
		this.STRURL = URL;
	}
	
	public MyDownLoading(String URL,String morenName) {
		this.STRURL = URL;
		this.morenName = morenName;
	}

}

测试类:

package 多线程下载器;

public class TextDemo {
	public static void main(String[] args) {
		Thread thread = new Thread(new MyDownLoading("http://pic.ibaotu.com/00/63/22/31Q888piC7wM.mp4"), "A视频线程");
		Thread thread2 = new Thread(new MyDownLoading("http://pic.ibaotu.com/00/63/22/31Q888piC7wM.mp4","帅气的视频"), "B视频线程");
		thread.start();
		thread2.start();
	}
}	

 

有待完善(未分片下载).......

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值