断点续传 java实现

package cn.edu.ujs.multithreaddownload;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MultiDownload {
	static int ThreadCount=3;
	static int Finishedthread=0;
	//确定下载地址
	static String path="http://10.3.11.33:8080/T.exe";
	public static void main(String[] args) {

		//发送get请求,请求这个地址的资源
		try {
			URL url=new URL(path);
			HttpURLConnection conn=(HttpURLConnection) url.openConnection();
			conn.setReadTimeout(5000);
			conn.setConnectTimeout(5000);
			conn.setRequestMethod("GET");
			if(conn.getResponseCode()==200){
				//拿到资源程度
				int length=conn.getContentLength();
				File file=new File("T.exe");
				//生成临时文件
				RandomAccessFile raf=new RandomAccessFile(file, "rwd");
				//设置临时文件长度
				raf.setLength(length);
				raf.close();
				//计算出每个现成应该下载的字节数
				int size=length/ThreadCount;
				for(int i=0;i<ThreadCount;i++){
					//计算现成起止位置
					int startIndex=i*size;
					int endIndex=(i+1)*size-1;
					if(i==ThreadCount-1){
						endIndex=length-1;
					}
					System.out.println("线程"+i+"的起止位置:"+startIndex+"---"+endIndex);
					new Thread(new DownLoadThread(startIndex, endIndex, i)).start();	
				}
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
class DownLoadThread implements Runnable {

	int startIndex;
	int endIndex;
	int threadId;
	
	public DownLoadThread(int startIndex, int endIndex, int threadId) {
		super();
		this.startIndex = startIndex;
		this.endIndex = endIndex;
		this.threadId = threadId;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			File progressFile=new File(threadId+".txt");
			if(progressFile.exists()){
				FileInputStream fis=new FileInputStream(progressFile);
				BufferedReader br=new BufferedReader(new InputStreamReader(fis));
				startIndex+=Integer.parseInt(br.readLine());
				fis.close();
			}
			URL url=new URL(MultiDownload.path);
			HttpURLConnection conn=(HttpURLConnection) url.openConnection();
			conn.setRequestMethod("GET");	
			
			conn.setConnectTimeout(5000);
			conn.setReadTimeout(5000);
			//设置本次http请求所请求的数据的区间
			conn.setRequestProperty("Range", "bytes=" + startIndex + "-" + endIndex);
			//请求部分数据,响应吗是206
			if(conn.getResponseCode()==206){
				InputStream is=conn.getInputStream();			
				byte[] b=new byte[1024];
				int len=0;
				int total=0;
				//拿到临时文件的输出流
				File file=new File("T.exe");
				RandomAccessFile raf=new RandomAccessFile(file, "rwd");
				//将文件起始位置移动至startIdex
				raf.seek(startIndex);
				while((len=is.read(b))!=-1){
					//读取流数据之后,同步把数据写入临时文件
					raf.write(b,0,len);
					total+=len;
					//System.out.println("线程"+threadId+"下载了"+total);
					
					RandomAccessFile progressRaf=new RandomAccessFile(progressFile, "rwd");
					progressRaf.write((total+"").getBytes());
					progressRaf.close();
				}
				System.out.println("线程"+threadId+"下载完毕-------");
				MultiDownload.Finishedthread++;
				//删除临时文件,同步代码块
				synchronized (MultiDownload.path) {
					if(MultiDownload.Finishedthread==MultiDownload.ThreadCount){
						for(int i=0;i<MultiDownload.ThreadCount;i++){
							File dfile=new File(i+".txt");
							dfile.delete();
						}
						MultiDownload.Finishedthread=0;
					}
					
				}
				raf.close();
				
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
}

你可能感兴趣的:(java)