简体   繁体   English

FreeMarker编码混乱

[英]FreeMarker encoding confusion

When I read an UTF-8 encoded template with FreeMarker, special chars are rendered correctly in the browser, although freeMarkerConfig.getDefaultEncoding() returns "Cp1252". 当我使用FreeMarker读取UTF-8编码模板时,虽然freeMarkerConfig.getDefaultEncoding()返回“Cp1252”,但在浏览器中正确呈现了特殊字符。 If I set freeMarkerConfig.setDefaultEncoding("UTF-8") , I see only question marks in the browser, although "UTF-8" is the actual encoding of the template file. 如果我设置freeMarkerConfig.setDefaultEncoding("UTF-8") ,我在浏览器中只看到问号,尽管“UTF-8”是模板文件的实际编码。 In every case the http header "Content-Type: text/html; charset=UTF-8" is sent. 在每种情况下,都会发送http标头“Content-Type:text / html; charset = UTF-8”。

Any idea what is wrong? 知道什么是错的吗?

Set the content type property into the FreeMarkerViewResolver. 将内容类型属性设置为FreeMarkerViewResolver。

Spring 4.2 example Spring 4.2的例子

@Bean
public FreeMarkerViewResolver freemarkerViewResolver() {
    FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
    resolver.setContentType("text/html; charset=utf-8");
    resolver.setCache(true);
    resolver.setPrefix("");
    resolver.setSuffix(".ftl.html");
    resolver.setRequestContextAttribute("rc");
    return resolver;
}

In the case you are using spring framework and a MimeMessage to send the email try setting content via a MimeMessagePreparator as follows (I skip the mimemessagepreparator method getMessagePreparator as the important thing is how to set content): 如果您使用spring框架和MimeMessage发送电子邮件,请尝试通过MimeMessagePreparator设置内容,如下所示(我跳过mimemessagepreparator方法getMessagePreparator,因为重要的是如何设置内容):

// Create the message helper from the received mimemessage on the preparator
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
// ... include the from, to, subject, freemarker html generation here... text is the String variable with generated html
// Set the content as follows instead of helper.setText(text, true);
helper.getMimeMessage().setContent(text, "text/html;charset=utf-8");

This worked for me and browsers are displaying chars correctly when sending emails. 这对我有用,浏览器在发送电子邮件时正确显示字符。

The implied classes are: 隐含的类是:

import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import javax.mail.internet.MimeMessage;

Also be sure that your workspace has a default encoding of UTF-8 for the template files by right clicking on them looking properties if you are using eclipse IDE. 如果您使用的是eclipse IDE,还要确保您的工作区对模板文件的默认编码为UTF-8,右键单击它们查找属性。

Hope this helps. 希望这可以帮助。

The output encoding is those of your java machinery. 输出编码是你的java机器的编码。 If you create an output file with UTF-FOO, and pass this output file to freemarker generation, the output encoding will be UTF-FOO. 如果使用UTF-FOO创建输出文件,并将此输出文件传递给freemarker生成,则输出编码将为UTF-FOO。

See Charset issues . 请参阅Charset问题

With ex. 与前。 code : 代码:

  Template templévénmts;
  BufferedWriter writ;
  OutputStreamWriter encodé;

  encodé = new OutputStreamWriter(
   new FileOutputStream(new File(f_dirDestination, résultat)), "UTF-8");
  writ = new BufferedWriter(
   encodé);
  templévénmts = f_freemarker.getTemplate(modèle);
  templévénmts.process(f_rootDatas, writ);
  writ.close();

You can also use FileWriterWithEncoding in commons io . 您也可以在commons io中使用FileWriterWithEncoding

Well, it definitely looks like regardless of the fact that you think your input is UTF-8 encoded, in reality it is indeed Cp1252 encoded. 好吧,无论您认为您的输入是UTF-8编码,它看起来确实如此,实际上它确实是Cp1252编码的。 Can you doublecheck, ie with a hex-editor. 你可以双重检查,即使用十六进制编辑器。 I second Istao's opinion -- try processing your template file to a local file and check the results. 我是第二个Istao的意见 - 尝试将模板文件处理为本地文件并检查结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM