From 8c486b4e4fad7f8955986fbbcd4752f4b64d8f3f Mon Sep 17 00:00:00 2001 From: Looly Date: Mon, 30 Sep 2019 16:35:32 +0800 Subject: [PATCH] add toURI encode option --- CHANGELOG.md | 1 - .../java/cn/hutool/core/util/URLUtil.java | 45 +++++++++++++++---- .../java/cn/hutool/core/util/URLUtilTest.java | 4 ++ .../http/cookie/GlobalCookieManager.java | 2 + 4 files changed, 42 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f34b5e20..0562c290f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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类型没有转换成功问题 diff --git a/hutool-core/src/main/java/cn/hutool/core/util/URLUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/URLUtil.java index 887692f2e..e9c35797f 100644 --- a/hutool-core/src/main/java/cn/hutool/core/util/URLUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/URLUtil.java @@ -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) { diff --git a/hutool-core/src/test/java/cn/hutool/core/util/URLUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/util/URLUtilTest.java index 741741e47..f735fb384 100644 --- a/hutool-core/src/test/java/cn/hutool/core/util/URLUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/util/URLUtilTest.java @@ -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 diff --git a/hutool-http/src/main/java/cn/hutool/http/cookie/GlobalCookieManager.java b/hutool-http/src/main/java/cn/hutool/http/cookie/GlobalCookieManager.java index e2ee735e3..1acf5b182 100644 --- a/hutool-http/src/main/java/cn/hutool/http/cookie/GlobalCookieManager.java +++ b/hutool-http/src/main/java/cn/hutool/http/cookie/GlobalCookieManager.java @@ -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> cookieHeader; try { + Console.log(URLUtil.toURI(conn.getUrl(), false)); cookieHeader = cookieManager.get(URLUtil.toURI(conn.getUrl()), new HashMap>(0)); } catch (IOException e) { throw new IORuntimeException(e);