RandomAccessFile断点续传和多线程断点续传

275 阅读4分钟

本文已参与[新人创作礼]活动,一起开启掘金创作之路

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第3天,点击查看活动详情

  • 怎么断点续传? 两点: 1、网络数据(可以设置从文件的哪个位置下载) conn.setRequestProperty("Range", "bytes="+startPos+"-"+endPos);
    2、写入文件(可以设置从本地文件哪个位置写入) 使用RandomAccessFile.seek

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

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

1、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);

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

多线程断点续传是把整个文件分割成几个部分,每个部分由一条线程执行下载,而每一条下载线程都要实现断点续传功能。 为了实现文件分割功能,我们需要使用到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;
    }

总结:

真正实现的时候最好使用: 1、线程池控制多个线程 2、采用同步数据库方法 3、采用Service中启动线程下载

为什么要在Service中做下载,而不在Activity中下载?

Activity是一个前台组件,可能会被关闭,也可能会被android系统回收。如果activity关闭了,在activity中创建线程就不好管理了,没法停止和其他操作。

Service属于后台组件,用户没法去关闭,优先级高,一般Android系统不会去回收的。 线程的启动关闭在Service中是比较保险的。