现在在oracle db中保存的是gb或者big5编码的数据,在原来的网页上显示的是gb或者big5,所以从数据库中读出来的东西在网页上显示并没有问题。但是现在客户要求所有的网页都要转成utf-8编码,但db不能转编码。我尝试过把JSP中
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
转成
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />然后在jsp中加入
String gb_value = resultSet.getString("name");
byte[] b = gb_value.getBytes("gb2312");
String utf8_name = new String(b, "UTF-8"); 结果输出<%=utf8_name%>时,显示乱码?由于只有jsp文件,没有web.xml。所以我本来想有filter来做都不可以。请问各位大虾有什么办法帮帮小弟呢?100分求最佳答案!
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
转成
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />然后在jsp中加入
String gb_value = resultSet.getString("name");
byte[] b = gb_value.getBytes("gb2312");
String utf8_name = new String(b, "UTF-8"); 结果输出<%=utf8_name%>时,显示乱码?由于只有jsp文件,没有web.xml。所以我本来想有filter来做都不可以。请问各位大虾有什么办法帮帮小弟呢?100分求最佳答案!
InputStreamReader reader = InputStreamReader(in, "GBK");
ByteArrayOutputStream out = new ByteArrayOutputStream();
OutputStreamWriter writer = OutputStreamWriter(out, "UTF-8");
int i = 0;
while((i = reader.read()) != -1)
writer.write(i);
String utf8_name = out.toString("UTF-8");试试看吧,没环境没测建议你把这个写成公用的函数,读GBK的字符串返回UTF-8的字符串。
byte[] b = gb_value.getBytes("gb2312");
String utf8_name = new String(b, "UTF-8");
换成:
String utf8_name = new String((new String(gb_value )).getBytes("gb2321"),"utf-8");
byte[] b = gb_value.getBytes("gb2312")改成byte[] b = gb_value.getBytes("ISO-8859-1"),
结果还是乱码。也试过
ByteArrayInputStream in = new ByteArrayInputStream(gb_value.getBytes());
InputStreamReader reader = InputStreamReader(in, "GBK");
ByteArrayOutputStream out = new ByteArrayOutputStream();
OutputStreamWriter writer = OutputStreamWriter(out, "UTF-8");
int i = 0;
while((i = reader.read()) != -1)
writer.write(i);
String utf8_name = out.toString("UTF-8");
结果什么都没有显示出来呢!
好郁闷啊!大家有什么好办法吗?