利用线程将网络指定的图片下载到本地,将文件分四次下载,结果并未实现QAQ求大神指教下

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;

public class FileDownload extends Thread{
	private static String source;
	private static File target;
	private long start;
	private long end;
	static HttpURLConnection con; 
	static URL url;
	public FileDownload(String source, File target, long start, long end) {
		super();
		this.source = source;
		this.target = target;
		this.start = start;
		this.end = end;
	}	
	@Override
	public void run() {
		int count = 0;
		InputStream input = null;
		RandomAccessFile output = null;
		long i = 0 ;
		try {
			int code = con.getResponseCode();
			if(code == HttpURLConnection.HTTP_OK){
			input = con.getInputStream();
			output = new RandomAccessFile(new File(target,"plmm.jpg"), "rw");
//			input.skip(start);
			output.seek(start);
			byte[] b = new byte[128];
			int len = 0;
			while((len = input.read(b))!=-1&&count<=(end - start)){
				count += len ;
				output.write(b, 0, len);
				i = output.getFilePointer();
			}
		} 
	}	
		catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
					if(input != null)input.close();
					if(output != null)output.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		
		
}
	public static void main(String[] args) throws IOException {
		source = "http://192.168.46.254:8888/easyBuy/images/plmm.jpg";
		target = new File("C:\\Documents and Settings\\Administrator\\桌面");
		url = new URL(source);
		con = (HttpURLConnection)url.openConnection();
		long item = con.getContentLength()/4;
		for(int i = 0;i < 4 ; i++){
			new FileDownload(source,target,i*item,(i+1)*item).start();
		}
	}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值