复制代码
@Override
public Observable<BaseResp<UploadFileInfoResp>> getUploadFileUrl(UploadFileInfoReq uploadFileInfoReq) {
return apiService.getUploadFileUrl(uploadFileInfoReq);
}
复制代码
@Override
public Observable<ResponseBody> uploadFileDirect(String url, Map<String, String> headers, RequestBody requestBody) {
return apiService.uploadFileDirect(url, headers, requestBody);
}
复制代码
public class UploadFileInfoReq {
public String fileName;
public long fileSize;
public boolean randomFileName;
public String systemCode;
}
复制代码
public class UploadFileInfoResp {
public long fileId;
public String fileUrl;
public String uploadUrl;
public Map<String, String> headers;
}
复制代码
public void getUploadFileUrl(File file, UploadFileInfoReq uploadFileInfoReq) {
dataRepository.getUploadFileUrl(uploadFileInfoReq)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe(this)
.subscribe(new DisposableObserver<BaseResp<UploadFileInfoResp>>() {
@Override
public void onNext(@NonNull BaseResp<UploadFileInfoResp> response) {
if (response.code == BaseConstants.API_HANDLE_SUCCESS) {
uploadFileDirect(file, response.data);
}
}
@Override
public void onError(@NonNull Throwable e) {
}
@Override
public void onComplete() {
}
});
}
复制代码
public void uploadFileDirect(File file, UploadFileInfoResp uploadFileInfoResp) {
RequestBody requestBody = RequestBody.create(file, MediaType.parse("image/png"));
dataRepository.uploadFileDirect(uploadFileInfoResp.uploadUrl, uploadFileInfoResp.headers, requestBody)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe(this)
.subscribe(new DisposableObserver<ResponseBody>() {
@Override
public void onNext(@NonNull ResponseBody response) {
Log.i("xxx", "------ done-");
}
@Override
public void onError(@NonNull Throwable e) {
}
@Override
public void onComplete() {
}
});
}
复制代码
topBarView.setOnSettingsClickListener(new TopBarView.OnSettingsItemClickListener() {
@Override
public void onClick(View view) {
File file = new File(getExternalCacheDir().getAbsolutePath() + "/xxx.png");
showToast("size=" + file.length());
UploadFileInfoReq uploadFileInfoReq = new UploadFileInfoReq("TestFile", file.length(), "Test_File_001");
viewModel.getUploadFileUrl(file, uploadFileInfoReq);
}
});