Retrofit下载大文件、监听进度、断点续传

前些天有个同学问我会不会使用Retrofit下载大文件,我就给了他我项目中使用的方法。发现有很多人还不会用Retrofit实现下载文件,即使会下载,也可能会出现问题,比如:不知道如何获取进度;一旦下载大文件就会OOM;不知道如何暂停下载,或者不知道如何实现断点续传等。今天这个demo就是实现上面的几个问题,使用Retrofit+Rxjava来实现,先看效果图:

                                                  

具体的实现逻辑:

1.自定义ResponseBody

public class DownloadResponseBody extends ResponseBody {

    private ResponseBody responseBody;
    private DownloadProgressListener listener;
    private BufferedSource bufferedSource;

    public DownloadResponseBody(ResponseBody responseBody, DownloadProgressListener listener) {
        this.responseBody = responseBody;
        this.listener = listener;
    }

    @Override
    public MediaType contentType() {
        return responseBody.contentType();
    }

    @Override
    public long contentLength() {
        return responseBody.contentLength();
    }

    @Override
    public BufferedSource source() {
        if (bufferedSource == null) {
            bufferedSource = Okio.buffer(source(responseBody.source()));
        }
        return bufferedSource;
    }

    private Source source(Source source) {
        return new ForwardingSource(source) {
            long totalBytesRead = 0L;

            @Override
            public long read(Buffer sink, long byteCount) throws IOException {
                long bytesRead = super.read(sink, byteCount);
                // read() returns the number of bytes read, or -1 if this source is exhausted.
                totalBytesRead += bytesRead != -1 ? bytesRead : 0;
                if (null != listener) {
                    listener.progress(totalBytesRead, responseBody.contentLength(), bytesRead == -1);
                }
                return bytesRead;
            }
        };
    }
}

2.进度回调监听:

public interface DownloadProgressListener {

    /**
     * @param read 已下载长度
     * @param contentLength 总长度
     * @param done 是否下载完毕
     */
    void progress(long read, long contentLength, boolean done);

}

3.准备Service

public interface DownLoadService {
    /**
     * @param start 从某个字节开始下载数据
     * @param url   文件下载的url
     * @return Observable
     * @Streaming 这个注解必须添加,否则文件全部写入内存,文件过大会造成内存溢出
     */
    @Streaming
    @GET
    Observable<ResponseBody> download(@Header("RANGE") String start, @Url String url);
}

4.准备进度拦截器

public class DownloadInterceptor implements Interceptor {

    private DownloadProgressListener listener;

    public DownloadInterceptor(DownloadProgressListener listener) {
        this.listener = listener;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Response originalResponse = chain.proceed(chain.request());

        return originalResponse.newBuilder()
                .body(new DownloadResponseBody(originalResponse.body(), listener))
                .build();
    }
}

5.准备下载的文件对应的JavaBean对象(这里省略set、get方法)

public class DownloadInfo {

    /* 存储位置 */
    private String savePath;
    /* 文件总长度 */
    private long contentLength;
    /* 下载长度 */
    private long readLength;
    /* 下载该文件的url */
    private String url;
    private DownLoadService service;
}

6.准备下载的管理类,下载任务在这里进行

public class DownloadManager implements DownloadProgressListener {

    private DownloadInfo info;
    private ProgressListener progressObserver;
    private File outFile;
    private Subscription subscribe;
    private DownLoadService service;
    private long currentRead;

