use of com.okhttplib.bean.UploadFileInfo in project OkHttp3 by MrZhousf.
the class DownUpLoadHelper method uploadFile.
/**
* 文件上传
*/
void uploadFile(OkHttpHelper helper) {
try {
final HttpInfo info = httpInfo;
List<UploadFileInfo> uploadFileList = helper.getUploadFileInfoList();
String url = info.getUrl();
if (TextUtils.isEmpty(url)) {
showLog("文件上传接口地址不能为空");
return;
}
StringBuilder log = new StringBuilder("PostParams: ");
MultipartBody.Builder mBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM);
ProgressCallback progressCallback = helper.getProgressCallback();
String logInfo;
if (null != info.getParams() && !info.getParams().isEmpty()) {
for (String key : info.getParams().keySet()) {
mBuilder.addFormDataPart(key, info.getParams().get(key));
logInfo = key + " =" + info.getParams().get(key) + ", ";
log.append(logInfo);
}
}
for (UploadFileInfo fileInfo : uploadFileList) {
if (progressCallback == null) {
progressCallback = fileInfo.getProgressCallback();
}
String filePath = fileInfo.getFilePathWithName();
String interfaceParamName = fileInfo.getInterfaceParamName();
File file = new File(filePath);
log.append(interfaceParamName);
log.append("=");
log.append(filePath);
log.append(",");
mBuilder.addFormDataPart(interfaceParamName, file.getName(), RequestBody.create(MediaTypeUtil.fetchFileMediaType(filePath), file));
}
showLog(log.toString());
RequestBody requestBody = mBuilder.build();
Request.Builder requestBuilder = new Request.Builder();
requestBuilder.url(url).post(new ProgressRequestBody(requestBody, progressCallback, timeStamp, requestTag));
helper.getHttpHelper().addHeadsToRequest(info, requestBuilder);
Request request = requestBuilder.build();
helper.setRequest(request);
helper.getHttpHelper().responseCallback(helper.doRequestSync(), progressCallback, OkMainHandler.RESPONSE_UPLOAD_CALLBACK, requestTag);
} catch (Exception e) {
showLog("上传文件失败:" + e.getMessage());
}
}
Aggregations