retrofit上传、下载文件

这篇博客详细介绍了如何利用Retrofit库在Java中实现文件上传和下载功能。对于文件上传,通过@Multipart注解和Call<ResponseBody>接口进行;下载部分,不仅展示了普通下载方式,还利用RxJava实现流式下载,将响应体转换为字节流保存到本地文件。示例代码清晰地展示了每个步骤,适用于需要处理文件传输的Android开发者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上传文件:

接口方法:

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

具体使用:

Retrofit retrofit = new Retrofit.Builder().baseUrl("https://www.httpbin.org/").build();
    UploadService uploadService = retrofit.create(UploadService.class);

    @Test
    public void upload() throws IOException {
        File file = new File("D://desktop//1.txt");
        MultipartBody.Part part = MultipartBody.Part.createFormData("file1",
                "1.txt", RequestBody.create(MediaType.parse("text/plain"), file));

        Call<ResponseBody> call = uploadService.upload(part);
        System.out.println(call.execute().body().string());
    }

下载文件:

普通下载:
接口方法:

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

使用:

@Test
    public void downLoad() throws IOException {
        Response<ResponseBody> response = uploadService.download("http://ww3.sinaimg.cn/mw600/3ecd3787jw1e31uvhxzp8j.jpg")
                .execute();
        InputStream inputStream = response.body().byteStream();
        FileOutputStream fileOutputStream = new FileOutputStream("D://desktop//1.jpg");
        int len;
        byte[] buffer = new byte[4096];

        while ((len = inputStream.read(buffer)) != -1) {
            fileOutputStream.write(buffer, 0, len);
        }
        fileOutputStream.close();
        inputStream.close();
    }

依据下载地址,对得到的response.body获取字节流即可实现下载功能。

Rxjava下载:
接口方法:

	@GET
    Flowable<ResponseBody> downloadRxjava(@Url String url);

具体使用:

Retrofit retrofit1 = new Retrofit.Builder().baseUrl("https://www.httpbin.org/")
            .addCallAdapterFactory(RxJava3CallAdapterFactory.create()).build();
    UploadService uploadService1 = retrofit1.create(UploadService.class);
    @Test
    public void downloadRxjava() {
        uploadService1.downloadRxjava("http://ww3.sinaimg.cn/mw600/3ecd3787jw1e31uvhxzp8j.jpg")
            .map(new Function<ResponseBody, File>() {
                @Override
                public File apply(ResponseBody ResponseBody) throws Throwable {
                    InputStream inputStream = ResponseBody.byteStream();
                    File file = new File("D://desktop//1.jpg");
                    FileOutputStream fileOutputStream = new FileOutputStream(file);
                    int len;
                    byte[] buffer = new byte[4096];

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

用RxJava就可以直接在apply中对文件进行下载操作。在搭配嵌套请求才能下载的情况下使用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

魔幻音

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值