rxjava2 + retrofit上传文件示例

service请求参数:

rxjava2 + retrofit上传文件示例_第1张图片

ApiService:

@Multipart
@POST("/api/chunkupload/upload")
Observable<Response<UploadFileModel>> fileUpload(@Part(value = "key") String key, @Part() MultipartBody.Part file);

请求过程:


核心部分

RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("attach_file",
        file.getName(), requestFile);

全部代码:


final String mp4Path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/video.mp4";
final long fileSize = new File(mp4Path).length();
RetrofitHelper.create().applyUpload("video.mp4", (int) fileSize)
        .filter(new Predicate<Response<UploadApplyModel>>() {
            @Override
            public boolean test(Response<UploadApplyModel> response) throws Exception {
                return response.getCode() == 200;
            }
        })
        .flatMap(new Function<Response<UploadApplyModel>, Observable<File>>() {
            @Override
            public Observable<File> apply(Response<UploadApplyModel> response) throws Exception {
                UploadApplyModel model = response.getData();
                int chunkSize = model.getChunk_size();
                String filePath = Environment.getExternalStorageDirectory().getAbsolutePath()+ "/video" +
                        ".mp4";
                List<File> list = new ArrayList<>();
                key = model.getKey();
                //切割文件
                splitFile(filePath, chunkSize, list);
                return Observable.fromIterable(list);
            }
        })
        .flatMap(new Function<File, Observable<Response<UploadFileModel>>>() {
            @Override
            public Observable<Response<UploadFileModel>> apply(File file) throws Exception {
                RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
                MultipartBody.Part body = MultipartBody.Part.createFormData("attach_file",
                        file.getName(), requestFile);
                Log.e("uploadFile", "key:" + key);
                return RetrofitHelper.create().fileUpload(key, body);
            }
        }).compose(RxTransformer.<UploadFileModel>handleResult(this))
        .subscribe(new Consumer<UploadFileModel>() {
            @Override
            public void accept(UploadFileModel uploadFileModel) throws Exception {
                Log.e("uploadFile", uploadFileModel.getProgress() + "");

            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                if(throwable instanceof RetrofitException){
                    Log.e("uploadFile", ((RetrofitException)throwable).getErrorMsg());
                }

            }
        });

上面的代码有缺陷!!!!!!!

上传的参数 key,服务器接受到的值会带有引号,所以需要修改如下:

@POST("/api/chunkupload/upload")
Observable<Response<UploadFileModel>> fileUpload(@Body RequestBody params);

MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
builder.addFormDataPart("key", key);
builder.addFormDataPart("attach_file",  file.getName(),
        RequestBody.create(MediaType.parse("multipart/octet-stream"), file));
return RetrofitHelper.create().fileUpload(builder.build());




你可能感兴趣的:(第三方框架的学习)