使用Retrofit实现文件的上传和下载

一、前言

使用Retrofit实现文件的上传和下载,代码是正确的代码但是我也不知道为什么运行不出来。

报错内容可以给你们看一下暂时没有解决。

1.文件的上传报错内容

什么添加读写权限,降低目标sdk的版本都试过了不行。有木有会的留个言。

使用Retrofit实现文件的上传和下载_第1张图片

2.文件的下载报错内容

使用Retrofit实现文件的上传和下载_第2张图片

二、代码实现文件上传

1.创建接口类对象

public interface UpLoadService {
    /**
     * 文件上传
     * 切记导包不要导错
     * @param file
     * @return
     */
    @POST("post")
    @Multipart
    Call upLoad(@Part MultipartBody.Part file);

}

2,代码实现

public class UpLoadFileUnitTest {
    Retrofit retrofit = new Retrofit.Builder().baseUrl("http://www.httpbin.org/").build();//创建retrofit对象
    UpLoadService upLoadService = retrofit.create(UpLoadService.class);

    @Test
    public void upLoadFileTest() throws IOException {
        File file1 = new File("C:\\Users\\Anglin\\Desktop\\1.doc");
        MultipartBody.Part part = MultipartBody.Part.createFormData("file1", "1.doc",
                RequestBody.create(file1, MediaType.parse("text/plain")));
        Call call = upLoadService.upLoad(part);
        System.out.println(call.execute().body().string());
    }
}

三、实现文件下载

1.创建接口类对象

public interface UpLoadService {


    /**
     * 下载文件
     * @param url
     * @return
     */
    @Streaming
    @GET
    Call downLoad(@Url String url);


}

2.代码实现

public class UpLoadFileUnitTest {
    Retrofit retrofit = new Retrofit.Builder().baseUrl("http://www.httpbin.org/").build();//创建retrofit对象
    UpLoadService upLoadService = retrofit.create(UpLoadService.class);


    @Test
    public void downLoadTest() throws IOException {
        Response response = upLoadService.downLoad("https://repo.maven.apache.org/maven2/org/bouncycastle/bcprov-jdk16/1.46/bcprov-jdk16-1.46.jar")
                .execute();
        InputStream inputStream = response.body().byteStream();
        FileOutputStream fos = new FileOutputStream("C:\\Users\\Anglin\\Desktop\\a.jar");
        int len;
        byte[] buffer = new byte[4096];
        while ((len = inputStream.read(buffer)) != -1){
            fos.write(buffer,0,len);
        }
        fos.close();
        inputStream.close();
    }


}

四、使用Rxjava实现文件下载

1.创建接口类对象

public interface UpLoadService {

    /**
     * 使用rxJava下载文件
     */
    @Streaming
    @GET
    Flowable downLoadRxJava(@Url String url);
}

2.代码实现

public class UpLoadFileUnitTest {
    Retrofit retrofit = new Retrofit.Builder().baseUrl("http://www.httpbin.org/").build();//创建retrofit对象
    UpLoadService upLoadService = retrofit.create(UpLoadService.class);


    @Test
    public void downLoadRxJavaText(){
        upLoadService.downLoadRxJava("https://repo.maven.apache.org/maven2/org/bouncycastle/bcprov-jdk16/1.46/bcprov-jdk16-1.46.jar")
                .map(new Function() {
                    @Override
                    public File apply(ResponseBody responseBody) throws Throwable {
                        InputStream inputStream = responseBody.byteStream();
                        File file = new File("C:\\Users\\Anglin\\Desktop\\a.jar");
                        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() {
                    @Override
                    public void accept(File file) throws Throwable {

                    }
                });
    }
}

五、总结

在实现文件的下载时需要在接口类对象中添加@Streaming,从而防止内存泄漏。


你可能感兴趣的:(Android,retrofit)