I have the following problem in my project:
If I run my project locally (and from Jar), the .ftlh
file that I am processing compiles just fine - it shows all international characters withouth any problems (like ą ę ć
).
Now, if I deploy my project to cloud, all of those international characters are displayed as ?
. I have no idea whats going on, as I've set the following in the .ftlh
file:
<#ftl encoding='UTF-8'>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
</head>
<body>
And my configuration:
@Bean
public freemarker.template.Configuration templateConfiguration() throws IOException {
freemarker.template.Configuration configuration = new freemarker.template.Configuration(freemarker.template.Configuration.VERSION_2_3_24);
configuration.setTemplateLoader(new ClassTemplateLoader(this.getClass(), "/folder"));
configuration.setDefaultEncoding("UTF-8");
configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
configuration.setLogTemplateExceptions(false);
return configuration;
}
And this is how I process the template:
@Qualifier("templateConfiguration")
@Autowired
private Configuration configuration;
public void generateEmail(Order order, OutputStream outputStream) throws IOException, TemplateException {
Template template = configuration.getTemplate(EMAIL, "UTF-8");
OutputStreamWriter out = new OutputStreamWriter(outputStream);
template.process(order, out);
}
When I generate the email, and use System.out.println on the following:
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try{
emailsTemplateService.generateEmail(order, byteArrayOutputStream);
} catch (Exception e){
e.printStackTrace();
}
String htmlMessage = new String(byteArrayOutputStream.toByteArray(), StandardCharsets.UTF_8);
System.out.println(htmlMessage);
It will print the HTML file with international characters (when runs locally). But when I run in the cloud, it will display ?
instead.
Any ideas on what am I doing wrong?
You've used a specified character encoding in almost all cases, which is good. But you forgot one.
This:
OutputStreamWriter out = new OutputStreamWriter(outputStream);
Should be this:
OutputStreamWriter out = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8);
Since you didn't specify the encoding for the OutputStreamWriter, it took the platform default encoding, which was different for the two platforms on which you ran the code (and it was not UTF-8 on the cloud)