Android :Retrofit实现文件的上传和下载

Posted JMW1407

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android :Retrofit实现文件的上传和下载相关的知识,希望对你有一定的参考价值。

Retrofit实现文件的上传和下载

UploadService接口

package com.enjoy.networkdemo;

import java.io.File;

import io.reactivex.rxjava3.core.Flowable;
import okhttp3.MultipartBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.Part;
import retrofit2.http.Streaming;
import retrofit2.http.Url;

public interface UploadService {

    @POST("post")
    @Multipart
    Call<ResponseBody> upload(@Part MultipartBody.Part file);


    @Streaming
    @GET
    Call<ResponseBody> download(@Url String url);

    @Streaming
    @GET
    Flowable<ResponseBody> downloadRxJava(@Url String url);
}

UploadFileUnitTest实现普通方法和rxjava两种下载文件的办法

package com.enjoy.networkdemo.retrofit;

import com.enjoy.networkdemo.HttpbinService;
import com.enjoy.networkdemo.UploadService;

import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.functions.Function;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;

public class UploadFileUnitTest {
    Retrofit retrofit = new Retrofit.Builder().baseUrl("https://www.httpbin.org/").build();
    UploadService uploadService = retrofit.create(UploadService.class);
//文件上传
    @Test
    public void uploadFileTest() throws IOException {
        File file1 = new File("C:\\\\Users\\\\Administrator\\\\Desktop\\\\1.txt");
        MultipartBody.Part part = MultipartBody.Part.createFormData("file1",
                "1.txt", RequestBody.create(file1, MediaType.parse("text/plain")));

        Call<ResponseBody> call =
                uploadService.upload(part);
        System.out.println(call.execute().body().string());
    }
//文件普通下载
    @Test
    public void downloadTest() throws IOException {
        Response<ResponseBody> response = uploadService.download(
            "https://fga1.market.xiaomi" +
                ".com/download/AppStore/07adf043b0b2c40371abc6c685363e83d27f3efd7/com.sdu.didi" +
                ".psnger.apk")
            .execute();
//        response.isSuccessful()
        InputStream inputStream = response.body().byteStream();
        FileOutputStream fos = new FileOutputStream("C:\\\\Users\\\\Administrator\\\\Desktop\\\\a.apk");
        int len;
        byte[] buffer = new byte[4096];

        while ((len = inputStream.read(buffer)) != -1) {
            fos.write(buffer, 0, len);
        }
        fos.close();
        inputStream.close();
    }
//文件rxjava实现下载
    @Test
    public void downloadRxjavaTest() {
        uploadService.downloadRxJava("https://fga1.market.xiaomi.com/download/AppStore/07adf043b0b2c40371abc6c685363e83d27f3efd7/com.sdu.didi.psnger.apk")
                .map(new Function<ResponseBody, File>() {
                    @Override
                    public File apply(ResponseBody responseBody) throws Throwable {
                        InputStream inputStream = responseBody.byteStream();
                        File file = new File("C:\\\\Users\\\\Administrator\\\\Desktop\\\\a.apk");
                        FileOutputStream fos = new FileOutputStream(file);
                        int len;
                        byte[] buffer = new byte[4096];

                        while ((len = inputStream.read(buffer)) != -1) {
                            fos.write(buffer, 0, len);
                        }
                        fos.close();
                        inputStream.close();
                        return file;
                    }
                }).subscribe(new Consumer<File>() {
            @Override
            public void accept(File file) throws Throwable {

            }
        });
        while (true) {
        }
    }
}

参考

1、Android网络开源库-Retrofit(二) 文件上传、下载及进度监听

以上是关于Android :Retrofit实现文件的上传和下载的主要内容,如果未能解决你的问题,请参考以下文章

Android实战----基于Retrofit实现多图片/文件图文上传

Android实战----基于Retrofit实现多图片/文件图文上传

Android实战----基于Retrofit实现多图片/文件图文上传

Android Retrofit 实现(图文上传)文字(参数)和多张图片一起上传

Android Retrofit 实现(图文上传)文字(参数)和多张图片一起上传

Android使用Retrofit技术仿微信图片上传,可以选择多张图片拍照上传