将XML的Document对象转换为UTF-8编码的字符串时,通过UE查看UTF-8编码的内容时,时不时的会有些中文出现乱码变成其它字,大部分中文是正常的。也不是所有有中文的情况都会出现这样的情况,只有少数情况会出现这样的情况,但也够致命的,因为涉及到多系统交互,对方的系统在收到我发过去带有中文乱码的报文时就会处理报异常了。
刚开始以为是JDOM的问题,但是我换了DOM4J、W3C这两种方式进行转换是一样的效果,网上有些地方说有可能是JDK的问题,这个没有证实,以下是我的部分代码,请知道或曾遇到过这样问题的朋友给个建设:
/*将字符串转换为XML Document的方法*/
public static Document getDocument(String xmlString) throws JDOMException, IOException
{
SAXBuilder builder = new SAXBuilder();
Document anotherDocument = builder.build(new StringReader(xmlString));
return anotherDocument;
}
/*将XML Document转换为字符串的方法*/
public static String toXML(Document xmlDoc, String encoding) throws IOException
{
ByteArrayOutputStream byteRep = new ByteArrayOutputStream();
Format format = Fo
XMLOutputter写入 ByteArrayOutputStream,中文出现乱码问题
最新推荐文章于 2024-04-24 10:23:32 发布