    private DownloadManager() {
        info = new DownloadInfo();
        outFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "yaoshi.apk");
        info.setSavePath(outFile.getAbsolutePath());
    }

    public static DownloadManager getInstance() {
        return Holder.manager;
    }

    public static class Holder {
        private static DownloadManager manager = new DownloadManager();
    }

    @Override
    public void progress(long read, final long contentLength, final boolean done) {
        Log.e("progress : ", "read = " + read + "contentLength = " + contentLength);
        // 该方法仍然是在子线程,如果想要调用进度回调,需要切换到主线程,否则的话,会在子线程更新UI,直接错误
        // 如果断电续传,重新请求的文件大小是从断点处到最后的大小,不是整个文件的大小,info中的存储的总长度是
        // 整个文件的大小,所以某一时刻总文件的大小可能会大于从某个断点处请求的文件的总大小。此时read的大小为
        // 之前读取的加上现在读取的
        if (info.getContentLength() > contentLength) {
            read = read + (info.getContentLength() - contentLength);
        } else {
            info.setContentLength(contentLength);
        }
        info.setReadLength(read);

        Observable.just(1).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Integer>() {
            @Override
            public void call(Integer integer) {
                if (progressObserver != null) {
                    progressObserver.progressChanged(info.getReadLength(), info.getContentLength(), done);
                }
            }
        });
    }

    /**
     * 开始下载
     * @param url
     */
    public void start(String url) {
        info.setUrl(url);
        final DownloadInterceptor interceptor = new DownloadInterceptor(this);
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(8, TimeUnit.SECONDS);
        builder.addInterceptor(interceptor);

        Retrofit retrofit = new Retrofit.Builder()
                .client(builder.build())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .baseUrl(CommonUtils.getBasUrl(MyConstants.DOWNLOAD_URL))
                .build();
        if (service == null) {
            service = retrofit.create(DownLoadService.class);
            info.setService(service);
        } else {
            service = info.getService();
        }

        downLoad();
    }

    /**
     * 开始下载
     */
    private void downLoad() {
        Log.e("下载:", info.toString());
        subscribe = service.download("bytes=" + info.getReadLength() + "-", info.getUrl())
                /*指定线程*/
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .retryWhen(new RetryWhenNetworkException())
                /* 读取下载写入文件,并把ResponseBody转成DownInfo */
                .map(new Func1<ResponseBody, DownloadInfo>() {
                    @Override
                    public DownloadInfo call(ResponseBody responseBody) {
                        try {
                            //写入文件
                            FileUtil.writeCache(responseBody, new File(info.getSavePath()), info);
                        } catch (IOException e) {
                            Log.e("异常:", e.toString());
                        }
                        return info;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<DownloadInfo>() {
                    @Override
                    public void onCompleted() {
                        Log.e("下载", "onCompleted");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("下载", "onError" + e.toString());
                    }

                    @Override
                    public void onNext(DownloadInfo downloadInfo) {
                        Log.e("下载", "onNext");
                    }
                });
    }

    /**
     * 暂停下载
     */
    public void pause() {
        if (subscribe != null)
            subscribe.unsubscribe();
    }

    /**
     * 继续下载
     */
    public void reStart() {
        downLoad();
    }

    /**
     * 进度监听
     */
    public interface ProgressListener {
        void progressChanged(long read, long contentLength, boolean done);
    }

    public void setProgressListener(ProgressListener progressObserver) {
        this.progressObserver = progressObserver;
    }
}

7.准备写入文件的工具类

public class FileUtil {
    /**
     * 写入文件
     *
     * @param file
     * @param info
     * @throws IOException
     */
    public static void writeCache(ResponseBody responseBody, File file, DownloadInfo info) throws IOException {
        if (!file.getParentFile().exists())
            file.getParentFile().mkdirs();
        long allLength;
        if (info.getContentLength() == 0) {
            allLength = responseBody.contentLength();
        } else {
            allLength = info.getContentLength();
        }

        FileChannel channelOut = null;
        RandomAccessFile randomAccessFile = null;
        randomAccessFile = new RandomAccessFile(file, "rwd");
        channelOut = randomAccessFile.getChannel();
        MappedByteBuffer mappedBuffer = channelOut.map(FileChannel.MapMode.READ_WRITE,
                info.getReadLength(), allLength - info.getReadLength());
        byte[] buffer = new byte[1024 * 4];
        int len;
        int record = 0;
        while ((len = responseBody.byteStream().read(buffer)) != -1) {
            mappedBuffer.put(buffer, 0, len);
            record += len;
        }
        responseBody.byteStream().close();
        if (channelOut != null) {
            channelOut.close();
        }
        if (randomAccessFile != null) {
            randomAccessFile.close();
        }
    }
}

8.准备处理baseURL的工具类

创建Retrofit需要有一个baseURL,并且结尾必须是以“/”结尾,否则会报异常

public class CommonUtils {
    /**
     * 读取baseurl
     *
     * @param url
     * @return
     */
    public static String getBasUrl(String url) {
        String head = "";
        int index = url.indexOf("://");
        if (index != -1) {
            head = url.substring(0, index + 3);
            url = url.substring(index + 3);
        }
        index = url.indexOf("/");
        if (index != -1) {
            url = url.substring(0, index + 1);
        }
        return head + url;
    }
}

9.准备一个下载文件的url

这个url是从应用宝中随便找的一个App的下载链接,这个App比较小,如果有兴趣,可以下载一个游戏类型的App,比如穿越火线App,这个有近700M,下载过程不会OOM,并且查看消耗的内存,一般会在15M一下。

public class MyConstants {
    public static final String DOWNLOAD_URL = "http://imtt.dd.qq.com/16891/89E1C87A75EB3E1221F2CDE47A60824A.apk?fsname=com.snda.wifilocating_4.2.62_3192.apk&csr=1bbd";
}

10.申请权限

因为只是做个下载的例子,所以没有做动态的权限申请,开启下载前,请手动在设置中给App赋予读写文件的权限,否则下载失败,无法写入。

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

11.开启下载

public class MainActivity extends AppCompatActivity implements DownloadManager.ProgressListener {

    private ProgressBar pb_progress;
    private TextView tv_progress;
    private DownloadManager downloadManager;
    private int i = 0;
    private Button btn_pasuse;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pb_progress = (ProgressBar) findViewById(R.id.pb_progress);
        tv_progress = (TextView) findViewById(R.id.tv_progress);
        btn_pasuse = (Button) findViewById(R.id.btn_pasuse);

        downloadManager = DownloadManager.getInstance();
        downloadManager.setProgressListener(this);
    }

    /**
     * 点击开始下载
     */
    public void start(View view) {
        downloadManager.start(MyConstants.DOWNLOAD_URL);
    }

    /**
     * 点击暂停下载或继续下载
     */
    public void pasuse(View view) {
        if (i % 2 == 0) {
            downloadManager.pause();
            btn_pasuse.setText("继续下载");
        } else {
            downloadManager.reStart();
            btn_pasuse.setText("暂停下载");
        }
        i++;
    }

    /**
     * 进度回调接口
     */
    @Override
    public void progressChanged(long read, long contentLength, boolean done) {
        final int progress = (int) (100 * read / contentLength);
        pb_progress.setProgress(progress);
        tv_progress.setText(progress + "%");
    }
}

