讀取模板的時候有一個編碼:

Template template = this.tempConfiguration.getTemplate(templatePath,"UTF-8");

生成檔案的時候使用編碼:

Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFile), "UTF-8"));

附(freeMarker生成靜態檔案的程式碼):

public class MakeHtml {
private Configuration tempConfiguration = new Configuration(); /**
* 注意:所有位置相對於根目錄
* @param path servletPath
* @param data
* @param templatePath 模板路徑
* @param targetHtmlPath 儲存路徑
*/
public void createHTML(String path, Map<String, Object> data,
String templatePath, String targetHtmlPath) {
try {
//filepath:ftl存放路徑(/template/file/static)
System.out.println(path);
this.tempConfiguration.setDirectoryForTemplateLoading(new File(path+"/freeMarker"));
//templatePath:ftl檔名稱(template.ftl)
Template template = this.tempConfiguration.getTemplate(templatePath,"UTF-8");
// 靜態頁面要存放的路徑
File htmlFile = new File(path + File.separator + targetHtmlPath);
if(!htmlFile.getParentFile().exists()) {
htmlFile.getParentFile().mkdirs();
}
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFile), "UTF-8"));
// 處理模版 map資料 ,輸出流
template.process(data, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
} public void setTempConfiguration(Configuration tempConfiguration) {
this.tempConfiguration = tempConfiguration;
}
}