Retrofit2学习之七:上传多个文件

上传多个文件有两种方案:

  • 使用多个域,每个域上传一个文件
  • 使用一个域,在该域名称下上传多个文件

使用多个域上传多个文件

创建业务请求接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public interface Api {

/**
* 上传多个文件:一个域对应一个文件
* @param description
* @param file1
* @param file2
* @return
*/
@Multipart
@POST("upload/multiple")
Call<ResponseBody> uploadMultipleFiles(@Part("description") RequestBody description,
@Part MultipartBody.Part file1,
@Part MultipartBody.Part file2);

}

创建一个Retrofit的实例,然后利用Retrofit实例创建接口对象和调用接口方法

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
public static final String MULTIPART_FORM_DATA = "multipart/form-data";

@NonNull
private RequestBody prepareFromPart(String description) {
return RequestBody.create(MediaType.parse(MULTIPART_FORM_DATA), description);
}

/**
* 一个域对应一个文件
* @param partName
* @param filePath
* @return
*/
private MultipartBody.Part prepareFilePart(String partName, String filePath) {
File file = new File(filePath);
// 为file建立RequestBody实例
RequestBody requestFile = RequestBody.create(MediaType.parse(MULTIPART_FORM_DATA), file);
// MultipartBody.Part借助文件名完成最终的上传
return MultipartBody.Part.createFormData(partName, file.getName(), requestFile);
}

public void uploadMultipleFiles(View view) {
String file1Path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "test.gif";
String file2Path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "source.gif";

Retrofit retrofit = new Retrofit.Builder()
.baseUrl(SERVER_ADDRESS)
.build();

api = retrofit.create(Api.class);

// 添加Form的part
RequestBody description = prepareFromPart("hello, this is description speaking");

// 创建文件的part (photo, video, ...)
MultipartBody.Part file1Body = prepareFilePart("file", file1Path);
MultipartBody.Part file2Body = prepareFilePart("file", file2Path);

api.uploadMultipleFiles(description, file1Body, file2Body).enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
//在UI主线程运行
if (response.isSuccessful()) {
Log.i(TAG, "上传成功");
}
}

@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.i(TAG, "上传失败: " + t.getLocalizedMessage());
}
});
}

使用一个域上传多个文件

创建业务请求接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public interface Api {

/**
* 上传多个文件:一个域对应多个文件
* @param description
* @param parts
* @return
*/
@Multipart
@POST("upload/multiples")
Call<ResponseBody> uploadMultipleFiles(@Part("description") RequestBody description,
@Part List<MultipartBody.Part> parts);

}

创建一个Retrofit的实例,然后利用Retrofit实例创建接口对象和调用接口方法

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
public static final String MULTIPART_FORM_DATA = "multipart/form-data";

@NonNull
private RequestBody prepareFromPart(String description) {
return RequestBody.create(MediaType.parse(MULTIPART_FORM_DATA), description);
}

/**
* 一个域对应多个文件
* @param partName
* @param filePathList
* @return
*/
private List<MultipartBody.Part> prepareFilesPart(String partName, List<String> filePathList) {
List<MultipartBody.Part> parts = new ArrayList<>(filePathList.size());
for (String filePath : filePathList) {
Log.i(TAG, "prepareFilesPart: " + filePath);
File file = new File(filePath);
RequestBody requestFile = RequestBody.create(MediaType.parse(MULTIPART_FORM_DATA), file);
MultipartBody.Part part = MultipartBody.Part.createFormData(partName, file.getName(), requestFile);
parts.add(part);
}
return parts;
}

public void uploadMultipleFiles(View view) {
String file1Path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "test.gif";
String file2Path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "source.gif";
List<String> filePathList = new ArrayList<>();
filePathList.add(file1Path);
filePathList.add(file2Path);

Retrofit retrofit = new Retrofit.Builder()
.baseUrl(SERVER_ADDRESS)
.build();

api = retrofit.create(Api.class);

// 添加Form的part
RequestBody description = prepareFromPart("hello, this is description speaking");

// 创建文件的part (photo, video, ...)
List<MultipartBody.Part> fileBody = prepareFilesPart("file", filePathList);

api.uploadMultipleFiles(description, fileBody).enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
//在UI主线程运行
if (response.isSuccessful()) {
Log.i(TAG, "上传成功");
}
}

@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.i(TAG, "上传失败: " + t.getLocalizedMessage());
}
});
}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :