Skip to content

文件上传 下载 进度监听

liujingxing edited this page Jun 17, 2024 · 11 revisions

android 10 文件上传下载,请看这里RxHttp 完美适配Android 10/11 上传/下载/进度监听

上传

//RxJava
RxHttp.postForm("/service/...")                //发送Form表单形式的Post请求
    .addFile("file", new File("xxx/1.png"))  //添加文件
    .toObservableString()
    .subscribe({
        //成功回调
    }, {
        //异常回调
    })

//Flow
RxHttp.postForm("/service/...")                //发送Form表单形式的Post请求
    .addFile("file", new File("xxx/1.png"))  //添加文件
    .toFlowString()
    .catch {
        //异常回调
    }.collect {
        //成功回调
    }

上传进度监听

//RxJava
RxHttp.postForm("/service/...")
    .addFile("file1", new File("xxx/1.png"))
    .toObservableString()
    .onMainProgress {
        val fraction = it.fraction //当前进度 [0.0, 1.0]
        val progress = it.progress //当前进度 [0, 100]
        val currentSize = it.currentSize //当前已上传的字节大小
        val totalSize = it.totalSize     //要上传的总字节大小
        val speed = it.speed   //上传速度 单位: byte/s
        val time = it.calculateRemainingTime()  //上传剩余时间,单位: s
    }
    .subscribe({             
        //成功回调
    }, {
        //异常回调
    })


//Flow
RxHttp.postForm("/service/...")                //发送Form表单形式的Post请求
    .addFile("file", new File("xxx/1.png"))  //添加文件
    .toFlowString()
    .onProgress {
        val fraction = it.fraction //当前进度 [0.0, 1.0]
        val progress = it.progress //当前进度 [0, 100]
        val currentSize = it.currentSize //当前已上传的字节大小
        val totalSize = it.totalSize     //要上传的总字节大小
        val speed = it.speed   //上传速度 单位: byte/s
        val time = it.calculateRemainingTime()  //上传剩余时间,单位: s
    }
    .catch {
        //异常回调
    }.collect {
        //成功回调
    }

下载

//RxJava
RxHttp.get("/service/...")
    .toDownloadObservable("sd/xxx/1.apk") //传入本地路径
    .subscribe({
        //下载成功,回调文件下载路径
    }, {
        //异常回调
    })

//Flow
RxHttp.get("/service/...")
    .toDownloadFlow("sd/xxx/1.apk") //传入本地路径
    .catch {
        //异常回调
    }.collect {
        //成功回调
    }

下载进度监听

//RxJava
RxHttp.get("/service/...")
    .toDownloadObservable("sd/xxx/1.apk")
    .onMainProgress {
        val fraction = it.fraction //当前进度 [0.0, 1.0]
        val progress = it.progress //当前进度 [0, 100]
        val currentSize = it.currentSize //当前已下载的字节大小
        val totalSize = it.totalSize     //要下载的总字节大小
        val speed = it.speed //下载速度 单位: byte/s
        val time = it.calculateRemainingTime()  //下载剩余时间,单位: s
    }
    .subscribe({                  //s为String类型,这里为文件存储路径
        //下载完成
    }, {
        //异常回调
    });

//Flow
RxHttp.get("/service/...")
    .toDownloadFlow("sd/xxx/1.apk") //传入本地路径
    .onProgress {
        val fraction = it.fraction //当前进度 [0.0, 1.0]
        val progress = it.progress //当前进度 [0, 100]
        val currentSize = it.currentSize //当前已下载的字节大小
        val totalSize = it.totalSize     //要下载的总字节大小
        val speed = it.speed //下载速度 单位: byte/s
        val time = it.calculateRemainingTime()  //下载剩余时间,单位: s
    }
    .catch { 
        //异常回调
    }.collect {
        //成功回调
    }

断点下载进度监听

//RxJava
RxHttp.get("/service/...")
    .toDownloadObservable("sd/xxx/1.apk", true)  //该方法第二个参数传入true,即代表断点下载
    .onMainProgress {
        val fraction = it.fraction //当前进度 [0.0, 1.0]
        val progress = it.progress //当前进度 [0, 100]
        val currentSize = it.currentSize //当前已下载的字节大小
        val totalSize = it.totalSize     //要下载的总字节大小
        val speed = it.speed //下载速度 单位: byte/s
        val time = it.calculateRemainingTime()  //下载剩余时间,单位: s
    }
    .subscribe({
        //下载成功,处理相关逻辑
    }, {
        //异常回调
    })


//Flow
RxHttp.get("/service/...")
    .toDownloadFlow("sd/xxx/1.apk", true) 
    .onProgress {
        val fraction = it.fraction //当前进度 [0.0, 1.0]
        val progress = it.progress //当前进度 [0, 100]
        val currentSize = it.currentSize //当前已下载的字节大小
        val totalSize = it.totalSize     //要下载的总字节大小
        val speed = it.speed //下载速度 单位: byte/s
        val time = it.calculateRemainingTime()  //下载剩余时间,单位: s
    }
    .catch {
        //异常回调
    }.collect {
        //成功回调
    }