OkHttp3断点下载

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Zone_Q/article/details/78920204
一、流程
1,与服务器建立请求,获取到该文件的大小,然后断开连接;
2,创建一个.tem文件来存储该下载的文件;
3,创建一个RadomAccessFile文件,并获取到已下载的文件的大小;
4,建立请求,传入需要下载的文件的区间,申请连接;
5,请求成功(如果服务器支持断点下载,则Response返回的code==206);
6,通过RandomAccessFile移动指针到已下载的位置,然后开始写入数据到本地;
二、RandomAccessFile
1,总体概念:RandomAccessFile类的主要功能是完成随机读取功能,可以读取指定位置的内容。之前的File类只是针对文件本身进行操作的,而如果要想对文件内容进行操作,则可以使用RandomAccessFile类,此类属于随机读取类,可以随机读取一个文件中指定位置的数据;
2,常用方法:
1)public  RandomAccessFile(File file, String mode)throws FileNotFoundException  构造方法  接收File类的对象,指定操作路径,但是在设置时需要设置模式:"r": 只读、"w": 只写、"rw": 读写。
(2)、public RandomAccessFile(String name, String mode) throws FileNotFoundException 构造方法 不再使用File类对象表示文件,而是直接输入了一个固定的文件路径。
(3)、public void close() throws IOException   关闭操作
(4)、public int read(byte[] b) throws IOException 将内容读取到一个byte数组之中
(5)、public final byte readByte()  throws IOException 读取一个字节
(6)、public final int readInt()  throws IOException从文件中读取整型数据。
(7)、public void seek(long pos) throws IOException 设置读指针的位置。
(8)、public final void writeBytes(String s) throws IOException 将一个字符串写入到文件之中,按字节的方式处理。
(9)、public final void writeInt(int v) throws IOException 将一个int型数据写入文件,长度为4位。
(10)、public int skipBytes(int n) throws IOException 指针跳过多少个字节。
(11)、public void seek(long offset) throws IOException 移动指针到文件的某个位置
(12)、构造:public RandomAccessFile(File file, String mode)  throws FileNotFoundException实例化此类的时候需要传递File类,告诉程序应该操作的是哪个文件,之后有一个模式,文件的打开模式,常用的两种模式:
r:读模式
w:只写
rw:读写,如果使用此模式,如果此文件不存在,则会自动创建。
三、相关代码
1,向服务器获取文件大小的代码:
private long getContentLength(String url) {
    Request request = new Request.Builder().url(url).build();
    Call call = client.newCall(request);
    try {
        Response response = call.execute();
        if (response != null && response.body() != null && response.code() == 200) {
            long length = response.body().contentLength();
            response.close();
            return length;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return -1;
}
    2、文件下载代码
private void downLoad(String url) {
    Request request = null;//请求体
    Call call = null;
    File file = null;//文件保存目录
    Response response = null;//请求响应
    InputStream inputStream = null;
    RandomAccessFile accessFile = null;//随机文件,可以自由移动文件开始位置
    long startIndex = 0;//文件下载开始位置
    try {
        file = new File(context.getExternalCacheDir(), Md5Utils.md5(url) + ".tmp");
        accessFile = new RandomAccessFile(file, "rwd");
        if (file.exists()) { //如果当前文件夹存在
            long read = accessFile.length();//获取已经下载文件的大小
            startIndex = read;
        }
        long total = getContentLength(url);
        Log.e("STARTINDEX", startIndex + "  total: " + total);
        if (startIndex == total) {
            return;
        }
        request = new Request.Builder().header("RANGE", "bytes=" + startIndex + "-" + total).url(url).build();
        call = client.newCall(request);
        response = call.execute();
        if (response != null && response.body() != null && response.code() == 206) {//206表示服务器支持断点续传
            inputStream = response.body().byteStream();
            accessFile.seek(startIndex);//移动到开始下载得位置
            byte[] blocks = new byte[50 * 1024];
            int len = -1;
            while ((len = inputStream.read(blocks)) > 0) {
                accessFile.write(blocks, 0, len);
            }
            accessFile.close();
            inputStream.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}



猜你喜欢

转载自blog.csdn.net/Zone_Q/article/details/78920204