servlet里 一个BufferedOutputStream 流 输出xml格式的字符。encoding="UTF-8" 的时候汉字浏览器显示乱码
encoding="gb2312" 的时候汉字显示正常 但是我需要一个UTF-8的格式的文件。于是我在拼接的时候,对汉字进行硬转码 
name = new String(name.getBytes("GBK"),"UTF-8");
....
或者这样
name = new String(name.getBytes("UTF-8"),"GBK");类似这样的转码一个个的都试了试,全部不行求方案。。

解决方案 »

  1.   

    程序很简单了,就是拼接了个xml串
    然后弄一个BufferedOutputStream
    os.write(xml.getBytes());
    浏览器查看
      

  2.   

    程序很简单了,就是拼接了个xml串 
    然后弄一个BufferedOutputStream 
    os.write(xml.getBytes()); 
      

  3.   


    答:楼主的对汉字进行硬转码 
    name = new String(name.getBytes("GBK"),"UTF-8"); 
    .... 
    或者这样 
    name = new String(name.getBytes("UTF-8"),"GBK"); 两种写法都是错误的.
    设String xml="你好!";
    则转成UTF-8的字节数据,就是:
    弄一个BufferedOutputStream 
    os.write(xml.getBytes("UTF-8")); 就行了.为何弄得那么复杂?
      

  4.   


        Document document = DocumentHelper.parseText(xml);
       OutputFormat format = OutputFormat.createPrettyPrint();
             format.setEncoding("utf-8");           
             XMLWriter writer = new XMLWriter(new FileOutputStream("test.xml"),format);
     writer.write(document);
    writer.close();不知道行不行,楼主该帖代码出来
      

  5.   

    name = new String(name.getBytes("UTF-8"),"gb2312"); 
      

  6.   

        public  String  gbkToUTF(String string){
            Charset asciiCharset=Charset.forName("Unicode");
            decord=asciiCharset.newDecoder();
            Charset  utfCharset= Charset.forName("UTF-8");
            encoder =utfCharset.newEncoder();
            ByteBuffer   utfBytes=null;
            CharBuffer  helpChars=null;
            try {
             // String GBKinfo = new String("hello胡国荣~@_@~123".getBytes("GBK"), "GBK");
                String GBKinfo = new String(string.getBytes("GBK"), "GBK");
                byte[]  bytes = GBKinfo.getBytes("Unicode");
                ByteBuffer  asciiBytes= ByteBuffer.wrap(bytes);
                helpChars = null;
                helpChars=decord.decode(asciiBytes);
                utfBytes=encoder.encode(helpChars);
                byte[] utfByte=utfBytes.array();
                String s=new String(utfByte,"UTF-8");
                System.out.println(s);
                return s;
            } catch (UnsupportedEncodingException ex) {
                ex.printStackTrace();
                return string;
            } catch (CharacterCodingException ex) {
                ex.printStackTrace();
                return string;
            }
        }
      //
     public String utf8Togbk(String string){
       Charset asciiCharset=Charset.forName("Unicode");
            decord=asciiCharset.newDecoder();
            Charset  utfCharset= Charset.forName("GBK");
            encoder =utfCharset.newEncoder();
            ByteBuffer  utfBytes=null;
            CharBuffer  helpChars=null;
            try {
              //String GBKinfo = new String("hello你好~@_@~123".getBytes("UTF-8"), "UTF-8");
              //byte[] bytes = GBKinfo.getBytes("Unicode");
                byte[] bytes =string.getBytes("Unicode");
                ByteBuffer  asciiBytes= ByteBuffer.wrap(bytes);
                helpChars = null;
                helpChars=decord.decode(asciiBytes);
                utfBytes=encoder.encode(helpChars);
                byte[] utfByte=utfBytes.array();
                String s=new String(utfByte,"GBK");
                System.out.println(s);
                return s;
            } catch (UnsupportedEncodingException ex) {
                ex.printStackTrace();
                return string;
            } catch (CharacterCodingException ex) {
                ex.printStackTrace();
                return string;
            }
     }   你试看看行不行,我测试是可以的哈,