关于中文 问题是个常 见问题。
不知你是怎么做的。一般而言只要在每个JSP页面有这句:
<%@ page contentType="text/html;charset=gb2312" %>
另外在数据库配置文件中有个转化为中文 的函数toChinese()
当然可以是别的名字中有这句:
strvalue = new String(strvalue.getBytes("GBK"), "gb2312");
一般情况下中文 就没什么问题。以上是我的个人以验。
如果还不行就将
strvalue = new String(strvalue.getBytes("GBK"), "gb2312");
换为:
strvalue = new String(strvalue.getBytes("ISO8859-1"), "gb2312");
不知你是怎么做的。一般而言只要在每个JSP页面有这句:
<%@ page contentType="text/html;charset=gb2312" %>
另外在数据库配置文件中有个转化为中文 的函数toChinese()
当然可以是别的名字中有这句:
strvalue = new String(strvalue.getBytes("GBK"), "gb2312");
一般情况下中文 就没什么问题。以上是我的个人以验。
如果还不行就将
strvalue = new String(strvalue.getBytes("GBK"), "gb2312");
换为:
strvalue = new String(strvalue.getBytes("ISO8859-1"), "gb2312");
但是我把数据写进mysql的时候,却出现了问题!
从mysql数据读出来之前我是这么处理的
byte[] bytes;
bytes=rs.getBytes("xxx");
String str=new String(bytes,"ISO8859-1");
那么,这个str就是unicode的了!
麻烦大家帮我解决一下我插入数据时候的问题!
byte[] temp;
temp=xm.getBytes("iso-8859-1");
xm=new String(temp);
我觉得是这样,如果不对就SORRY!!
因为中文问题没有固定的解决方法,大体上就是两个编码的转换但是这和你的os和容器有很大关系。
public static String U2C(String s)
{
String str = s;
try
{
if ( str == null ) return str;
byte[] u = str.getBytes("iso-8859-1");
return new String(u,"GB2312");
}
catch(java.io.UnsupportedEncodingException e)
{
e.printStackTrace();
return str;
}
}
<%@ page contentType="text/html;charset=gb2312" %>
就可以了
public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); ServletOutputStream out = res.getOutputStream(); OutputStreamWriter ow = new OutputStreamWriter(out,"GB2312"); ow.write("这是测试"); ow.flush(); ow.close(); }
String zx01Value = "你好";
写入时也用String temp = new String(zx01Value.getBytes("GBK"),"ISO8859_1");试
<%response.setContentType("text/html;charset=iso8859_1");%><meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=GB2312"> //把数据库中取得的结果转化--显示汉字
public String GS(String str)
{ try
{ String temp_p=str;
byte[] temp_t=temp_p.getBytes("GBK");
String temp=new String(temp_t,"ISO8859_1");
return temp;
}
catch(Exception e) { return "null";}
} //入库前把表单中提交的中文进行转换
public String toGB(String iso)
{ String gb=null;
if (iso != null)
{ try { gb=new String(iso.getBytes("ISO8859_1"),"GB2312"); }
catch (Exception e) { gb=null; }
}
return gb;
}
String url = "jdbc:mysql://localhost/coffeebreak?" +"useUnicode=true&characterEncoding=gb2312";
con = DriverManager.getConnection (url,"username", "password");