From 914463b4d8a3ece0adcc5c7a962b8a19efbd987c Mon Sep 17 00:00:00 2001 From: zhaoxinhu <759054522@qq.com> Date: Thu, 18 Aug 2022 08:58:05 +0800 Subject: [PATCH 1/2] =?UTF-8?q?punycode=E8=BD=AC=E7=A0=81=E5=AE=8C?= =?UTF-8?q?=E6=95=B4=E5=9F=9F=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/hutool/core/codec/PunyCode.java | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/hutool-core/src/main/java/cn/hutool/core/codec/PunyCode.java b/hutool-core/src/main/java/cn/hutool/core/codec/PunyCode.java index c6474a4a0..5bca5f6b0 100644 --- a/hutool-core/src/main/java/cn/hutool/core/codec/PunyCode.java +++ b/hutool-core/src/main/java/cn/hutool/core/codec/PunyCode.java @@ -24,6 +24,35 @@ public class PunyCode { public static final String PUNY_CODE_PREFIX = "xn--"; + /** + * punycode转码域名 + * @param domain + * @return + * @throws UtilException + */ + private static String encodeDomain(String domain) throws UtilException{ + Assert.notNull(domain, "domain must not be null!"); + String[] split = domain.split("\\."); + StringBuilder outStringBuilder = new StringBuilder(); + for (String string: split) { + boolean encode = false; + for (int index=0; index Date: Thu, 18 Aug 2022 09:02:43 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E4=BF=AE=E9=A5=B0=E7=AC=A6=E4=B8=BApublic=E5=B9=B6=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0junit=E6=B5=8B=E8=AF=95=E4=BE=8B=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/cn/hutool/core/codec/PunyCode.java | 4 ++-- .../src/test/java/cn/hutool/core/codec/PunyCodeTest.java | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/codec/PunyCode.java b/hutool-core/src/main/java/cn/hutool/core/codec/PunyCode.java index 5bca5f6b0..613beaa5d 100644 --- a/hutool-core/src/main/java/cn/hutool/core/codec/PunyCode.java +++ b/hutool-core/src/main/java/cn/hutool/core/codec/PunyCode.java @@ -30,7 +30,7 @@ public class PunyCode { * @return * @throws UtilException */ - private static String encodeDomain(String domain) throws UtilException{ + public static String encodeDomain(String domain) throws UtilException{ Assert.notNull(domain, "domain must not be null!"); String[] split = domain.split("\\."); StringBuilder outStringBuilder = new StringBuilder(); @@ -154,7 +154,7 @@ public class PunyCode { * @return * @throws UtilException */ - private static String decodeDomain(String domain) throws UtilException{ + public static String decodeDomain(String domain) throws UtilException{ Assert.notNull(domain, "domain must not be null!"); String[] split = domain.split("\\."); StringBuilder outStringBuilder = new StringBuilder(); diff --git a/hutool-core/src/test/java/cn/hutool/core/codec/PunyCodeTest.java b/hutool-core/src/test/java/cn/hutool/core/codec/PunyCodeTest.java index d25b3b7e9..296ac2fd1 100644 --- a/hutool-core/src/test/java/cn/hutool/core/codec/PunyCodeTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/codec/PunyCodeTest.java @@ -15,4 +15,12 @@ public class PunyCodeTest { decode = PunyCode.decode("xn--Hutool-ux9js33tgln"); Assert.assertEquals(text, decode); } + + @Test + public void encodeEncodeDomainTest(){ + String domain = "赵新虎.中国"; + String strPunyCode = PunyCode.encodeDomain(domain); + String decode = PunyCode.decodeDomain(strPunyCode); + Assert.assertEquals(decode, domain); + } }