java实现文件的断点续传的下载

java的断点续传是基于之前java文件下载基础上的功能拓展

首先设置一个以线程ID为名的下载进度文件,

每一次下载的进度会保存在这个文件中,下一次下载的时候,会根据进度文件里面的内容来判断下载的进度。

package com.ldw.multilthreaddownload;

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.URL;

public class Multidownload {

	static int ThreadCount = 3;   //线程的个数
	static int finishedThread = 0;   //初始化下载完成的线程的个数
	static String path = "http://192.168.0.102:8080/QQ.exe";  //确定下载地址
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//发送get请求,请求这个地址的资源
		try {
			URL url = new URL(path);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("GET");
			conn.setConnectTimeout(5000);
			conn.setReadTimeout(5000);
			if(conn.getResponseCode() == 200){
				//获取到请求资源文件的长度
				int length = conn.getContentLength();
				File file = new File("QQ.exe");
				//创建随机存储文件
				RandomAccessFile raf = new RandomAccessFile(file, "rwd");
				//设置临时文件的大小
				raf.setLength(length);
				//关闭raf
				raf.close();
				//计算出每一个线程下载多少字节
				
				int size = length / Multidownload.ThreadCount;
				
				for(int i = 0; i < Multidownload.ThreadCount; i ++){
					//startIndex,endIndex分别代表线程的开始和结束位置
					int startIndex = i * size;
					int endIndex = (i + 1) * size - 1;
					if(i == ThreadCount - 1){
						//如果是最后一个线程,那么结束位置写死
						endIndex = length -1;
					}
					//System.out.println("线程" + i + "的下载区间是" + startIndex + "到" + endIndex);
					new DownLoadThread(startIndex, endIndex, i).start();
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}

class DownLoadThread extends Thread{
	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(){
		//使用http请求下载安装包文件
		URL url;
		try {
			File fileProgress = new File(threadId + ".txt");
			//判断存储下载进度的临时文件是否存在,
			if(fileProgress.exists()){
				FileInputStream fis = new FileInputStream(fileProgress);
				BufferedReader br = new BufferedReader(new InputStreamReader(fis));
				//从下载进度的临时文件中读取上一次下载的总进度,然后和原来文本的开始位置相加,得到新的下载位置
				startIndex += Integer.parseInt(br.readLine());
				fis.close();
			}
			System.out.println("线程" + threadId + "下载区间是" + startIndex +"====" + endIndex);
			url = new URL(Multidownload.path);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("GET");
			conn.setConnectTimeout(5000);
			conn.setReadTimeout(5000);
			//设置请求数据的区间
			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("QQ.exe");
				RandomAccessFile raf = new RandomAccessFile(file, "rwd");
				//更新文件的写入位置,startIndex
				raf.seek(startIndex);
				while((len = is.read(b)) != -1 ){
					//每次读取流里面的数据,同步吧数据写入临时文件
					raf.write(b, 0, len);
					total += len;
					//System.out.println("线程" + threadId + "下载了" + total);
					
					//生成一个专门记录下载进度的临时文件
					//File fileProgress = new File(threadId + ".txt");
					RandomAccessFile fileProgressraf = new RandomAccessFile(fileProgress, "rwd");
					//每一次读取流里面的数据以后,把当前线程下载的总进度写入临时文件中
					fileProgressraf.write((total + "").getBytes());
					fileProgressraf.close();
					
				}
				System.out.println("线程" + threadId + "下载过程结束===========================");
				raf.close();
				//三条线程下载完成以后,清理临时文件
				Multidownload.finishedThread++;
				//线程安全
				synchronized(Multidownload.path){
					if(Multidownload.finishedThread == Multidownload.ThreadCount){
						for(int i = 0; i < Multidownload.ThreadCount; i++){
							File filefinish = new File(i + ".txt");
							filefinish.delete();
							
						}
						Multidownload.finishedThread = 0;
					}
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}

 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值