try{
s = new String(s.getBytes(), "UTF8");
byte[] bytes = s.getBytes("UTF8");
System.out.println("length: "+bytes.length);
    }catch(java.io.UnsupportedEncodingException uee){
uee.printStackTrace();
    }
改为:
try{
s = new String(s.getBytes(), "GBK");
byte[] bytes = s.getBytes("UTF8");
System.out.println("length: "+bytes.length);
    }catch(java.io.UnsupportedEncodingException uee){
uee.printStackTrace();
    }
看看。

解决方案 »

  1.   


    static void toUTF8(String s)
        {
          System.out.println("Source string: "+s);
          try
          {
            byte[] bytes = s.getBytes("ISO-8859-1");
            String strReturn = new String(bytes,"UTF-8");
            System.out.println(strReturn);
            System.out.println( "length: "+strReturn.length() );
          }
          catch(java.io.UnsupportedEncodingException uee)
          {
            uee.printStackTrace();
          }
      }
      

  2.   

    http://www.csdn.net/expert/topic/589/589476.xml?temp=.701214
      

  3.   

    应该直接
    byte[] bytes = s.getBytes("UTF8");
    你多了一个NEW所以才这样,字符转换时不需要的。
    长度这样是4。
      

  4.   

    不多那个new也不成,我很早以前就试过了,得到一个空串
    最后只好自己写了个方法
      

  5.   

    new String(s.getBytes(fromCode),toCode);
      

  6.   

    我明白了一些:自己对String的getByte()理解有误。先给出一些分数,等我系统测试之后结帐。先谢谢各位了
      

  7.   

    public byte[] getBytes(String enc)
                    throws UnsupportedEncodingException
    Convert this String into bytes according to the specified character encoding, 
    storing the result into a new byte array.
    这个函数是将字符串按指定的编码方案编码,返回其编码
    所以,想知道字符串的UTF8编码的话,可以使用getBytes("UTF8")
    byte[] bytes = "中文".getBytes("UTF8");
    System.out.println("length: "+bytes.length);
    >>length: 6
      

  8.   

    那我要是把中文字符编码成utf8写入数据库,再读出数据显示在jsp页面上怎么做