这个例子也是看了很多别人的demo,但是并没有找到简单点的比较全的例子,所以综合了几个人的代码,自己也研究了一些,写出来了这个例子。

源码

  • 1
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
使用 Retrofit 下载文件需要定义一个返回类型为 `Call<ResponseBody>` 的 Retrofit 接口,其中 `ResponseBody` 表示响应体,它可以包含文件数据。下面是一个简单的示例代码: ```java public interface FileDownloadService { @GET("file/download") Call<ResponseBody> downloadFile(@Query("fileUrl") String fileUrl); } ``` 然后,你可以在应用程序中使用这个接口来下载文件。下面是一个使用 Retrofit 下载文件的示例: ```java String fileUrl = "https://example.com/file.pdf"; Call<ResponseBody> call = fileDownloadService.downloadFile(fileUrl); call.enqueue(new Callback<ResponseBody>() { @Override public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { if (response.isSuccessful()) { // 获取文件数据 ResponseBody body = response.body(); try { InputStream inputStream = body.byteStream(); // 创建文件 File file = new File(Environment.getExternalStorageDirectory(), "file.pdf"); FileOutputStream outputStream = new FileOutputStream(file); byte[] buffer = new byte[1024]; int len; while ((len = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, len); } outputStream.flush(); outputStream.close(); inputStream.close(); // 下载完成 Log.d(TAG, "Download completed!"); } catch (IOException e) { e.printStackTrace(); } } else { Log.d(TAG, "Download failed!"); } } @Override public void onFailure(Call<ResponseBody> call, Throwable t) { Log.e(TAG, "Download failed: " + t.getMessage()); } }); ``` 在这个示例中,我们首先创建了一个 Retrofit 接口 `FileDownloadService`,然后使用这个接口下载文件。在响应成功时,我们从响应体中获取文件数据,并将其写入存储设备中的文件。在响应失败时,我们打印一条日志消息。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值