项目中经常用到前台传中文参数的问题,传中文很容易乱码,如果是form表单post提交,就不用考虑这些问题,但是有些情况不适宜使用form表单,一般都是js中将中文进行编码,但是在后台如何解码,这就存在一些差异性了。前台肯定需要将中文encodeURIComponent,但后台如何解码呢?
分析情况,有如下几种转码:
1、
public staticString getRealString(String input) throws UnsupportedEncodingException{
input = new String(input.getBytes("iso8859-1"),input);
return input;
}
2、
public staticString getRealString1(String input) throwsUnsupportedEncodingException{
input = java.net.URLDecoder.decode(input, "UTF-8");
return input;
}
以上两种解码方式,根据线上环境不同,从而可能能正常解码,可能解析完仍然乱码,这就跟线上服务器环境有关系啦,如果服务器进行一次转码,以上方式再进行转码,肯定仍然是乱的,还有记得在大于号项目中,如果是异步跨域的请求,后台必须用第二种方式解码。总之要依赖服务器环境,有没有特定的解码方案呢,答案是,有的。
3、
public staticString decodeURIComponent(String encodedURI) {
char actualChar;
StringBuffer buffer = new StringBuffer();
int bytePattern, sumb = 0;
for (int i =0, more = -1; i < encodedURI.length(); i++) {
actualChar = encodedURI.charAt(i);
switch (actualChar) {
case'%': {
actualChar = encodedURI.charAt(++i);
int hb = (Character.isDigit(actualChar)? actualChar -'0'
: 10 + Character.toLowerCase(actualChar) - 'a')& 0xF;
actualChar = encodedURI.charAt(++i);
int lb = (Character.isDigit(actualChar)? actualChar -'0'
: 10 + Character.toLowerCase(actualChar) - 'a')& 0xF;
bytePattern = (hb << 4) | lb;
break;
}
case'+': {
bytePattern = ' ';
break;
}
default: {
bytePattern = actualChar;
}
}
if ((bytePattern & 0xc0) == 0x80) {//10xxxxxx
sumb = (sumb << 6) | (bytePattern & 0x3f);
if (--more == 0)
buffer.append((char) sumb);
} else if((bytePattern & 0x80) == 0x00) { // 0xxxxxxx
buffer.append((char) bytePattern);
} else if((bytePattern & 0xe0) == 0xc0) { // 110xxxxx
sumb = bytePattern & 0x1f;
more = 1;
} else if((bytePattern & 0xf0) == 0xe0) { // 1110xxxx
sumb = bytePattern & 0x0f;
more = 2;
} else if((bytePattern & 0xf8) == 0xf0) { // 11110xxx
sumb = bytePattern & 0x07;
more = 3;
} else if((bytePattern & 0xfc) == 0xf8) { // 111110xx
sumb = bytePattern & 0x03;
more = 4;
} else {// 1111110x
sumb = bytePattern & 0x01;
more = 5;
}
}
return buffer.toString();
}
此种解码方式,不依赖于服务器环境,至少在编年史项目中,我最后采用这种方式解码,完全没有问题,大家也可以在项目中试试。