我在将UTF-8文档正确转换为HTML时遇到问题。
当代码在Solaris上的glassfish服务器内运行时,会产生无效结果。
在Windows上的测试环境中运行时,结果有效。
两种环境都使用Java 1.6.0_27
我包括两者的代码 - 我没有看到任何相关的差异。
app server test的输出如下(如果UTF-8完成此过程):
<CityName>Ð Ð¾Ð²Ð¾Ñ Ð¸Ð±Ð¸Ñ€Ñ Ðº</CityName>
<CityName>Новосибирск</CityName>
请注意,第二个有几个实体编码,而第一个是纯UTF-8。编码是错误的。
首先 - 应用程序服务器代码(logger是一个log4j Logger - 它在test13上显示出良好的结果,在test14上显示不好):
logger.info("===test13:"+descr+req);
DocumentBuilderFactory domFactory = DocumentBuilderFactory .newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
Document doc = domBuilder.parse(new InputSource(new StringReader(req)));
logger.info("===test14:"+XmlHelper.domToString(doc));
然后,测试代码 - “out”在两种情况下都显示出良好的数据:
PrintStream out = new PrintStream(System.out, true, "UTF-8");
String inmsg = TestRvcMessage.readUtfFile(fname);
inmsg = inmsg.trim();
out.println("INPUT:"+inmsg);
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
Document doc = domBuilder.parse(new InputSource(new StringReader(inmsg)));
out.println("OUTPUT:"+xmlHelper.domToString(doc);