Retrofit2文件上传下载及其进度显示
序
前面一篇文章介绍了Retrofit2的基本使用,这篇文章接着介绍使用Retrofit2实现文件上传和文件下载,以及上传下载过程中如何实现进度的显示。
文件上传
定义接口
1
2
3
|
@Multipart
@POST("fileService")
Call<User> uploadFile(@Part MultipartBody.Part file);
|
构造请求体上传
1
2
3
4
5
|
File file = new File(filePath);
RequestBody body = RequestBody.create(MediaType.parse("application/otcet-stream"), file);
MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), body);
Call<User> call = getRetrofitService().uploadOneFile(part);
call.enqueue(callback);
|
这样就可以将这个文件上传到服务器,但就这样上传操作不够友好,最好加上文件上传进度。而Retrofit本身是不支持文件上传进度显示的,所以就需要我们自己扩展OkHttp来实现文件上传进度。
我的做法是直接扩展一个RequestBody来实现进度显示,实现完成之后只需要将上面body进行包装转换即可
上传进度显示
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
RetrofitCallback<User> callback = new RetrofitCallback<User>() {
@Override
public void onSuccess(Call<User> call, Response<User> response) {
runOnUIThread(activity, response.body().toString());
//进度更新结束
}
@Override
public void onFailure(Call<User> call, Throwable t) {
runOnUIThread(activity, t.getMessage());
//进度更新结束
}
@Override
public void onLoading(long total, long progress) {
super.onLoading(total, progress);
//此处进行进度更新
}
};
RequestBody body1 = RequestBody.create(MediaType.parse("application/otcet-stream"), file);
//通过该行代码将RequestBody转换成特定的FileRequestBody
FileRequestBody body = new FileRequestBody(body1, callback);
MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), body);
Call<User> call = getRetrofitService().uploadOneFile(part);
call.enqueue(callback);
|
回调RetrofitCallback
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public abstract class RetrofitCallback<T> implements Callback<T> {
@Override
public void onResponse(Call<T> call, Response<T> response) {
if(response.isSuccessful()) {
onSuccess(call, response);
} else {
onFailure(call, new Throwable(response.message()));
}
}
public abstract void onSuccess(Call<T> call, Response<T> response);
public void onLoading(long total, long progress) {
}
}
|
FileRequestBody
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
/**
* 扩展OkHttp的请求体,实现上传时的进度提示
*
* @param <T>
*/
public final class FileRequestBody<T> extends RequestBody {
/**
* 实际请求体
*/
private RequestBody requestBody;
/**
* 上传回调接口
*/
private RetrofitCallback<T> callback;
/**
* 包装完成的BufferedSink
*/
private BufferedSink bufferedSink;
public FileRequestBody(RequestBody requestBody, RetrofitCallback<T> callback) {
super();
this.requestBody = requestBody;
this.callback = callback;
}
@Override
public long contentLength() throws IOException {
return requestBody.contentLength();
}
@Override
public MediaType contentType() {
return requestBody.contentType();
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
if (bufferedSink == null) {
//包装
bufferedSink = Okio.buffer(sink(sink));
}
//写入
requestBody.writeTo(bufferedSink);
//必须调用flush,否则最后一部分数据可能不会被写入
bufferedSink.flush();
}
/**
* 写入,回调进度接口
* @param sink Sink
* @return Sink
*/
private Sink sink(Sink sink) {
return new ForwardingSink(sink) {
//当前写入字节数
long bytesWritten = 0L;
//总字节长度,避免多次调用contentLength()方法
long contentLength = 0L;
@Override
public void write(Buffer source, long byteCount) throws IOException {
super.write(source, byteCount);
if (contentLength == 0) {
//获得contentLength的值,后续不再调用
contentLength = contentLength();
}
//增加当前写入的字节数
bytesWritten += byteCount;
//回调
callback.onLoading(contentLength, bytesWritten);
}
};
}
}
|
文件下载
接口定义
文件下载请求与普通的Get和Post请求是一样的,只是他们的返回值不一样而已,文件下载请求的返回值一般定义成ResponseBody
1
2
3
4
|
//这里只举例POST方式进行文件下载
@FormUrlEncoded
@POST("fileService")
Call<ResponseBody> downloadFile(@Field("param") String param);
|
发起请求
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
RetrofitCallback<ResponseBody> callback = new RetrofitCallback<ResponseBody>() {
@Override
public void onSuccess(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
InputStream is = response.body().byteStream();
String path = Util.getSdCardPath();
File file = new File(path, "download.jpg");
FileOutputStream fos = new FileOutputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
byte[] buffer = new byte[1024];
int len;
while ((len = bis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
fos.flush();
fos.close();
bis.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
runOnUIThread(activity, t.getMessage());
}
};
Call<ResponseBody> call = getRetrofitService(callback).downloadFile(param);
call.enqueue(callback);
|
下载进度显示
下载进度显示有两种方式实现,一种是通过OkHttp设置拦截器将ResponseBody进行转换成我们扩展后的ResponseBody(稍后介绍),另外一种则是在上面的回调Callback中将ResponseBody的流写入到文件时进行进度处理,下面分别进行介绍。
扩展ResponseBody设置OkHttp拦截器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
private <T> RetrofitService getRetrofitService(final RetrofitCallback<T> callback) {
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
clientBuilder.addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
okhttp3.Response response = chain.proceed(chain.request());
//将ResponseBody转换成我们需要的FileResponseBody
return response.newBuilder().body(new FileResponseBody<T>(response.body(), callback)).build();
}
});
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(clientBuilder.build())
.addConverterFactory(GsonConverterFactory.create())
.build();
RetrofitService service = retrofit.create(RetrofitService.class);
return service ;
}
//通过上面的设置后,我们需要在回调RetrofitCallback中实现onLoading方法来进行进度的更新操作,与上传文件的方法相同
|
FileResponseBody
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
/**
* 扩展OkHttp的请求体,实现上传时的进度提示
*
* @param <T>
*/
public final class FileResponseBody<T> extends ResponseBody {
/**
* 实际请求体
*/
private ResponseBody mResponseBody;
/**
* 下载回调接口
*/
private RetrofitCallback<T> mCallback;
/**
* BufferedSource
*/
private BufferedSource mBufferedSource;
public FileResponseBody(ResponseBody responseBody, RetrofitCallback<T> callback) {
super();
this.mResponseBody = responseBody;
this.mCallback = callback;
}
@Override
public BufferedSource source() {
if (mBufferedSource == null) {
mBufferedSource = Okio.buffer(source(mResponseBody.source()));
}
return mBufferedSource;
}
@Override
public long contentLength() {
return mResponseBody.contentLength();
}
@Override
public MediaType contentType() {
return mResponseBody.contentType();
}
/**
* 回调进度接口
* @param source
* @return Source
*/
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);
totalBytesRead += bytesRead != -1 ? bytesRead : 0;
mCallback.onLoading(mResponseBody.contentLength(), totalBytesRead);
return bytesRead;
}
};
}
}
|
直接在回调中进行进度更新
上面介绍了通过扩展ResponseBody同时设置OkHttp拦截器来实现进度条更新显示,另外也可以直接在请求回调onSuccess中将流转换成文件时实现进度更新,下面给出大致实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
RetrofitCallback<ResponseBody> callback = new RetrofitCallback<ResponseBody>() {
@Override
public void onSuccess(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
InputStream is = response.body().byteStream();
//获取文件总长度
long totalLength = is.available();
String path = Util.getSdCardPath();
File file = new File(path, "download.jpg");
FileOutputStream fos = new FileOutputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
byte[] buffer = new byte[1024];
int len;
while ((len = bis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
//此处进行更新操作
//len即可理解为已下载的字节数
//onLoading(len, totalLength);
}
fos.flush();
fos.close();
bis.close();
is.close();
//此处就代表更新结束
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
runOnUIThread(activity, t.getMessage());
}
};
|
以上就是Retrofit中文件上传下载及其进度更新显示的实现
Retrofit2文件上传下载及其进度显示的更多相关文章
- 使用Typescript重构axios(二十五)——文件上传下载进度监控
0. 系列文章 1.使用Typescript重构axios(一)--写在最前面 2.使用Typescript重构axios(二)--项目起手,跑通流程 3.使用Typescript重构axios(三) ...
- SpringMVC文件上传下载
在Spring MVC的基础框架搭建起来后,我们测试了spring mvc中的返回值类型,如果你还没有搭建好springmvc的架构请参考博文->http://www.cnblogs.com/q ...
- JavaWeb实现文件上传下载功能实例解析
转:http://www.cnblogs.com/xdp-gacl/p/4200090.html JavaWeb实现文件上传下载功能实例解析 在Web应用系统开发中,文件上传和下载功能是非常常用的功能 ...
- java web 文件上传下载
文件上传下载案例: 首先是此案例工程的目录结构:
- 2013第38周日Java文件上传下载收集思考
2013第38周日Java文件上传&下载收集思考 感觉文件上传及下载操作很常用,之前简单搜集过一些东西,没有及时学习总结,现在基本没啥印象了,今天就再次学习下,记录下自己目前知识背景下对该类问 ...
- C# 文件上传下载功能实现 文件管理引擎开发
Prepare 本文将使用一个NuGet公开的组件技术来实现一个服务器端的文件管理引擎,提供了一些简单的API,来方便的实现文件引擎来对您自己的软件系统的文件进行管理. 在Visual Studio ...
- JavaWeb 文件上传下载
1. 文件上传下载概述 1.1. 什么是文件上传下载 所谓文件上传下载就是将本地文件上传到服务器端,从服务器端下载文件到本地的过程.例如目前网站需要上传头像.上传下载图片或网盘等功能都是利用文件上传下 ...
- JavaWeb -- 文件上传下载示例
1. 上传简单示例 Jsp <%@ page language="java" import="java.util.*" pageEncoding=&quo ...
- javaEE(14)_文件上传下载
一.文件上传概述 1.实现web开发中的文件上传功能,需完成如下二步操作: •在web页面中添加上传输入项•在servlet中读取上传文件的数据,并保存到本地硬盘中. 2.如何在web页面中添加上传输 ...
随机推荐
- comet
comet 1.简介: 基于 HTTP长连接的“服务器推”技术,是一种新的 Web 应用架构,基于这种架构开发的应用中,服务器端会主动以异步的方式向客户端程序推送数据,而不需要客户端显式的发出请求.C ...
- ResultSet结果集判断是否为空
目前亲测过能用的一个方法是: if(rs.next())//当前行有内容 { msg2 = "有这个活动!"; } else //rs对象为空表示查无此活动 { msg2 = &q ...
- openstack中运行定时任务的两种方法及源代码分析
启动一个进程,如要想要这个进程的某个方法定时得进行执行的话,在openstack有两种方式: 一种是通过继承 periodic_task.PeriodicTasks,另一种是使用loopingcall ...
- GSM Hacking:使用BladeRF、树莓派、YatesBTS搭建便携式GSM基站
每次看到黑客在网上发布的那些GSM技术相关文章我都十分惊讶.然而在没有Software Defined Radios (SDRs)之前,玩GSM并不便宜,除此之外想要好好玩你得下大功夫. 拓展阅读 G ...
- 极客DIY:开源WiFi智能手表制作
如果你喜欢拥有一款属于自己的无线手表,那么请不要错过,相信阅读完这篇文章对你会很有帮助. 硬件规格 ESP8266(32Mbit闪存) MPU-9250(陀螺仪传感器)以及 AK8963(内置磁力计) ...
- 正确的SVN导入代码命令
sudo svn import -m "" /media/yang/10/ubuntuFiles/svnAbout/mypro/ svn://127.0.0.1/mypro/
- svn出现权限不足时的解决方法
将所有svn目录设置为当前用户所有....即可 sudo chown will:will . -R
- linux命令:mv
1.命令介绍: mv是move的缩写,用来移动文件或重命名文件 2.命令格式: mv [选项] 源文件 目标文件 3.命令参数: -b :若需覆盖文件,则覆盖前先行备份. -f --force:fo ...
- Alice and Bob(不断补充)
我之前做过一些博弈的题目,以为博弈都是DP,结果被坑了很多次,其实博弈有很多种,在此,把我见过的类型都搬上来. 1,HDU3951(找规律) 题意:把n枚硬币围成一个圆,让Alice和Bob两个人分别 ...
- 【LeetCode OJ】Longest Consecutive Sequence
Problem Link: http://oj.leetcode.com/problems/longest-consecutive-sequence/ This problem is a classi ...