多线程断点文件拷贝

package threadFileCopy;

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

public class Test {
	public static void main(String[] args) throws IOException {
		String s = "http://img5.duitang.com/uploads/item/201512/15/20151215223141_icL2m.jpeg";
		URL url = new URL(s);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setRequestMethod("GET");
		conn.setReadTimeout(500);
		long len = conn.getContentLength();
		System.out.println("响应数据的字节数:" + len);
		int code = conn.getResponseCode();
		if (code == HttpURLConnection.HTTP_OK) {
			File dir = new File("C:\\Users\\rhs\\Desktop", s.substring(s.lastIndexOf("/")));
			long l = len / 4;
			int count = 4;
			for (int i = 0; i < count; i++) {
				FileCopy fc = null;
				if (i == count - 1) {
					fc = new FileCopy(url, dir, i * l, len);
				} else {
					fc = new FileCopy(url, dir, i * l, (i + 1) * l - 1);
				}
				fc.start();
			}

			Schedule sd = new Schedule(len, dir);
			sd.setDaemon(true);
			sd.start();
		}
	}
}
package threadFileCopy;

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

public class FileCopy extends Thread {
	private URL url;
	private File dir;
	private long start;
	private long end;
	private File file = new File(this.getName() + ".txt");

	public FileCopy(URL url, File dir, long start, long end) {
		super();
		this.url = url;
		this.dir = dir;
		this.start = start;
		this.end = end;

	}

	@Override
	public void run() {

		RandomAccessFile write = null;
		RandomAccessFile write1 = null;
		RandomAccessFile read = null;
		InputStream is = null;
		try {
			if (!file.exists()) {
				write1 = new RandomAccessFile(file, "rw");
				write1.writeLong(start);
			}
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("GET");
			conn.setReadTimeout(500);
			write = new RandomAccessFile(dir, "rw");
			is = conn.getInputStream();

			write1 = new RandomAccessFile(file, "rw");
			read = new RandomAccessFile(file, "r");
			start = read.readLong();
			System.out.println(start);
			is.skip(start);
			write.seek(start);
			;

			byte[] b = new byte[1024];
			int len = 0;
			while (((len = is.read(b)) != -1) && start < end) {
				write.write(b, 0, len);
				start += len;
				write1.seek(0);
				write1.writeLong(start);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				write.close();
				is.close();
				sleep(100);
			} catch (IOException e) {
				e.printStackTrace();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
package threadFileCopy;

import java.io.File;
import java.text.DecimalFormat;

public class Schedule extends Thread {
	private long len;
	private File dir;

	public Schedule(long len, File dir) {
		super();
		this.len = len;
		this.dir = dir;
	}

	@Override
	public void run() {
		DecimalFormat df = new DecimalFormat("##.##%");
		long length = len;
		long nowlength = 0;
		while (nowlength < length) {
			nowlength = dir.length();
			System.out.println(df.format(nowlength * 1.0 / length));
			try {
				sleep(500); 
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}







                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值