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

完整版传送门

自定义线程类:

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();
	}
}	

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

猜你喜欢

转载自blog.csdn.net/acDream_/article/details/81610795
今日推荐