/**
* 上传头像 文件
*/
public void postFile(String url, Map<String, String> map,HttpListener listener) {
if (map == null) {
map = new HashMap<>();
}
MultipartBody multipartBody = filesToMultipartBody(map);
mBaseApis.postFile(url, multipartBody)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(getObserver(listener));
}
/**
* 把string类型 转换为multpartBody
*/
public static MultipartBody filesToMultipartBody(Map<String,String> map) {
MultipartBody.Builder builder = new MultipartBody.Builder();
for (Map.Entry<String, String> entry : map.entrySet()) {
File file = new File(entry.getValue());
builder.addFormDataPart(entry.getKey(), file.getName(),
RequestBody.create(MediaType.parse("multipart/form-data"), file));
}
builder.setType(MultipartBody.FORM);
MultipartBody multipartBody = builder.build();
return multipartBody;
}