首页>代码>利用java swing多线程实现的下载器,完爆迅雷!>/DowmloadURLFile/src/download/multithread/GetFileDispatchThread.java
package download.multithread;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import download.ui.GUI;


public class GetFileDispatchThread extends Thread{

	//网络文件地址
	private String urlFile;
	
	//指定同时下载的线程数
	private int threadNum;
	
	//下载文件要存放的本地路径地址
	private String localFileAddress;
	
	//每个线程要下载的文件分块的大小
	private long[] eachThreadLength;
	
	//网络文件的大小
	private long utlFileLength;
	
	//各线程执行结果状态,结束为true
	public static boolean[] checkList;
	
	//类构造函数
	public GetFileDispatchThread(String urlFile, int threadNum, String localFileAddress) {
		// TODO Auto-generated constructor stub
		
		this.urlFile = urlFile;
		this.threadNum = threadNum;
		this.localFileAddress = localFileAddress;
	}
	
	//设定每个线程文件最终要写的文件的大小
	private void setEachThreadLength(){
		
		this.eachThreadLength = new long[this.threadNum];
		
		long length = this.utlFileLength/this.threadNum;
		log("每个线程下载文件大小=" + length);
		log("urlFileLenfth=" + this.utlFileLength + "&length=" + length);
		for(int i=0;i<this.threadNum-1;i++){
			this.eachThreadLength[i] = length;
		}
		this.eachThreadLength[this.threadNum-1] = this.utlFileLength%this.threadNum+length;	
	}
	
	//获取文件的名称 - 从URL对象
	private String getFileName(String pathAndFile){
		return pathAndFile.substring(pathAndFile.lastIndexOf("/")+1);
	}
	
	//线程启动后调用的方法
	public void run(){
		
		if(new File(this.localFileAddress + "\\" + this.getFileName(urlFile)).exists()){
			log("文件存在,退出下载");
			GUI.showLog("文件存在,退出下载");
			GUI.setBgRunning(false);
			return;
		}
		
		//创建临时文件夹 - tmp
		String tmpDirectory = this.localFileAddress + "tmp";
		new File(tmpDirectory).mkdir();
		
		try {
			//获取所需下载网络资源的连接
			URLConnection con = new URL(this.urlFile).openConnection();
			log("连接成功");
			GUI.showLog("网络资源连接成功");
			
			//获取所需下载网络资源文件大小
			this.utlFileLength = con.getContentLength();
			log("获取资源大小=" + this.utlFileLength);
			
			//获取网络资源文件在web服务器上的路径及文件名
			String filename = tmpDirectory + "\\" + this.getFileName(urlFile);
			log("临时目录=" + filename);
			
			//初始化每个线程下载内容大小
			this.setEachThreadLength();
			log("初始化每个线程下载大小完成");
			
			GUI.showLog("启动下载线程");
			
			//获取下载线程信息 - [startPos,endPos)
			this.checkList = new boolean[this.threadNum];
			log("启动下载线程:" + this.urlFile + "&" + 0+ "&" + (this.eachThreadLength[0]-1)+ "&" +  (filename+".part"+0) + "&" +  0);			
			new DownloadFileThread(this.urlFile, 0, (this.eachThreadLength[0]-1), filename+".part0", 0).start();
			checkList[0] = false;
			for(int i=1;i<this.threadNum;i++){
				
				long pos = 0;
				for(int j=0;j<i;j++){
					pos += this.eachThreadLength[j];
				}
				log("启动下载线程:" + this.urlFile + "&" + (pos)+ "&" + (pos+this.eachThreadLength[i]-1)+ "&" +  (filename+".part"+i) + "&" +  i);
				new DownloadFileThread(this.urlFile, pos, pos+this.eachThreadLength[i]-1, filename+".part"+i, i).start();
				checkList[i] = false;
			}
			
			//启动文件下载监视线程 - 此时调度结束 - 监视目标为checkList对象
			log("启动下载监听线程");
			GUI.showLog("启动下载监听线程");
			new MonitorThread(this.threadNum, this.localFileAddress, tmpDirectory).start();
			
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			GUI.showLog("资源URL非法");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			GUI.showLog("出错了 ...");
		}
	}

	/*
	 * 日志
	 */
	private boolean showLog = false;
	
	public void log(String msg){
		if(this.showLog)System.out.println(msg);
	}
	
//	/**
//	 * @param args
//	 */
//	public static void main(String[] args) {
//		// TODO Auto-generated method stub
//		new GetFileDispatchThread("http://192.168.28.121/eclipse.rar", 10, "E:\\").start();
//	}
}
最近下载更多
snowing_for  LV9 2024年11月25日
刘先生-OL  LV13 2024年11月25日
刘孟飞  LV22 2024年6月21日
愚人劫  LV1 2023年5月30日
Xgcxgc  LV1 2023年3月28日
MoonSight  LV1 2022年7月1日
liys1234  LV9 2022年4月25日
bai620123  LV16 2022年4月16日
一个好人520  LV10 2021年9月29日
qq8945051  LV1 2021年9月11日
最近浏览更多
snowing_for  LV9 2024年11月25日
刘先生-OL  LV13 2024年11月25日
刘孟飞  LV22 2024年6月21日
pangzhihui  LV14 2023年12月14日
愚人劫  LV1 2023年5月30日
Xgcxgc  LV1 2023年3月28日
xingxing1234  LV10 2023年3月22日
liuind 2023年3月17日
暂无贡献等级
MoonSight  LV1 2022年7月1日
fantaohaofan  LV2 2022年6月23日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友