3

我在将 UTF-8 文档正确转换为 HTML 时遇到问题。

当代码在 Solaris 上的 glassfish 服务器中运行时,会产生无效结果。

在我的 Windows 测试环境上运行时,结果是有效的。

两种环境都使用 Java 1.6.0_27

我包括两者的代码 - 我没有看到任何相关的区别。

应用服务器测试的输出如下(如果 UTF-8 经过此过程):

<CityName>Ð Ð¾Ð²Ð¾Ñ Ð¸Ð±Ð¸Ñ€Ñ Ðº</CityName>

<CityName>Ð&#157;овоÑ&#129;ибиÑ&#128;Ñ&#129;к</CityName>

请注意,第二个有几个实体编码,而第一个是纯 UTF-8。编码错误。

首先 - 应用服务器代码(记录器是一个 log4j 记录器 - 它在 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);
4

0 回答 0