// 按照utf-8的格式解码
public static String decodeStringByUTF8(String str) {
if (isBlank(str))
return "";
try {
return URLDecoder.decode(str, "utf-8");
} catch (UnsupportedEncodingException e) {
}
return "";
}
// 按照utf-8的格式编码
public static String encodeStringByUTF8(String str) {
if (isBlank(str))
return "";
try {
return URLEncoder.encode(str, "utf-8");
} catch (UnsupportedEncodingException e) {
}
return "";
}
// 判断是字符串否为空
public static boolean isBlank(String str) {
return (str == null || str.trim().equals(""));
}
这个函数非常有用,在项目中很多时候是和encodeURIComponent配合使用的,在前台jsp页面中代码如下:
在后台action中对author做如下处理,即可还原数据,不会出现编码问题: