java 多线程下载

package com.test;

import java.io.IOException;

public class Main {

//	public static final String ADD = "http://mat1.gtimg.com/www/iskin960/s01/s_b_1.1.0.png";
//	public static final String ADD = "http://mat1.gtimg.com/www/iskin960/logo_soso.png";
//	public static final String ADD = "http://ossweb-img.qq.com/upload/qqgg/dancer/1305786311_1837963180_10218_sPicName.jpg";
//	public static final String ADD = "http://dl_dir.qq.com/qqfile/qq/QQ2011/QQ2011Beta2.exe";
	public static final String FILE = "jay_picture_90.jpg";
	public static final String ADD = "http://www.ppig.com.cn/images/jay_picture_90.jpg";

	public static void main(String[] args) throws IOException {
		DownMain main=new DownMain(ADD,4);//package com.test;

import java.io.IOException;

public class Main {

//	public static final String ADD = "http://mat1.gtimg.com/www/iskin960/s01/s_b_1.1.0.png";
//	public static final String ADD = "http://mat1.gtimg.com/www/iskin960/logo_soso.png";
//	public static final String ADD = "http://ossweb-img.qq.com/upload/qqgg/dancer/1305786311_1837963180_10218_sPicName.jpg";
//	public static final String ADD = "http://dl_dir.qq.com/qqfile/qq/QQ2011/QQ2011Beta2.exe";
	public static final String FILE = "jay_picture_90.jpg";
	public static final String ADD = "http://www.ppig.com.cn/images/jay_picture_90.jpg";

	public static void main(String[] args) throws IOException {
		DownMain main=new DownMain(ADD,4);//参数1,下载地址,参数2:用多少线程
		main.download();
	}

}

		main.download();
	}

}
 

闲来无事 搞下java多线程下载,发发代码,以后改进改进

 

下载线程类:

package com.test;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Properties;

public class DownTread implements Runnable {

	private String address;//链接地址
	
	private long start;//开始字节
	
	private long end;//结束字节数
	
	private ArrayList<DownData> data;//下载的文件数据
	
	private CallBack callBack;//下载完成,回调写入下载容
	
	private int cache;//缓存
	
	public DownTread(String address,long start,long end,CallBack callBack){
		this.address=address;
		this.start=start;
		this.end=end;
		this.callBack=callBack;
		Properties properties=new Properties();
		try {
			properties.load(this.getClass().getResourceAsStream("/sys.properties"));//读取配置文件的缓存
			cache=Integer.parseInt(properties.getProperty("cache"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	@Override
	public void run() {
		try {
			callBack.befor(this);
			down();
		} catch (IOException e) {
			e.printStackTrace();
			callBack.error(this);
		}
		callBack.allFinish(this);
	}
	
	private void down()throws IOException{
		URL url=new URL(address);
		HttpURLConnection conn=(HttpURLConnection) url.openConnection();
		conn.setUseCaches(false);
		conn.setDoInput(true);
		conn.setDoOutput(false);
		conn.setRequestMethod("GET");
		conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1");
		conn.setRequestProperty("Range", "bytes="+start+"-"+end);
		conn.connect();
		System.out.println("线程:"+Thread.currentThread().getName()+conn.getHeaderFields().get("Content-Range"));
		data=new ArrayList<DownData>();
		InputStream bins=conn.getInputStream();
		byte [] bs=new byte[cache];
		long len=end-start+1;
		int dlen=0;
		long dstart=start;
		while((dlen=bins.read(bs))!=-1){
			if(dlen<len){
				data.add(new DownData(dstart,bs, dlen));
				len-=dlen;
				dstart+=dlen;
			}
			else {
				data.add(new DownData(dstart,bs, len));
				dstart+=len;
			}
			if(DownMain.isWriteing){
				callBack.finish(this);
				data.clear();
			}
			bs=new byte[cache];
		}
		bins.close();
		conn.disconnect();
		
	}

	public long getStart() {
		return start;
	}

	public long getEnd() {
		return end;
	}

	public ArrayList<DownData> getData() {
		return data;
	}
	
	interface CallBack {

		void befor(DownTread thread);
		
		void finish(DownTread downTread);//读取到一次数据
		
		void allFinish(DownTread thread);//该线程分配的任务下载完成
		
		void error(DownTread thread);//出现错误
	}
}

 下载的数据包

package com.test;


public class DownData {

	private long start;//该段数据在总文件中的位置
	
	private long len;
	
	private byte [] data;
	
	public DownData(long start,byte []data,long len){
		this.start=start;
		this.data=data;
		this.len=len;
	}

	public long getLen() {
		return len;
	}

	public byte[] getData() {
		return data;
	}

	public long getStart() {
		return start;
	}
	
	
}

 下载主线程类:管理,把下载的文件数据写入文件

package com.test;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;

import com.test.DownTread.CallBack;

public class DownMain implements CallBack {

	private String address;

	private RandomAccessFile af;
	
	public static boolean isWriteing=true;
	
	private int dts=0;
	
	private int thread;
	
	public DownMain(String address,int thread) {
		this.address = address;
		this.thread=thread;
	}
	

	public void download() throws IOException {
		System.out.println("开始下载文件");
		URL url = new URL(this.address);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setUseCaches(false);
		conn.setDoInput(true);
		conn.setDoOutput(false);
		conn.setRequestMethod("GET");
		conn.setConnectTimeout(20000);
		conn.setRequestProperty("User-Agent",
						"Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1");
		System.out.println("开始连接文件");
		conn.connect();
		System.out.println(conn.getHeaderFields());
		if(conn.getResponseCode()==HttpURLConnection.HTTP_NOT_FOUND){
			System.out.println("找不到文件,连接失败!");
			return;
		}
		
		System.out.println("连接成功");
		System.out.println("文件大小:" + conn.getContentLength() + "字节");
		System.out.println("文件类型:" + conn.getContentType());
		long fileLength = conn.getContentLength();
		conn.disconnect();
		File file = new File("d:\\jay\\" + Main.FILE);
		if (!file.exists()) {
			file.createNewFile();
		} else {
			file.delete();
			file.createNewFile();
		}
		af = new RandomAccessFile(file, "rw");
		long unit = fileLength / thread;
		long start = 0;
		long[][] task = new long[thread][2];
		for (int i = 0; i < thread; i++) {
			task[i][0] = start;
			start += unit;
			task[i][1] = start - 1;
			if (i == thread-1 && task[i][1] < fileLength) {
				task[i][1] = fileLength;
			}
		}
		/**/
		System.out.println("开始分段下载");
		for (int i = 0; i < thread; i++) {
			new Thread(new DownTread(address, task[i][0], task[i][1], this),"线程"+i).start();
			start += unit;
			dts++;
		}
	}

	@Override
	public void finish(DownTread down) {
		try {
			synchronized (af) {
				isWriteing=false;
				for (DownData d : down.getData()) {
					af.seek(d.getStart());
					System.out.print("线程"+Thread.currentThread().getName()+"写入位置:"+af.getFilePointer());
					af.write(d.getData(), 0, (int)d.getLen());
					System.out.println("-"+af.getFilePointer());
				}
				System.gc();
				isWriteing=true;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	@Override
	public void allFinish(DownTread thread) {
		System.out.println("线程:"+Thread.currentThread().getName()+"开始完成");
		synchronized (af) {
			dts--;
			if(dts==0){
				try {
					finish();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}


	@Override
	public void befor(DownTread thread) {
		System.out.println("线程:"+Thread.currentThread().getName()+"开始下载");
	}


	@Override
	public void error(DownTread thread) {
		System.out.println("线程:"+Thread.currentThread().getName()+"出现错误");
	}
	
	
	private void finish() throws IOException{
		System.out.println("全部下载完成!");
		af.close();
		
	}
}

 

 

使用方法:

 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值