JAVA多线程下载,断点下载

该博客介绍了如何使用Java实现多线程断点续传下载大文件,通过设置HTTP请求的Range头信息,每个线程负责下载文件的一部分,并记录下载进度,以便在下次启动时从断点继续下载。内容包括创建临时文件,设置线程下载区间,以及处理下载中断后的恢复策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com;

/**
 * @author *cruder
 * @version 1.0
 * @since 2021/3/23 20:16
 */
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class Test{
    public static void main(String[] args){
        downlaod4();
    }

    public static void downlaod4(){
        final int THREAD_COUNT = 3;
        String path = "http://192.168.56.1:8080/DGSETUP_3054BZ6840_20200310.exe";
        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("D:\\temp.exe");
                RandomAccessFile raf = new RandomAccessFile(file,"rwd");//rw 全部写到缓存,才写到硬盘,rwd同步写到硬盘
                //设置临时未见的大小
                raf.setLength(length);
                raf.close();
                int size = length/THREAD_COUNT;
                //计算出每个线程的开始位置和结束位置
                for (int i = 0; i <THREAD_COUNT ; i++) {
                    int startIndex = i*size;
                    int endIndex = (i+1)*size - 1;
                    if(i == THREAD_COUNT-1){
                        endIndex = length - 1;
                    }
                    int finalEndIndex = endIndex;
                    new Thread(){
                        @Override
                        public void run() {

                            try {
                                URL url = new URL(path);
                                HttpURLConnection conn = (HttpURLConnection)url.openConnection();
                                conn.setRequestMethod("GET");
                                conn.setConnectTimeout(5000);
                                conn.setReadTimeout(5000);
                                conn.setRequestProperty("Range","bytes="+startIndex+"-"+ finalEndIndex);
                                if(conn.getResponseCode()==206){
                                    InputStream is = conn.getInputStream();
                                    //拿到临时文件的输出流
                                    File file = new File("D:\\temp.exe");
                                    RandomAccessFile raf = new RandomAccessFile(file,"rwd");//rw 全部写到缓存,才写到硬盘,rwd同步写到硬盘
                                    byte[] b = new byte[1024];
                                    int len = 0;
                                    int total = 0;
                                    //把文件的写入位置移动至startIndex
                                    raf.seek(startIndex);
                                    System.out.println(Thread.currentThread().getName()+"下载区间:"+startIndex+"---"+finalEndIndex);
                                    while((len = is.read(b))!=-1){
                                        //每次读取流里数据同步写入临时文件
                                        raf.write(b,0,len);
                                        total +=len;
                                        System.out.println(Thread.currentThread().getName()+"下载到"+total);
                                    }
                                    raf.close();
                                    System.out.println(Thread.currentThread().getName()+"————————————————下载完毕——————————————");
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }.start();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

在这里插入图片描述

断点下载要记录断点位置,下次开始时从断点位置开始

package com;

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

import javax.print.attribute.standard.Finishings;

public class MultiDownload {

	static int ThreadCount = 3;
	static int finishedThread = 0;
	//确定下载地址
	static String path = "http://192.168.13.13:8080/QQPlayer.exe";
	public static void main(String[] args) {
		
		//发送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("QQPlayer.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 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请求,下载原文件
		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();
			}
			System.out.println("线程" + threadId + "的下载区间是:" + startIndex + "---" + endIndex);
			HttpURLConnection conn;
			URL url = new URL(MultiDownload.path);
			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){
				//流里此时只有1/3原文件的数据
				InputStream is = conn.getInputStream();
				byte[] b = new byte[1024];
				int len = 0;
				int total = 0;
				//拿到临时文件的输出流
				File file = new File("QQPlayer.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);
					
					//生成一个专门用来记录下载进度的临时文件
					RandomAccessFile progressRaf = new RandomAccessFile(progressFile, "rwd");
					//每次读取流里数据之后,同步把当前线程下载的总进度写入进度临时文件中
					progressRaf.write((total + "").getBytes());
					progressRaf.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 f = new File(i + ".txt");
							f.delete();
						}
						MultiDownload.finishedThread = 0;
					}
				}
				
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值