美文网首页
Android开发记录-基于okhttp的断点续传

Android开发记录-基于okhttp的断点续传

作者: LH_1994 | 来源:发表于2020-06-17 11:20 被阅读0次

    需求
    基于okhttp实现文件断点续传

    一、okhttp

    省略okhttp依赖和使用方式。

    二、实现

    1、interface

    import java.io.IOException;
    import okhttp3.Call;
    import okhttp3.Response;
    
    public interface HttpDownloadListener{
        void onFailure(Call call, IOException e,long totalLength, long downloadLength);
        void inProgress(Call call, Response response, long totalLength, long downloadLength);
        void onResponse(Call call, Response response, long totalLength, long downloadLength);
    }
    

    2、OkHttpDownloadUtil

    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.RandomAccessFile;
    import okhttp3.Call;
    import okhttp3.Callback;
    import okhttp3.OkHttpClient;
    import okhttp3.Request;
    import okhttp3.Response;
    import okhttp3.ResponseBody;
    
    
    public class OkHttpDownloadUtil {
        private final String TAG = this.getClass().getSimpleName();
        private Call mCall;
        private String downloadUrl;//url
        private long downloadLength = 0;//已经下载长度
        private long totalLength = 0;//整体文件大小
        private File file;//保存文件
        private HttpDownListener mHttpDownListener;//下载进度接口回调
    
        public void getDownloadRequest(final String downloadUrl, final File file, Long already,Long total, final HttpDownloadListener listener){
            this.downloadUrl = downloadUrl;
            this.downloadLength = already;
            this.totalLength = total;
            this.file = file;
            this.mHttpDownListener = listener;
    
            OkHttpClient okHttpClient = new OkHttpClient();
            Request request = new Request.Builder()
                    .url(downloadUrl)
                    .header("RANGE", "bytes=" + downloadLength + "-")
                    .build();
            mCall = okHttpClient.newCall(request);
            //发送请求
            mCall.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    if (mHttpDownListener != null) {
                        mHttpDownListener.onFailure(call, e,totalLength, downloadLength);
                    }
                }
                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    ResponseBody responseBody = response.body();
                    InputStream inputStream = responseBody.byteStream();
                    RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
                    if (totalLength == 0){
                        totalLength = responseBody.contentLength();
                        randomAccessFile.setLength(totalLength);
                    }
                    if (downloadLength != 0){
                        randomAccessFile.seek(downloadLength);
                    }
                    byte[] bytes = new byte[2048];
                    int len = 0;
                    try {
                        while ((len = inputStream.read(bytes)) != -1) {
                            randomAccessFile.write(bytes,0,len);
                            downloadLength = downloadLength + len;
                            if (mHttpDownListener != null) {
                                mHttpDownListener.inProgress(call, response, totalLength, downloadLength);
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        downloadLength = randomAccessFile.getFilePointer();
                        randomAccessFile.close();
                        inputStream.close();
                        if (mHttpDownListener != null) {
                            mHttpDownListener.onResponse(call, response, totalLength, downloadLength);
                        }
                    }
    
                }
            });
        }
    
        /**
         * 停止下载
         */
        public void stopDownload(){
            if (mCall!=null){
                mCall.cancel();
            }
        }
    
    }
    

    3、使用

    OkHttpDownUtil okHttpDownUtil = new OkHttpDownUtil();
            okHttpDownUtil.getDownloadRequest(url, file, already,total,new HttpDownloadListener() {
                @Override
                public void onFailure(Call call, IOException e,long mTotalLength,long mAlreadyDownLength) {
                     //
                }
    
                @Override
                public void inProgress(Call call, Response response, long mTotalLength, long mAlreadyDownLength) {
                     //
                }
    
                @Override
                public void onResponse(Call call, Response response, long mTotalLength, long mAlreadyDownLength){
                     //
            });
    

    相关文章

      网友评论

          本文标题:Android开发记录-基于okhttp的断点续传

          本文链接:https://www.haomeiwen.com/subject/vkghxktx.html