0

我们使用 Apache Velocity 框架将电子邮件模板格式存储在 .vm 文件中。那些电子邮件有汉字内容。在 Java 中检索并显示为字符串时,汉字变成问号。

我们使用此代码将内容提取到字符串中

VelocityEngine engine = new VelocityEngine();
engine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
engine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
engine.init();
VelocityContext context = new VelocityContext();
context.put("params", params);
StringWriter writer = new StringWriter();
Template template = engine.getTemplate("testfile.vm","UTF-8");
template.merge(context, writer);
String message = writer.toString();
System.out.println(message);

如果我们想在 System.out 中看到实际的汉字怎么办?

PS:这与其他线程不同,因为下面的行对我不起作用。

Template template = Velocity.getTemplate("testfile.vm", "UTF-8");

尽管添加了“UTF-8”,汉字在打印时仍然变成问号

4

0 回答 0