mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix bug
This commit is contained in:
@@ -14,6 +14,7 @@ package org.dromara.hutool.http.client.body;
|
||||
|
||||
import org.dromara.hutool.core.io.stream.FastByteArrayOutputStream;
|
||||
import org.dromara.hutool.core.io.IoUtil;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
@@ -32,23 +33,39 @@ public interface HttpBody {
|
||||
void write(OutputStream out);
|
||||
|
||||
/**
|
||||
* 获取Content-Type
|
||||
* 获取Content-Type<br>
|
||||
* 根据实现不同,Content-Type可能包含编码信息,也可以不包含
|
||||
*
|
||||
* @return Content-Type值
|
||||
*/
|
||||
String getContentType();
|
||||
|
||||
/**
|
||||
* 获取指定编码的Content-Type,类似于:application/json;charset=UTF-8
|
||||
* 获取指定编码的Content-Type,类似于:application/json;charset=UTF-8<br>
|
||||
* 如果{@link #getContentType()} 已经包含编码信息,则编码信息一致直接返回,否则替换为指定编码。
|
||||
*
|
||||
* @param charset 编码
|
||||
* @return Content-Type
|
||||
* @see #getContentType()
|
||||
*/
|
||||
default String getContentType(final Charset charset){
|
||||
final String contentType = getContentType();
|
||||
if(null == contentType){
|
||||
default String getContentType(final Charset charset) {
|
||||
String contentType = getContentType();
|
||||
if (null == contentType) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final String charsetName = charset.name();
|
||||
if (StrUtil.endWithIgnoreCase(contentType, charsetName) || StrUtil.containsIgnoreCase(contentType, "boundary=")) {
|
||||
// 已经包含编码信息,且编码一致,无需再次添加
|
||||
// multipart无需添加charset
|
||||
return contentType;
|
||||
}
|
||||
|
||||
if (StrUtil.containsIgnoreCase(contentType, ";charset=")) {
|
||||
// 已经包含编码信息,但编码不一致,需要替换
|
||||
contentType = StrUtil.subBefore(contentType, ";charset=", true);
|
||||
}
|
||||
|
||||
return contentType + ";charset=" + charset.name();
|
||||
}
|
||||
|
||||
|
@@ -84,6 +84,12 @@ public class MultipartBody extends FormBody<MultipartBody> {
|
||||
return CONTENT_TYPE_MULTIPART_PREFIX + boundary;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getContentType(final Charset charset) {
|
||||
// multipart的Content-Type头指定"Content-Type; boundary=XXXX",编码无效
|
||||
return getContentType();
|
||||
}
|
||||
|
||||
/**
|
||||
* 写出Multiparty数据,不关闭流
|
||||
*
|
||||
|
Reference in New Issue
Block a user