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");类似这样的转码一个个的都试了试,全部不行求方案。。
encoding="gb2312" 的时候汉字显示正常 但是我需要一个UTF-8的格式的文件。于是我在拼接的时候,对汉字进行硬转码
name = new String(name.getBytes("GBK"),"UTF-8");
....
或者这样
name = new String(name.getBytes("UTF-8"),"GBK");类似这样的转码一个个的都试了试,全部不行求方案。。
解决方案 »
- XMLBean的问题!!!灌水帖(坐等解决方案)
- 一个编码的奇怪现象,散分
- 为什么程序中有退出的代码java还要抛出异常呢?
- 高手请进,这样总不能说classpath设置不对吧
- org.hibernate.MappingException: entity class not found:
- 新学freemarker,请教一个简单的问题
- 为何用Spring?
- hibernatetemplate.save在保存多对多时候保存不了
- 散分~~~问各位高手问题
- jsp如何与applet交互
- weblogic出现cannot load TLD: weblogic.xml.dom.ChildCountException: missing child tagclass in tag错误是什么原因?
- 朋友们 帮帮忙
然后弄一个BufferedOutputStream
os.write(xml.getBytes());
浏览器查看
然后弄一个BufferedOutputStream
os.write(xml.getBytes());
答:楼主的对汉字进行硬转码
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")); 就行了.为何弄得那么复杂?
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();不知道行不行,楼主该帖代码出来
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;
}
} 你试看看行不行,我测试是可以的哈,