-
Notifications
You must be signed in to change notification settings - Fork 268
上传文件
Tamic (码小白) edited this page Sep 23, 2017
·
18 revisions
这里主要介绍怎么使用Novate上传文件:
Novate提供了2种方式上传文件。body和part模式,Body不包含key值,part包含key值。
以Body方式post数据,可以上报文件,图片等。
String mPath = uploadPath; //"you File path ";
String url = "http:/xxx.com";
novate.rxUploadWithBody(url, new File(mPath), new RxStringCallback() {
@Override
public void onNext(Object tag, String response) {
}
@Override
public void onError(Object tag, Throwable e) {
}
@Override
public void onCancel(Object tag, Throwable e) {
}
});
}
**上传文件,默认的key是 image **
String mPath = uploadPath; //"you File path ";
String url = "http:/xxx.com";
File file = new File(mPath);
novate.rxUploadWithPart(url, file, new RxStringCallback() {
@Override
public void onError(Object tag, Throwable e) {
}
@Override
public void onCancel(Object tag, Throwable e) {
}
@Override
public void onNext(Object tag, String response) {
}
});
如果自定义key 请看下面
String mPath = uploadPath; //"you File path ";
String url = "http:/xxx.com";
File file = new File(mPath);
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data; charset=utf-8"), file);
final NovateRequestBody requestBody = Utils.createNovateRequestBody(requestFile, new UpLoadCallback() {
@Override
public void onProgress(Object tag, int progress, long speed, boolean done) {
updateProgressDialog(progress);
}
});
MultipartBody.Part body2 =
MultipartBody.Part.createFormData("image", file.getName(), requestBody);
//请将image改成你和服务器约定好的key
novate.rxUploadWithPart(url, body2, new RxStringCallback() {
@Override
public void onError(Object tag, Throwable e) {
}
@Override
public void onCancel(Object tag, Throwable e) {
}
@Override
public void onNext(Object tag, String response) {
}
});
List<File> fileList = new ArrayList<>();
fileList.add(file);
fileList.add(file);
fileList.add(file);
novate.rxUploadWithPartListByFile(url, fileList, new RxStringCallback() {
@Override
public void onStart(Object tag) {
super.onStart(tag);
}
@Override
public void onNext(Object tag, String response) {
}
@Override
public void onError(Object tag, Throwable e) {
}
});
//构建body
RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)
.addFormDataPart("key1", V1)
.addFormDataPart("key2", v2)
.addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("image/*"), file))
.build();
novate.rxBody(url , requestBody, callback);
//构建body
RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)
.addFormDataPart("key1", V1)
.addFormDataPart("key2", v2)
.addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("image/*"), file))
.build();
novate.rxBody(url , requestBody, callback);
1.3.3 上已过时,但是可以使用
upLoadImage
RequestBody requestFile = RequestBody.create(MediaType.parse("image/jpg"), new File(you file path));
novate.upload(url, requestFile, new BaseSubscriber<ResponseBody>{
'''''''''''''''
});
带进度:
RequestBody requestFile = Utils.createFile(str);
NovateRequestBody novateRequestBody = Utils.createNovateRequestBody(requestFile, new UpLoadCallback() {
@Override
public void onProgress(Object tag, int progress, long speed, boolean done) {
}
});
novate.upload(url, novateRequestBody, new BaseSubscriber<ResponseBody>() {
'''''''''''''''
});
upLoadFile
File file = new File(path);
// 创建 RequestBody,用于封装 请求RequestBody
RequestBody requestFile = Utils.createFile(file);
// MultipartBody.Part is used to send also the actual file name
MultipartBody.Part body =
MultipartBody.Part.createFormData("image", file.getName(), requestFile);
String descriptionString = "hello, 这是文件描述";
RequestBody description = Utils.createPartFromString(descriptionString);
novate.uploadFlie(url, description, body,new BaseSubscriber<ResponseBody>() {
.......
});
upLoadFiles
Map<String, RequestBody> fileMaps = new HashMap<>(); maps.put("key1", requestFile1); maps.put("key2", requestFile2);
novate.uploadFlies(url, fileMaps, new BaseSubscriber<ResponseBody>(Context) {
......
} );
带进度:
File file = new File(path);
RequestBody requestFile = Utils.createFile(file);
Map<String, RequestBody> maps = new HashMap<>();
maps.put("file1", Utils.createNovateRequestBody(requestFile, callback));
maps.put("file2", Utils.createNovateRequestBody(requestFile, callback));
novate.uploadFlies(url, maps, new BaseSubscriber<ResponseBody>(ExempleActivity.this) {
......
} );