# Retrofit2 文件上传与下载
# 文件上传:(包括多文件上传)
# 第一步:创建服务返回数据的 bean 类;(此处服务器返回的是 json 字符串);
1 2 3 4 5 6 7 8
| public class BaseBean{
private int Code; private String Msg; private String Data;
}
|
# 第二步:创建用于描述网络请求的接口
1 2 3 4 5 6 7 8 9 10 11 12
| public interface Api {
@Multipart @POST("服务器地址(就创建retrofit设置的基站地址后面的具体地址)") Call<BaseBean> upLoading(@Part List<MultipartBody.Part> partList);
}
|
# 第三步:创建表单,里面存储服务器本接口所需要的数据;
1 2 3 4 5 6
| 参数添加 MultipartBody.Builder builder = new MultipartBody.Builder() .setType(MultipartBody.FORM) .addFormDataPart("参数1", "值1") .addFormDataPart("参数2", "值2");
|
# 创建文件 (你需要上传到服务器的文件)
1
| File file = new File(file1Location);
|
# 设置文件的格式
1 2 3 4 5
| RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
builder.addFormDataPart("uploadfile", file.getName(), imageBody);
|
# 生成接口需要的 list
1
| List<MultipartBody.Part> parts = builder.build().parts();
|
# 创建设置 OkHttpClient
1 2 3 4 5 6 7
| OkHttpClient okHttpClient = new OkHttpClient.Builder() .connectTimeout(20, TimeUnit.SECONDS) .readTimeout(20, TimeUnit.SECONDS) .writeTimeout(20, TimeUnit.SECONDS) .retryOnConnectionFailure(true) .build();
|
# 创建 retrofit 实例对象
1 2 3 4 5 6 7 8 9 10 11
| Retrofit retrofit = new Retrofit.Builder() .baseUrl("你的基站地址") .client(okHttpClient) .addConverterFactory(GsonConverterFactory.create()) .build();
|
# 实例化请求接口,把表单传递过去;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| Call<BaseBean> call = retrofit.create(Api.class).upLoading(parts); call.enqueue(new Callback<BaseBean>() {
@Override public void onResponse(Call<BaseBean> call, Response<BaseBean> response) { System.out.println(response.body().toString()); }
@Override public void onFailure(Call<BaseBean> call, Throwable t) { System.out.println("网络不可用"); } }); }
|
# 这就 ok 了,单个文件上传完毕!!!
# 多文件上传 (以两个文件为例)
1 2 3 4 5 6
| 对比一个文件,这个只需要在[设置文件的格式] 这一步,多添加一个即可; RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), file); RequestBody imageBody1 = RequestBody.create(MediaType.parse("multipart/form-data"), file1); builder.addFormDataPart("uploadfile", file.getName(), imageBody); builder.addFormDataPart("uploadfile1", file1.getName(), imageBody1);
|
# 接下来看文件下载ღ
# 文件下载:
# 首先还是要在 API 接口创建一个方法;
1 2 3 4 5 6 7 8 9
|
@GET Call<ResponseBody> download(@Url String fileUrl);
|
# 然后就是创建一个 retrofit 对象,跟上面一样 (由于每次创建 retrofit 对象会用到很多重复的代码,可以抽取成一个公共方法)
# 实例化请求接口:
我在百度上找到一张图片,把他的地址拆分了一下,最后一个斜杠之前的 url 设置为了 baseUrl, 斜杠之后设置在这里;
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
| 地址拆分: .baseUrl("https://gss3.bdstatic.com/-Po3dSag_xI4khGkpoWK1HF6hhy/baike/w%3D268%3Bg%3D0/sign=e3dc64d05a3d26972ed30f5b6dc0d5c6/")
Call<ResponseBody> download = retrofit.create(Api.class).download("241f95cad1c8a7868a2713146c09c93d70cf509e.jpg"); download.enqueue(new Callback<ResponseBody>() {
@Override public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { if (response != null && response.isSuccessful()) { boolean toDisk = writeResponseBodyToDisk(response.body()); if (toDisk) { System.out.println("下载成功请查看"); } else { System.out.println("下载失败,请稍后重试"); } } else { System.out.println("服务器返回错误"); } }
@Override public void onFailure(Call<ResponseBody> call, Throwable t) { System.out.println("网络不可用"); } });
|
# writeResponseBodyToDisk (下载文件保存到本地工具类)
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
|
private boolean writeResponseBodyToDisk(ResponseBody body) { try { File files = new File(SD_HOME_DIR); if (!files.exists()) { files.mkdirs(); } File futureStudioIconFile = new File(SD_HOME_DIR + "download.jpg"); InputStream inputStream = null; OutputStream outputStream = null; try { byte[] fileReader = new byte[4096]; long fileSize = body.contentLength(); long fileSizeDownloaded = 0; inputStream = body.byteStream(); outputStream = new FileOutputStream(futureStudioIconFile); while (true) { int read = inputStream.read(fileReader); if (read == -1) { break; } outputStream.write(fileReader, 0, read); fileSizeDownloaded += read; }
outputStream.flush(); return true; } catch (IOException e) { return false; } finally { if (inputStream != null) { inputStream.close(); } if (outputStream != null) { outputStream.close(); } } } catch (IOException e) { return false; } }
|