mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
add toURI encode option
This commit is contained in:
parent
351abcefdc
commit
8c486b4e4f
@ -25,7 +25,6 @@
|
||||
* 【crypto】 修复SM2算法在自定义密钥时无效问题(issue#I12P5I@Gitee)
|
||||
* 【core】 修复StopWatch.prettyPrint条件问题(issue#I12RAC@Gitee)
|
||||
* 【core】 修复StrBuilder.del无法删除最后一个字符的问题(issue#I12R14@Gitee)
|
||||
* 【core】 修复StrBuilder.del无法删除最后一个字符的问题(issue#I12R14@Gitee)
|
||||
* 【poi】 修复sax方式读取复用行导致的问题(issue#I12O0U@Gitee)
|
||||
* 【core】 修复ClassUtil循环调用问题
|
||||
* 【core】 修复MapConvert转换Bean为Map类型没有转换成功问题
|
||||
|
@ -21,6 +21,7 @@ import cn.hutool.core.io.IORuntimeException;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.net.URLEncoder;
|
||||
|
||||
/**
|
||||
@ -419,7 +420,7 @@ public class URLUtil {
|
||||
* @exception UtilException 包装URISyntaxException
|
||||
*/
|
||||
public static String getPath(String uriStr) {
|
||||
URI uri = null;
|
||||
URI uri;
|
||||
try {
|
||||
uri = new URI(uriStr);
|
||||
} catch (URISyntaxException e) {
|
||||
@ -460,26 +461,52 @@ public class URLUtil {
|
||||
* @exception UtilException 包装URISyntaxException
|
||||
*/
|
||||
public static URI toURI(URL url) throws UtilException {
|
||||
return toURI(url, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转URL为URI
|
||||
*
|
||||
* @param url URL
|
||||
* @param isEncode 是否编码参数中的特殊字符(默认UTF-8编码)
|
||||
* @return URI
|
||||
* @exception UtilException 包装URISyntaxException
|
||||
* @since 4.6.9
|
||||
*/
|
||||
public static URI toURI(URL url, boolean isEncode) throws UtilException {
|
||||
if (null == url) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return url.toURI();
|
||||
} catch (URISyntaxException e) {
|
||||
throw new UtilException(e);
|
||||
}
|
||||
|
||||
return toURI(url.toString(), isEncode);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转字符串为URI
|
||||
*
|
||||
* @param location 字符串路径
|
||||
* @return URI
|
||||
* @exception UtilException 包装URISyntaxException
|
||||
*/
|
||||
public static URI toURI(String location) throws UtilException {
|
||||
return toURI(location, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转字符串为URI
|
||||
*
|
||||
* @param location 字符串路径
|
||||
* @param isEncode 是否编码参数中的特殊字符(默认UTF-8编码)
|
||||
* @return URI
|
||||
* @exception UtilException 包装URISyntaxException
|
||||
* @since 4.6.9
|
||||
*/
|
||||
public static URI toURI(String location) throws UtilException {
|
||||
public static URI toURI(String location, boolean isEncode) throws UtilException {
|
||||
if(isEncode){
|
||||
location = encode(location);
|
||||
}
|
||||
try {
|
||||
return new URI(location.replace(" ", "%20"));
|
||||
return new URI(location);
|
||||
} catch (URISyntaxException e) {
|
||||
throw new UtilException(e);
|
||||
}
|
||||
@ -619,7 +646,7 @@ public class URLUtil {
|
||||
}
|
||||
|
||||
// 去除开头的\或者/
|
||||
body = body.replaceAll("^[\\/]+", StrUtil.EMPTY);
|
||||
body = body.replaceAll("^[\\\\/]+", StrUtil.EMPTY);
|
||||
// 替换多个\或/为单个/
|
||||
body = body.replace("\\", "/").replaceAll("//+", "/");
|
||||
if (isEncodeBody) {
|
||||
|
@ -42,6 +42,10 @@ public class URLUtilTest {
|
||||
url = "www.hutool.cn//aaa/bbb?a=1&b=2";
|
||||
normalize = URLUtil.normalize(url, true);
|
||||
Assert.assertEquals("http://www.hutool.cn/aaa/bbb?a=1&b=2", normalize);
|
||||
|
||||
url = "\\/www.hutool.cn//aaa/bbb?a=1&b=2";
|
||||
normalize = URLUtil.normalize(url, true);
|
||||
Assert.assertEquals("http://www.hutool.cn/aaa/bbb?a=1&b=2", normalize);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -8,6 +8,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import cn.hutool.core.io.IORuntimeException;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.util.URLUtil;
|
||||
import cn.hutool.http.HttpConnection;
|
||||
|
||||
@ -56,6 +57,7 @@ public class GlobalCookieManager {
|
||||
|
||||
Map<String, List<String>> cookieHeader;
|
||||
try {
|
||||
Console.log(URLUtil.toURI(conn.getUrl(), false));
|
||||
cookieHeader = cookieManager.get(URLUtil.toURI(conn.getUrl()), new HashMap<String, List<String>>(0));
|
||||
} catch (IOException e) {
|
||||
throw new IORuntimeException(e);
|
||||
|
Loading…
x
Reference in New Issue
Block a user