【发布时间】:2016-11-08 02:20:33
【问题描述】:
我有适用于所有欧洲国家和少数亚洲国家(如日本、中国、韩国)的 Unicode 字符。除日本、中国、韩语外,所有 Unicode 都适用于欧洲国家/地区。
以日本为例:
dear_name=\u30c7\u30a3\u30fc\u30e9\u30fc
以中国为例:
dear_name=\u4eb2\u7231\u7684
韩语示例:
dear_name=\uce5c\uc560\ud558\ub294
瑞典的例子(这个工作正常):
dear_name=Till
默认字符编码为 UTF-8。
Template template = VelocityFactory.getTemplate("test.vm", "UTF-8");
String messageText = VelocityFactory.merge(context, template, charset);
在调试合并方法时,我发现合并的结果在此处被抓取为中文、日文、韩文。
public static String merge(VelocityContext context, Template template, String charset) throws Exception {
String newResult = null;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
OutputStreamWriter streamWriter;
if(charset != null && charset.length() > 0) {
streamWriter = new OutputStreamWriter(outputStream, charset);
} else {
streamWriter = new OutputStreamWriter(outputStream);
}
template.merge(context, streamWriter);
streamWriter.close();
mergedResult = outputStream.toString();
outputStream.close();
return newResult;
}
}
以下是邮件模板,仅对于标题,它以正确的格式显示日文、中文和韩文,但不适用于正文:
<html>
<head>
<meta http-equiv="Content-Type" content="$contentType">
</head>
<body>
<div id="content">
<table border="0" cellpadding="0" cellspacing="0" style="margin-left: 0px;">
<tr>
<td>
<table border="0" cellpadding="0" cellspacing="0" class="textBody" style="margin-bottom: 120px;">
<tr>
<td valign="bottom" class="mainHeader" nowrap>
$velocityUtils.getMessage("test")
</td>
</tr>
<tr>
<td colspan="2">
<img src="$imageBar" class="clipped">
</td>
</tr>
</table>
<div id="info" class="textBody">$velocityUtils.getMessage("test1")<br><br></div>
</td>
</tr>
</table>
</div>
</body>
</html>
任何信息如何解决这个问题?如何正确编码??
【问题讨论】:
标签: java unicode encoding character-encoding streamwriter