每个应用基本都会涉及到文件的上传或下载,最普遍的一般也就是上传头像或者照片,下载安装包了,本篇文章就这两点简单说一下retrofit+rxjava的对文件的上传和下载。
1.上传
首先说一下单文件上传,一般上传头像等会用到.
1).写api@Multipart
@POST
("")//引号内为地址Observable httpName(@PartMultipartBody.Part file);
2).写presenter的方法
public void httpName(File file) {
RequestBody requestBody = RequestBody.create(MediaType.parse("image/png"), file);
MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName() , requestBody);
Observable observable = api. httpName (part);
…rajava+retrofit处理逻辑
}
3)调用方法发起请求
mPresenter. httpName (f);
其中f我为你要上传的文件对象
以图片为例,经过裁剪后将其转化为文件对象方法如下
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
Bundle bundle = data.getExtras();
if (bundle != null) {
bitmap = bundle.getParcelable("data");
File f = new File(this.getFilesDir(), (new Date()).getTime() + ".png");
if (f.exists()) {f.delete();}
try {
FileOutputStream out = new FileOutputStream(f);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
f = null;
} catch (IOException e) {
e.printStackTrace();
f = null;
}
if (f != null) {
mPresenter. httpName(f);
}}}//括号可能多或者少,自己添吧
再说一下多文件上传,以及附带有参数的请求,类似这样
mPresenter.httpUpLoadMore(id,phone, File1, File2, File3);
@Multipart
@POST("")
Observable<ResponseBody> httpUpLoadMore (@Query("id") String id, @Query("phone") String phone, @Part MultipartBody.Part file1, @Part MultipartBody.Part file2, @Part MultipartBody.Part file3);
这里附带参数用@FieldMap Map maps也可以,用query好像不太恰当
后面只需要像传单文件一样
RequestBody requestBody1/2/3 = RequestBody.create(MediaType.parse("image/png"), file1/2/3);;
MultipartBody.Part part1/2/3 = MultipartBody.Part.createFormData("file", file1/2/3.getName() , requestBody1/2/3);
Observable bservable= api.httpUpLoadMore(id,phone,part1,part2,part3);
……
2下载
1)写api
@Streaming//下载大文件时需要加上
@GET
Observable > download(@Url String url);
2)Presenter方法
mApi.download (path)
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.io())
.flatMap(new Func1,Observable>() {
@Override
public Observablecall(Response response) {
boolean b = writeToFile(response, file);//将返回的流转写入到file对象中
final Boolean aBoolean =Boolean.valueOf(b);
return Observable.create(new Observable.OnSubscribe(){
@Override
public void call(Subscriber subscriber) {
try {
subscriber.onNext(aBoolean);
subscriber.onCompleted();
} catch (Exceptione) {
subscriber.onError(ExceptionManager.handleException(e));}}});}})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1(){
@Override
public void call(Boolean bean) {}
}, new Action1(){
@Override
public void call(Throwablethrowable) {}});
[if !supportLineBreakNewLine]
[endif]
private boolean writeToFile(Responsebody,File file) {
try {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
byte[] fileReader = new byte[2048];
inputStream =body.body().byteStream();
outputStream = new FileOutputStream(file);
while (true) {
int read =inputStream.read(fileReader);
if (read == -1) break;
outputStream.write(fileReader, 0, read);
}
outputStream.flush();
return true;
} catch (IOException e) {
return false;
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}}
} catch (IOException e) {
return false;
}}
3)调用方法发起下载请求
mPresenter.httpToDownload(downPath, file);//file为你下载下来的文件要存放的位置
因本人app中用的是rxjava1,所以一些rxjava+retrofit处理逻辑写的不细甚至有些乱,所以大家可以自己作相应修改,不要拿来就用.