private void uploadImg(File file) {
//时间戳
long time = System.currentTimeMillis();
//参数以map形式组装起来
Map<String, Object> map = new TreeMap<>();
map.put("orgId", SharePreferenceUtils.getInt(HaoYunApp.getAppContext(), LoginResult.LOGIN_ORG_ID));
map.put("uId", SharePreferenceUtils.getInt(HaoYunApp.getAppContext(), LoginResult.LOGIN_USER_ID));
map.put("sign", Md5Util.encrypt(EncryptHelper.getUrlParamsByMap(map) + Interface.MD5 + time));
//以RequestBody形式上传
RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)
.addFormDataPart("orgId", SharePreferenceUtils.getInt(HaoYunApp.getAppContext(), LoginResult.LOGIN_ORG_ID) + "")
.addFormDataPart("uId", SharePreferenceUtils.getInt(HaoYunApp.getAppContext(), LoginResult.LOGIN_USER_ID) + "")
.addFormDataPart("sign", (String) map.get("sign"))
.addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("image/*;video/*"), file))
.build();
HttpManager.getUpLoadImpl()
.uploadImg(
DeviceUtils.getDeviceID(HaoYunApp.getAppContext()),
time, requestBody)
.observeOn(AndroidSchedulers.mainThread())//事件消费的线程,主线程显示
.subscribeOn(Schedulers.io())//事件产生的线程,后台线程取数据
.subscribe(new Observer<Images>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(Images images) {
//此处为成功返回结果
}
@Override
public void onError(Throwable e) {
e.printStackTrace();
//此处为失败返回结果
}
@Override
public void onComplete() {
}
});
}
======================================================
// 资源上传
@POST("fdfs/upLoadFile")
Observable<Images> uploadImg(
@Header("devId") String devId,
@Header("time") long time,
@Body RequestBody body
);
=====================================================================
Retrofit 上传文件
最新推荐文章于 2022-09-21 17:02:19 发布