Browse Source

add toURI encode option

Looly 6 years ago
parent
commit
8c486b4e4f

+ 0 - 1
CHANGELOG.md

@@ -25,7 +25,6 @@
 * 【crypto】      修复SM2算法在自定义密钥时无效问题(issue#I12P5I@Gitee)
 * 【crypto】      修复SM2算法在自定义密钥时无效问题(issue#I12P5I@Gitee)
 * 【core】        修复StopWatch.prettyPrint条件问题(issue#I12RAC@Gitee)
 * 【core】        修复StopWatch.prettyPrint条件问题(issue#I12RAC@Gitee)
 * 【core】        修复StrBuilder.del无法删除最后一个字符的问题(issue#I12R14@Gitee)
 * 【core】        修复StrBuilder.del无法删除最后一个字符的问题(issue#I12R14@Gitee)
-* 【core】        修复StrBuilder.del无法删除最后一个字符的问题(issue#I12R14@Gitee)
 * 【poi】         修复sax方式读取复用行导致的问题(issue#I12O0U@Gitee)
 * 【poi】         修复sax方式读取复用行导致的问题(issue#I12O0U@Gitee)
 * 【core】        修复ClassUtil循环调用问题
 * 【core】        修复ClassUtil循环调用问题
 * 【core】        修复MapConvert转换Bean为Map类型没有转换成功问题
 * 【core】        修复MapConvert转换Bean为Map类型没有转换成功问题

+ 36 - 9
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.IoUtil;
 import cn.hutool.core.io.resource.ResourceUtil;
 import cn.hutool.core.io.resource.ResourceUtil;
 import cn.hutool.core.lang.Assert;
 import cn.hutool.core.lang.Assert;
+import cn.hutool.core.lang.Console;
 import cn.hutool.core.net.URLEncoder;
 import cn.hutool.core.net.URLEncoder;
 
 
 /**
 /**
@@ -419,7 +420,7 @@ public class URLUtil {
 	 * @exception UtilException 包装URISyntaxException
 	 * @exception UtilException 包装URISyntaxException
 	 */
 	 */
 	public static String getPath(String uriStr) {
 	public static String getPath(String uriStr) {
-		URI uri = null;
+		URI uri;
 		try {
 		try {
 			uri = new URI(uriStr);
 			uri = new URI(uriStr);
 		} catch (URISyntaxException e) {
 		} catch (URISyntaxException e) {
@@ -460,26 +461,52 @@ public class URLUtil {
 	 * @exception UtilException 包装URISyntaxException
 	 * @exception UtilException 包装URISyntaxException
 	 */
 	 */
 	public static URI toURI(URL url) throws UtilException {
 	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) {
 		if (null == url) {
 			return null;
 			return null;
 		}
 		}
-		try {
-			return url.toURI();
-		} catch (URISyntaxException e) {
-			throw new UtilException(e);
-		}
+
+		return toURI(url.toString(), isEncode);
 	}
 	}
 
 
 	/**
 	/**
 	 * 转字符串为URI
 	 * 转字符串为URI
-	 * 
+	 *
 	 * @param location 字符串路径
 	 * @param location 字符串路径
 	 * @return URI
 	 * @return URI
 	 * @exception UtilException 包装URISyntaxException
 	 * @exception UtilException 包装URISyntaxException
 	 */
 	 */
 	public static URI toURI(String location) throws UtilException {
 	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, boolean isEncode) throws UtilException {
+		if(isEncode){
+			location = encode(location);
+		}
 		try {
 		try {
-			return new URI(location.replace(" ", "%20"));
+			return new URI(location);
 		} catch (URISyntaxException e) {
 		} catch (URISyntaxException e) {
 			throw new UtilException(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("//+", "/");
 		body = body.replace("\\", "/").replaceAll("//+", "/");
 		if (isEncodeBody) {
 		if (isEncodeBody) {

+ 4 - 0
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";
 		url = "www.hutool.cn//aaa/bbb?a=1&b=2";
 		normalize = URLUtil.normalize(url, true);
 		normalize = URLUtil.normalize(url, true);
 		Assert.assertEquals("http://www.hutool.cn/aaa/bbb?a=1&b=2", normalize);
 		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
 	@Test

+ 2 - 0
hutool-http/src/main/java/cn/hutool/http/cookie/GlobalCookieManager.java

@@ -8,6 +8,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Map;
 
 
 import cn.hutool.core.io.IORuntimeException;
 import cn.hutool.core.io.IORuntimeException;
+import cn.hutool.core.lang.Console;
 import cn.hutool.core.util.URLUtil;
 import cn.hutool.core.util.URLUtil;
 import cn.hutool.http.HttpConnection;
 import cn.hutool.http.HttpConnection;
 
 
@@ -56,6 +57,7 @@ public class GlobalCookieManager {
 		
 		
 		Map<String, List<String>> cookieHeader;
 		Map<String, List<String>> cookieHeader;
 		try {
 		try {
+			Console.log(URLUtil.toURI(conn.getUrl(), false));
 			cookieHeader = cookieManager.get(URLUtil.toURI(conn.getUrl()), new HashMap<String, List<String>>(0));
 			cookieHeader = cookieManager.get(URLUtil.toURI(conn.getUrl()), new HashMap<String, List<String>>(0));
 		} catch (IOException e) {
 		} catch (IOException e) {
 			throw new IORuntimeException(e);
 			throw new IORuntimeException(e);