Android開發(fā)記錄-基于okhttp的斷點(diǎn)續(xù)傳

需求
基于okhttp實(shí)現(xiàn)文件斷點(diǎn)續(xù)傳

一、okhttp

省略okhttp依賴和使用方式。

二、實(shí)現(xiàn)

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;//已經(jīng)下載長(zhǎng)度
    private long totalLength = 0;//整體文件大小
    private File file;//保存文件
    private HttpDownListener mHttpDownListener;//下載進(jìn)度接口回調(diào)

    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);
        //發(fā)送請(qǐng)求
        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){
                 //
        });
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容