如果如get方式的传的参数值是中文,进行如下解码不会发生乱码。post方式传的参数进行如下解码也是没问题。
public String getParameter(String name) {
String ss = this.request.getParameter(name);
if (StringUtil.isNullOrEmpty(ss)) {
return "";
}
if (ss != null) {
try
{
byte[] bs = ss.getBytes("ISO-8859-1");
for (int i = 0; i < bs.length; i++) {
byte b = bs[i];
if (b == 63)
break;
if (b <= 0)
{
if (b < 0) {
ss = new String(bs, "UTF-8");
break;
}
}
}
} catch (UnsupportedEncodingException e) { e.printStackTrace(); }
}
return ss;
}
本文提供了一种解决GET和POST请求中URL中文参数乱码的方法。通过将获取到的参数从ISO-8859-1编码转换为UTF-8编码来避免乱码问题。该方法适用于多种应用场景。
1064

被折叠的 条评论
为什么被折叠?



