这种问题我是这样解决的,先判断这个字符串中有没有中文字符,有则不用转换,否则就转换。
public static String getGBKStringParam(HttpServletRequest request,String paramName) {
String rtn = request.getParameter(paramName);
if (rtn != null && !isGBKString(rtn)) {
try {
rtn = new String(rtn.getBytes("ISO8859_1"), "GBK");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return rtn;
}public static boolean isGBKString(String str) {
boolean rtn = false;
for (int i = 0; i < str.length(); i++) {
char chr = str.charAt(i);
if (chr > 255) {
rtn = true;
break;
}
}
return rtn;
}