java学习_小程序(四线程断点网络资源下载,即时进度反馈)

/**
* 完成一个多线程断点下载工具
* 1.通过提供的网络文件地址,下载该文件
* 2.使用多个线程同时下载,并且实时更新下载进度(0%~100%)
* 3.能够实现断点功能
package com.netDemo;

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

public class MyThreadCopy extends Thread  {

	/**
	 * 完成一个多线程断点下载工具
	 * 1.通过提供的网络文件地址,下载该文件
	 * 2.使用多个线程同时下载,并且实时更新下载进度(0%~100%)
	 * 3.能够实现断点功能
	 */
	
	private String sorce ;
	private File target ;
	private long start ;
	private long end ;

	public MyThreadCopy(String sorce,File target, long start, long end) {
		this.sorce = sorce ;
		this.target = target;
		this.start = start;
		this.end = end;
	}

	@Override
	public void run() {
		InputStream input = null ;
		 RandomAccessFile output = null;
		long count = 0;
		try {
			URL url =  new URL(sorce);
			HttpURLConnection conn =  (HttpURLConnection)url.openConnection();
			conn.setRequestMethod("GET");
			conn.setReadTimeout(100); 
			input =  conn.getInputStream();
			output = new RandomAccessFile(target, "rw");
			
			long total = conn.getContentLength();
			File targetp = new File(target.getParent(),"copy");
			ProgressReturn pr = new ProgressReturn (total,Long.valueOf(getName().substring(7)),targetp,target);
			pr.start();
			input.skip(start+pr.getCurrent());
			output.seek(start+pr.getCurrent());
			byte[] b = new byte[1024];
			int len = 0;
			while ((len = input.read(b)) != -1 && count <= (end - start)) {
				sleep(1000);
				output.write(b, 0, len); 
				count += len;
				pr.setCurrent(count);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (output != null)
					output.close();
				if (input != null)
					input.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) throws IOException {
		String sorce = "http://img.taopic.com/uploads/allimg/131117/234805-13111FZ25393.jpg";  
		URL url =  new URL(sorce);
		HttpURLConnection conn =  (HttpURLConnection)url.openConnection();
		conn.setRequestMethod("GET");
		conn.setReadTimeout(10000);
		int code =	conn.getResponseCode();
		if(code == HttpURLConnection.HTTP_OK){
			 long length = conn.getContentLength();
			 long needToCopy = length/4 ;
			 File target = new File ("C:\\Users\\ddf\\Desktop\\test",sorce.substring(sorce.lastIndexOf("/")));//新生成的目标文件 路径
			 for(int i = 0 ;i < 4 ;i++){
				 new MyThreadCopy(sorce,target,i*needToCopy,(i+1)*needToCopy).start();
			 }
		}
	}
}


package com.netDemo;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class ProgressReturn extends Thread  {
	private long total ;	//源文件要拷贝的总字节数目
	private long current ;//记录当前所有线程共写了多少字节
	private long index ;	//当前是哪个线程 由 thread.getId()传值;
	private File targetP ;	//记录文件
	private long ctotal ;	//当前四个进程一共写了多少
	private File target ;   //要写入的目标文件
	
	
	public ProgressReturn(long total, long index, File targetP,File target) {
		super();
		this.total = total;
		this.index = index;
		this.targetP = targetP;
		this.target = target;
	}

	public  long getCurrent(){
		return current; // 向外提供每个线程当前拷贝字节数 ,用于断点拷贝
	}
	public void setCurrent(long current){
		this.current = current;
	}
	
	
	@Override
	public void run() {
		while(ctotal != total)
				{	
			try {
					RandomAccessFile raf = new RandomAccessFile(targetP,"rw");
					raf.seek(index*8);
					raf.writeLong(current);	
					ctotal = target.length(); 
					System.out.println("当前总进程拷贝了:"+ctotal*1.0/total*100+"%");
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
	
		}
		System.out.println("拷贝100%,完成");
		if(	ctotal == total) {
			if(targetP.exists())	
			{
				targetP.delete();
			}
			System.out.println("已删除记录文件");
		}
	
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值