从此
文章
📄文章 #️⃣专题 🌐上网 📺 🛒 📱

Java RandomAccessFile多线程断点续传(大文件分段下载)原理和实现

🕗2020-08-09
        我们平常创建流对象关联文件,开始读文件或者写文件都是从头开始的,不能从中间开始,如果是开多线程下载一个文件我们之前学过的FileWriter或者FileReader等等都无法完成,而当前介绍的RandomAccessFile他就可以解决这个问题,因为它可以指定位置读,指定位置写的一个类,通常开发过程中,多用于多线程下载一个大文件。
 
  • 怎么断点续传?
    两点:
    1、网络数据(可以设置从文件的哪个位置下载)
    conn.setRequestProperty(“Range”, “bytes=”+startPos+”-“+endPos);
    2、写入文件(可以设置从本地文件哪个位置写入)
    使用RandomAccessFile.seek

 

  • 单个文件怎么分段下载?

         得到文件的总长度,把长度分为N个线程进行分开下载

 

 

RandomAccessFile 实现断点续传:

断点 : 当前线程已经下载完成的数据长度。
续传 : 向服务器请求上次线程停止位置之后的数据。
每当线程停止时就把已下载的数据长度写入记录文件,
当重新下载时,从记录文件读取已经下载了的长度。而这个长度就是所需要的断点

续传的实现也简单,可以通过设置网络请求参数,请求服务器从指定的位置开始读取数据。
而要实现这两个功能只需要使用到httpURLconnection里面的setRequestProperty方法便可以实现

如下所示,便是向服务器请求500-1000之间的500个byte:

conn.setRequestProperty("Range", "bytes=" + 500 + "-" + 1000);

 

以上只是续传的一部分需求,当我们获取到下载数据时,还需要将数据写入文件,
而普通发File对象并不提供从指定位置写入数据的功能,这个时候,就需要使用到
RandomAccessFile来实现从指定位置给文件写入数据的功能

如下所示,便是从文件的的第100个byte后开始写入数据。

raFile.seek(100);

 

开始写入数据时还需要用到RandomAccessFile里面的另外一个方法

public void write(byte[] buffer, int byteOffset, int byteCount)

 

该方法的使用和OutputStream的write的使用一模一样…
以上便是断点续传的原理

具体代码:

                URL url = new URL(threadInfo.getUrl());
                connection = (HttpURLConnection) url.openConnection();
                connection.setConnectTimeout(5000);
                connection.setRequestMethod("GET");

                int start = threadInfo.getStart() + threadInfo.getFinished();
                //设置范围
                connection.setRequestProperty("Range", "bytes=" + start + "-" + threadInfo.getEnd());

                //设置文件写入位置
                File file = new File(DownLoadService.DOWNLOAD_PATH, fileInfo.getFileName());
                randomAccessFile = new RandomAccessFile(file, "rwd");
                randomAccessFile.seek(start);

                //暂停之前的数据进行累加
                currentProgress += threadInfo.getFinished();

 

代码中重要的2个方法是

//设置开始和结束的范围,每次暂停后,从上一次的进度开始下载
connection.setRequestProperty("Range", "bytes=" + start + "-" + threadInfo.getEnd());

 

//从指定位置进行下载
 randomAccessFile.seek(start);

 

多线程对大文件进行分段下载:

多线程断点续传是把整个文件分割成几个部分,每个部分由一条线程执行下载,而每一条下载线程都要实现断点续传功能。
为了实现文件分割功能,我们需要使用到httpURLconnection的另外一个方法:

public int getContentLength()

 

当请求成功时,可以通过该方法获取到文件的总长度。 每一条线程下载大小 =
fileLength / THREAD_NUM

在多线程断点续传下载中,有一点需要特别注意: 由于文件是分成多个部分是被不
同的线程的同时下载的,这就需要,每一条线程都分别需要有一个断点记录,和一
个线程完成状态的记录;

关键代码:

    //线程数量
    private int mThreadCount = 3;
    //下载的文件的总长度
    private int length ;

        //多线程下载
        //获得每个线程下载长度
        int childLength = length / mThreadCount;
        //线程一:0,childLength 
        //线程二:childLength, childLength*2
        //线程三:childLength*2,childLength*3
        int start = childLength * i;
        int end = (i + 1) * childLength - 1;

        for (int i = 0; i < mThreadCount; i++) {
            ThreadInfo threadInfo = new ThreadInfo(i, fileInfo.getUrl(), start , end , fileInfo.getFinished());
            //最后一个除不尽的情况
            if (i == mThreadCount - 1) {
                threadInfo.setEnd(fileInfo.getLength());
            }
            //在循环中直接开启线程进行下载
            DownloadThread downloadThread = new DownloadThread(threadInfo);
            downloadThread.start();

        }



//实体类
public class ThreadInfo implements Serializable{

    public static final String THREAD_INFO = "thread_info";

    private int id;
    //下载的URL
    private String url;
    //下载开始节点
    private int start;
    //下载结束节点
    private int end;
    //当前完成进度
    private int finished;

        public ThreadInfo(int id, String url, int start, int end, int finished) {
        this.id = id;
        this.url = url;
        this.start = start;
        this.end = end;
        this.finished = finished;
    }