GBK编码项目解决中文乱码
前提:SSH框架下 项目编码类型:GBK
提交方式1:表单提交中文参数:
String keyword=””;
keyword=URLEncoder.encode(request.getParameter("keyword"), "ISO-8859-1");
keyword=this.unescape(keyword);
提交方式2:URL地址提交中文参数:
String keyword=””;
keyword=URLEncoder.encode(request.getParameter("keywords"), "GBK");
keyword=this.unescape(keyword);
使用的unescape(String str)方法如下
public static String unescape(String src) throws UnsupportedEncodingException {
StringBuffer tmp = new StringBuffer();
tmp.ensureCapacity(src.length());
int lastPos = 0, pos = 0;
char ch;
while (lastPos < src.length()) {
pos = src.indexOf("%", lastPos);
if (pos == lastPos) {
if (src.charAt(pos + 1) == \'u\') {
ch = (char) Integer.parseInt(src.substring(pos + 2, pos + 6), 16);
tmp.append(ch);
lastPos = pos + 6;
}
else {
ch = (char) Integer.parseInt(src.substring(pos + 1, pos + 3), 16);
tmp.append(ch);
lastPos = pos + 3;
}
}
else {
if (pos == -1) {
tmp.append(src.substring(lastPos));
lastPos = src.length();
}
else {
tmp.append(src.substring(lastPos, pos));
lastPos = pos;
}
}
}
String currentTmp = tmp.toString();
// return currentTmp;
return new String(currentTmp.getBytes("ISO-8859-1"),"GBK");
}