mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
add method
This commit is contained in:
parent
96c83cb16b
commit
aafcecc3d9
@ -10,6 +10,7 @@
|
||||
* 【http 】 HttpBase增加clearHeaders方法(issue#I49P23@Gitee)
|
||||
* 【core 】 CsvWriter的write和writeBeans参数改为Iterable(issue#I49O4S@Gitee)
|
||||
* 【core 】 BitStatusUtil添加来源声明(issue#1824@Github)
|
||||
* 【core 】 UrlQuery.build增加重载,支持可选是否转义(issue#I4AIX1@Gitee)
|
||||
*
|
||||
### 🐞Bug修复
|
||||
* 【core 】 修复FuncKey函数无效问题
|
||||
|
@ -173,9 +173,9 @@ public class UrlQuery {
|
||||
}
|
||||
}
|
||||
|
||||
if(i - pos == len){
|
||||
if (i - pos == len) {
|
||||
// 没有任何参数符号
|
||||
if(queryStr.startsWith("http") || queryStr.contains("/")){
|
||||
if (queryStr.startsWith("http") || queryStr.contains("/")) {
|
||||
// 可能为url路径,忽略之
|
||||
return this;
|
||||
}
|
||||
@ -216,6 +216,18 @@ public class UrlQuery {
|
||||
* @return URL查询字符串
|
||||
*/
|
||||
public String build(Charset charset) {
|
||||
return build(charset, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建URL查询字符串,即将key-value键值对转换为key1=v1&key2=&key3=v3形式
|
||||
*
|
||||
* @param charset encode编码,null表示不做encode编码
|
||||
* @param isEncode 是否转义键和值
|
||||
* @return URL查询字符串
|
||||
* @since 5.7.13
|
||||
*/
|
||||
public String build(Charset charset, boolean isEncode) {
|
||||
if (MapUtil.isEmpty(this.query)) {
|
||||
return StrUtil.EMPTY;
|
||||
}
|
||||
@ -232,10 +244,10 @@ public class UrlQuery {
|
||||
}
|
||||
key = entry.getKey();
|
||||
if (null != key) {
|
||||
sb.append(URLUtil.encodeAll(StrUtil.str(key), charset));
|
||||
sb.append(toStr(key, charset, isEncode));
|
||||
value = entry.getValue();
|
||||
if (null != value) {
|
||||
sb.append("=").append(URLUtil.encodeAll(StrUtil.str(value), charset));
|
||||
sb.append("=").append(toStr(value, charset, isEncode));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -287,4 +299,21 @@ public class UrlQuery {
|
||||
this.query.put(URLUtil.decode(value, charset), null);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 键值对的{@link CharSequence}转换为String,可选是否转义
|
||||
*
|
||||
* @param str 原字符串
|
||||
* @param charset 编码,只用于encode中
|
||||
* @param isEncode 是否转义
|
||||
* @return 转换后的String
|
||||
* @since 5.7.13
|
||||
*/
|
||||
private static String toStr(CharSequence str, Charset charset, boolean isEncode) {
|
||||
String result = StrUtil.str(str);
|
||||
if (isEncode) {
|
||||
result = URLUtil.encodeAll(result, charset);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -442,7 +442,9 @@ public class HttpUtil {
|
||||
* @param paramMap 表单数据
|
||||
* @param charsetName 编码
|
||||
* @return url参数
|
||||
* @deprecated 请使用 {@link #toParams(Map, Charset)}
|
||||
*/
|
||||
@Deprecated
|
||||
public static String toParams(Map<String, Object> paramMap, String charsetName) {
|
||||
return toParams(paramMap, CharsetUtil.charset(charsetName));
|
||||
}
|
||||
@ -461,7 +463,26 @@ public class HttpUtil {
|
||||
* @return url参数
|
||||
*/
|
||||
public static String toParams(Map<String, ?> paramMap, Charset charset) {
|
||||
return URLUtil.buildQuery(paramMap, charset);
|
||||
return toParams(paramMap, charset, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将Map形式的Form表单数据转换为Url参数形式<br>
|
||||
* paramMap中如果key为空(null和"")会被忽略,如果value为null,会被做为空白符("")<br>
|
||||
* 会自动url编码键和值
|
||||
*
|
||||
* <pre>
|
||||
* key1=v1&key2=&key3=v3
|
||||
* </pre>
|
||||
*
|
||||
* @param paramMap 表单数据
|
||||
* @param charset 编码,null表示不encode键值对
|
||||
* @param isEncode 是否转义键和值
|
||||
* @return url参数
|
||||
* @since 5.7.13
|
||||
*/
|
||||
public static String toParams(Map<String, ?> paramMap, Charset charset, boolean isEncode) {
|
||||
return UrlQuery.of(paramMap).build(charset, isEncode);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user