现在在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分求最佳答案!

解决方案 »

  1.   

    这句byte[] b = gb_value.getBytes("gb2312")改成byte[] b = gb_value.getBytes("ISO-8859-1")试试.
      

  2.   

    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");试试看吧,没环境没测建议你把这个写成公用的函数,读GBK的字符串返回UTF-8的字符串。
      

  3.   

    最后记得释放一些东西,不知道直接把jvm的file.encoding改成UTF-8行不行假如可以动数据库的话,直接弄个弄个utf-8的数据库把原来的数据导进来,该下jvm的file.encoding改成UTF-8,然后改下<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />肯定能最快的解决问题
      

  4.   

    不止光改动<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />还要文件-〉右键-〉Properties-〉Text file encoding。这个文件编码格式也给改掉
      

  5.   

    String gb_value = resultSet.getString("name");
    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");
      

  6.   

    试过
    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");
    结果什么都没有显示出来呢!
    好郁闷啊!大家有什么好办法吗?