mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
punycode转码完整域名
This commit is contained in:
parent
a60aa0ec47
commit
914463b4d8
@ -24,6 +24,35 @@ public class PunyCode {
|
|||||||
|
|
||||||
public static final String PUNY_CODE_PREFIX = "xn--";
|
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<string.length(); index++) {
|
||||||
|
char c = string.charAt(index);
|
||||||
|
if (!isBasic(c)) {
|
||||||
|
encode = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (encode) {
|
||||||
|
outStringBuilder.append(PunyCode.encode(string, true));
|
||||||
|
} else {
|
||||||
|
outStringBuilder.append(string);
|
||||||
|
}
|
||||||
|
outStringBuilder.append(".");
|
||||||
|
}
|
||||||
|
return outStringBuilder.substring(0, outStringBuilder.length() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将内容编码为PunyCode
|
* 将内容编码为PunyCode
|
||||||
*
|
*
|
||||||
@ -119,6 +148,27 @@ public class PunyCode {
|
|||||||
return output.toString();
|
return output.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解码punycode域名
|
||||||
|
* @param domain
|
||||||
|
* @return
|
||||||
|
* @throws UtilException
|
||||||
|
*/
|
||||||
|
private static String decodeDomain(String domain) throws UtilException{
|
||||||
|
Assert.notNull(domain, "domain must not be null!");
|
||||||
|
String[] split = domain.split("\\.");
|
||||||
|
StringBuilder outStringBuilder = new StringBuilder();
|
||||||
|
for (String string: split) {
|
||||||
|
if (string.startsWith(PUNY_CODE_PREFIX)) {
|
||||||
|
outStringBuilder.append(decode(string));
|
||||||
|
} else {
|
||||||
|
outStringBuilder.append(string);
|
||||||
|
}
|
||||||
|
outStringBuilder.append(".");
|
||||||
|
}
|
||||||
|
return outStringBuilder.substring(0, outStringBuilder.length() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 解码 PunyCode为字符串
|
* 解码 PunyCode为字符串
|
||||||
*
|
*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user