use of com.dtflys.forest.http.body.ObjectRequestBody in project forest by dromara.
the class HttpclientBodyBuilder method setFileBody.
@Override
protected void setFileBody(T httpReq, ForestRequest request, Charset charset, String contentType, LifeCycleHandler lifeCycleHandler) {
String boundary = request.getBoundary();
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
if (StringUtils.isNotEmpty(boundary)) {
entityBuilder.setBoundary(boundary);
}
// 解决文件名乱码问题
ForestJsonConverter jsonConverter = request.getConfiguration().getJsonConverter();
Charset httpCharset = charset;
Charset itemCharset = StandardCharsets.UTF_8;
if (charset != null) {
itemCharset = charset;
}
boolean needSetMode = false;
for (ForestRequestBody item : request.body()) {
if (item instanceof NameValueRequestBody) {
needSetMode = true;
NameValueRequestBody nameValueItem = (NameValueRequestBody) item;
String name = nameValueItem.getName();
Object value = nameValueItem.getValue();
String partContentType = nameValueItem.getContentType();
addMultipart(entityBuilder, name, value, partContentType, itemCharset, jsonConverter);
} else if (item instanceof ObjectRequestBody) {
Object obj = ((ObjectRequestBody) item).getObject();
if (obj == null) {
continue;
}
needSetMode = true;
Map<String, Object> attrs = jsonConverter.convertObjectToMap(obj);
for (Map.Entry<String, Object> entry : attrs.entrySet()) {
String name = entry.getKey();
Object value = entry.getValue();
addMultipart(entityBuilder, name, value, null, itemCharset, jsonConverter);
}
}
}
if (needSetMode) {
entityBuilder.setCharset(httpCharset);
entityBuilder.setMode(HttpMultipartMode.RFC6532);
}
List<ForestMultipart> multiparts = request.getMultiparts();
for (ForestMultipart multipart : multiparts) {
String name = multipart.getName();
String fileName = multipart.getOriginalFileName();
String partContentType = multipart.getContentType();
ContentType ctype = null;
if (StringUtils.isNotEmpty(partContentType)) {
ctype = ContentType.create(partContentType, httpCharset);
}
if (ctype == null) {
String mimeType = URLConnection.guessContentTypeFromName(fileName);
if (mimeType == null) {
// guess this is a video uploading
ctype = ContentType.create(com.dtflys.forest.backend.ContentType.MULTIPART_FORM_DATA, httpCharset);
} else {
ctype = ContentType.create(mimeType);
}
}
AbstractContentBody contentBody = null;
if (multipart.isFile()) {
contentBody = new HttpclientMultipartFileBody(request, multipart.getFile(), ctype, fileName, lifeCycleHandler);
} else {
contentBody = new HttpclientMultipartCommonBody(request, multipart, ctype, fileName, lifeCycleHandler);
}
entityBuilder.addPart(name, contentBody);
}
HttpEntity entity = entityBuilder.build();
httpReq.setEntity(entity);
}
use of com.dtflys.forest.http.body.ObjectRequestBody in project forest by dromara.
the class ForestRequest method addBody.
/**
* 添加 Map 类型 Body 数据
* <p>将 Map 的 key 作为键值对的 key
* <p>Map 的 value 作为键值对的 value
* <p>批量插入到请求的请求体中
*
* @param bodyMap 字段名
* @param contentType 该请求体项的Content-Type
* @return {@link ForestRequest}类实例
* @since 1.5.4
*/
public ForestRequest<T> addBody(Map bodyMap, String contentType) {
if (bodyMap == null) {
return this;
}
if (bodyMap.isEmpty()) {
addBody(new ObjectRequestBody(bodyMap));
return this;
}
for (Object key : bodyMap.keySet()) {
Object value = bodyMap.get(key);
addBody(String.valueOf(key), contentType, value);
}
return this;
}
Aggregations