我现在所有jsp页面为big5的编码格式,现有index.jsp页面中有username属性要存入到MySql数据中,MySql为utf-8的编码格式,myindex.jsp要取出username属性的值(username属性值是中文)为乱码..请问各位大虾该怎么转码啊...我的转码代码为
big5-->utf-8 index.jsp中存入MySql数据库
String user=request.getParameter("username");
byte [] bytes=user.getBytes("8859_1");
String username=new String(bytes,"UTF-8");//此处测试时username为乱码utf-8-->big5 myindex.jsp页面中获取username属性值
String user=rs.get("username");
byte [] bytes=user.getBytes("8859_1");
String username=new String(bytes,"BIG5");//此处测试时username为乱码
big5-->utf-8 index.jsp中存入MySql数据库
String user=request.getParameter("username");
byte [] bytes=user.getBytes("8859_1");
String username=new String(bytes,"UTF-8");//此处测试时username为乱码utf-8-->big5 myindex.jsp页面中获取username属性值
String user=rs.get("username");
byte [] bytes=user.getBytes("8859_1");
String username=new String(bytes,"BIG5");//此处测试时username为乱码
页面和数据库为utf8的情况下:
form提交:后台iso88591转gb后写数据库(IE)iso88591转utf(FF)
ajax提交:统一由iso88591转utf,无视浏览器
------------------
即,传统的form提交,简体和繁体的转码处理方式一样,简体能转对,繁体就没问题
ajax的话要iso转utf
楼主再试试吧,出乱码的原因实在不少,呵呵。
那么
request.setCharacterEncoding("BIG5");
然后就拿到了 String 了2 存入MySQL, 设置连接的参数, useUnicode=true&characterEncoding=UTF8保证保存和读取的都是正确的最后,为何你的MySQL不能使用BIG5作为默认编码呢?
入库之前做iso转big5的转码,入库前最好也print出来,看看是不是对的
出库无需转jsp也就是个servlet,所以servlet里能做的操作,jsp也一样能做。form表单提交默认还是get,除非指定为post。楼主不会是把转码和写数据库的逻辑放在跳转后的jsp里了吧。
我猜入库前作iso转utf8
出库作utf8转big5
试试入库时和出库时同时转成好几种然后打出来吧,哪种转法是对的一目了然,我通常烦了就直接用这种无赖方法。
public class Native {// "gb2312"到"Unicode"的转码
public String toUnicode(String str){
char[]arChar=str.toCharArray();
int iValue=0;
String uStr="";
for(int i=0;i<arChar.length;i++){
iValue=(int)str.charAt(i);
if(iValue<=256){
// uStr+="�"+Integer.toHexString(iValue)+";";
uStr+="\\u00"+Integer.toHexString(iValue);
}else{
// uStr+="&#x"+Integer.toHexString(iValue)+";";
uStr+="\\u"+Integer.toHexString(iValue);
}
}
return uStr;
}
// "gb2312"到"ISO-8859-1"的转码
public String iso2gb(String str) {
try {
str = new String(str.getBytes("ISO-8859-1"), "gb2312");
} catch (Exception e) {
System.out.println("Encoding Error!");
}
return str;
}
//"ISO-8859-1"到"gb2312"的转码
public String gb2iso(String str) {
try {
str = new String(str.getBytes("gb2312"), "ISO-8859-1");
} catch (Exception e) {
System.out.println("Encoding Error!");
}
return str;
}
public String getChinese(String InputString) throws Exception
{
byte b[]=InputString.getBytes("ISO-8859-1");
String OutputString=new String(b);
return OutputString;
}
public static void main(String args[]){
System.out.println(new Native().toUnicode("人"));
}}
上面的代码是想要从表单中取出字符串,在你调用下面的代码时Stringusername=newString(bytes,"UTF-8");//此处测试时username为乱码应该想一下,bytes内部保存的数据流是否是UTF-8?
在你的系统中显然不是,而是BIG5。所以上面的代码应该修改成Stringuser=request.getParameter("username");
byte[] bytes=user.getBytes("ISO-8859-1");
Stringusername=newString(bytes,"BIG5");//bytes中的字节流是BIG5编码当然WEB应用总体来说比较复杂,根据整体架构的不同,乱码问题也有不同的处理办法,建议看一下如何解决Java WEB应用中的乱码问题一文中的描述。第二个问题:数据库编码问题
将Java字符串保存到数据库时,MySQL会自动将unicode编码的Java字符串转换成它所对应的编码集,如UTF-8编码。所以保证你的字符串是unicode内码,而不是利用ISO-8859-1保存的其它字符集的字节数据流。
也就是说,将你的第一个问题解决好,第二个问题是由MySQL自己解决的。
关于字符串编码问题,以及ISO-8859-1字符集的作用,请参考Java字符串与字符集的基本概念一文。