Java实现Unicode与utf-8互转
// Unicode转中文
public static String getUnicodeChina(String unicode) {
/** 以 \ u 分割,因为java注释也能识别unicode,因此中间加了一个空格 */
String[] strs = unicode.split("\\\\u");
String returnStr = "";
// 由于unicode字符串以 \ u 开头,因此分割出的第一个字符是""。
for (int i = 1; i < strs.length; i++) {
returnStr += (char) Integer.valueOf(strs[i], 16).intValue();
}
return returnStr;
}
// 中文转Unicode
public static String getChinaUnicode(String china) {
char[] chars = china.toCharArray();
String returnStr = "";
for (int i = 0; i < chars.length; i++) {
returnStr += "\\u" + Integer.toString(chars[i], 16);
}
return returnStr;
}
Java实现Url与utf-8互转
utf-8转Url
public static String getFileUrlName(String agent, String filename)
throws UnsupportedEncodingException {
if (agent.contains("MSIE")) {
filename = URLEncoder.encode(filename, "utf-8")
.replaceAll("+", " ");
} else if (agent.contains("Firefox")) {
filename = "=?utf-8?B?"
+ new BASE64Encoder().encode(filename.getBytes("utf-8"))
+ "?=";
} else {
filename = URLEncoder.encode(filename, "utf-8");
}
return filename;
}
Url转utf-8
public static String getFileChinaName(String agent, String filename)
throws IOException {
if (agent.contains("MSIE")) {
filename = URLDecoder.decode(filename, "utf-8")
.replaceAll("+", " ");
} else if (agent.contains("Firefox")) {
filename = "=?utf-8?B?"
+ new String(new BASE64Decoder().decodeBuffer(filename),
"utf-8") + "?=";
} else {
filename = URLDecoder.decode(filename, "utf-8");
}
return filename;
}