1
我在我的項目中的以下問題:生成的html顯示?而不是國際字符
如果我在本地運行我的項目(從罐),該.ftlh
文件,我處理編譯就好了 - 它顯示了所有國際字符withouth的任何問題(如ą ę ć
)。
現在,如果我將我的項目部署到雲,所有這些國際字符顯示爲?
。我不知道怎麼回事,在.ftlh
文件,因爲我已經設置如下:
<#ftl encoding='UTF-8'>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
</head>
<body>
而且我的配置:
@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;
}
這就是我如何處理模板:
@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);
}
當我生成電子郵件,並在以下使用System.out.println:
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);
它將打印具有國際字符的HTML文件(當本地運行時)。但是當我在雲中運行時,它將顯示?
。
關於我在做什麼錯的任何想法?
我正在部署到雲,我會讓你知道在這個工作中的一些。 – uksz
工作得很好:)謝謝! – uksz
也可以刪除多餘的「UTF-8」-s。正如你有'configuration.setDefaultEncoding(「UTF-8」)','#ftl'頭文件和'getTemplate'參數是不必要的。真正的關鍵點是從字節(由setDefaultEncoding口授)和從字符('OutputStreamWriter')創建字節。 – ddekany