问题描述:我的jsp编码是GBK 后台的web.xlm默认也是GBK,但我想用Ajax的异步提交带中文的数据(我用的是json),这样到后台解析是乱码,存入数据库也是乱码。在网上找了好多方法看到问的很多解决的却很少。
网上大多解决都是用java API自带的String的一个方法,先将字符串转化成字节,然后再转utf-8。告诉大家你看到这个帖子的时候就不用再尝试这种解决办法了,那是个美丽扯。utf-8是一个汉字三个字节,当你是奇数个汉字或汉字与字母数字混合是还是有乱码。
下面是我的解决办法。
1、在jsp中我的数据输入是用textarea装载,用js给这个标签写了一个失去焦点事件
html:
<textarea name="memotemp" rows="4" id="memotemp"></textarea>
<input type="hidden" id="memoval" name = "reason" value=""/>
js:
//textarea 失去焦点事件
$("#memotemp").blur(function(){
var memotemp= $("#memotemp").val();
$("#memoval").val(escape(memotemp));
});
ps: 1.var memotemp= $("#memotemp").val();意思是得到textarea 里的内容。
2.escape(memotemp)意思是memotemp被escape转码(这里不多说escape你可以Google一下)。
3.$("#memoval").val(escape(memotemp));意思是将转码后的内容放入id为
memoval的input里。
实际上这个id为memoval的input内容是 我们要提交给后台的。这个到后台也是一堆类似url里的那些乱码。这时候你可以用一个自己写的java方法将escape转码过的再转回原先的真实的值。这样一来你就避免了Ajax提交过程中默认编码是utf-8而你的程序中都是GBK引起的乱码问题。
下面是java代码,非小弟本人所创在网上找的。
package com.cfcc.cams.aduitmanage.util;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
public class CommonStr {
private final static String[] hex = { "00", "01", "02", "03", "04", "05",
"06", "07", "08", "09", "0a", "0b", "0c", "0d", "0e", "0f", "10",
"11", "12", "13", "14", "15", "16", "17", "18", "19", "1a", "1b",
"1c", "1d", "1e", "1f", "20", "21", "22", "23", "24", "25", "26",
"27", "28", "29", "2a", "2b", "2c", "2d", "2e", "2f", "30", "31",
"32", "33", "34", "35", "36", "37", "38", "39", "3a", "3b", "3c",
"3d", "3e", "3f", "40", "41", "42", "43", "44", "45", "46", "47",
"48", "49", "4a", "4b", "4c", "4d", "4e", "4f", "50", "51", "52",
"53", "54", "55", "56", "57", "58", "59", "5a", "5b", "5c", "5d",
"5e", "5f", "60", "61", "62", "63", "64", "65", "66", "67", "68",
"69", "6a", "6b", "6c", "6d", "6e", "6f", "70", "71", "72", "73",
"74", "75", "76", "77", "78", "79", "7a", "7b", "7c", "7d", "7e",
"7f", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89",
"8a", "8b", "8c", "8d", "8e", "8f", "90", "91", "92", "93", "94",
"95", "96", "97", "98", "99", "9a", "9b", "9c", "9d", "9e", "9f",
"a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "aa",
"ab", "ac", "ad", "ae", "af", "b0", "b1", "b2", "b3", "b4", "b5",
"b6", "b7", "b8", "b9", "ba", "bb", "bc", "bd", "be", "bf", "c0",
"c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "ca", "cb",
"cc", "cd", "ce", "cf", "d0", "d1", "d2", "d3", "d4", "d5", "d6",
"d7", "d8", "d9", "da", "db", "dc", "dd", "de", "df", "e0", "e1",
"e2", "e3", "e4", "e5", "e6", "e7", "e8", "e9", "ea", "eb", "ec",
"ed", "ee", "ef", "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
"f8", "f9", "fa", "fb", "fc", "fd", "fe", "ff" };
private final static byte[] val = { 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,
0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,
0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,
0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,
0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x00, 0x01,
0x02, 0x03, 0x04,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3f, 0x3f,
0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,
0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,
0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f };
public static String unescape(String s) {
StringBuffer sbuf = new StringBuffer();
int i = 0;
int len = s.length();
while (i < len) {
int ch = s.charAt(i);
if (ch == '+') {// + : map to ' '
sbuf.append(' ');
} else if ('A' <= ch && ch <= 'Z') {// 'A'..'Z' : as it was
sbuf.append((char) ch);
} else if ('a' <= ch && ch <= 'z') {// 'a'..'z' : as it was
sbuf.append((char) ch);
} else if ('0' <= ch && ch <= '9') {// '0'..'9' : as it was
sbuf.append((char) ch);
} else if (ch == '-'
|| ch == '_'// unreserved : as it was
|| ch == '.' || ch == '!' || ch == '~' || ch == '*'
|| ch == '\' || ch == '(' || ch == ')') {
sbuf.append((char) ch);
} else if (ch == '%') {
int cint = 0;
if ('u' != s.charAt(i + 1)) { // %XX : map to ascii(XX)
cint = (cint << 4) | val[s.charAt(i + 1)];
cint = (cint << 4) | val[s.charAt(i + 2)];
i += 2;
} else {// %uXXXX : map to unicode(XXXX)
cint = (cint << 4) | val[s.charAt(i + 2)];
cint = (cint << 4) | val[s.charAt(i + 3)];
cint = (cint << 4) | val[s.charAt(i + 4)];
cint = (cint << 4) | val[s.charAt(i + 5)];
i += 5;
}
sbuf.append((char) cint);
}
i++;
}
return sbuf.toString();
}
}
这次你的问题应该可以解决了,我的表达能力不太好, 这套办法解决常用编码问题是绝对可以的。