Display chinese characters as it is from velocity file

286 views Asked by At

We are using Apache Velocity framework to store email template format in .vm files. Those emails have chinese character content. When retrieved in Java and displayed as String, the chinese characters become questions marks.

We use this code to extract the contents into a string

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);

What if we want to see the actual chinese characters in the System.out?

PS: This is different from other threads because the line below did not work for me.

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

Chinese characters are still turned to question marks when printed despite adding "UTF-8"

0

There are 0 answers