首先感謝JScud提供的好文章。《
使用FreeMarker生成Html靜態(tài)文件(實(shí)例)》
????? 在我們的項(xiàng)目中也用到了Freemarker生成靜態(tài)文件。不過這里我要說的是編碼的問題。我們的項(xiàng)目使用的都是UTF-8編碼,我直接使用 飛云小俠 提供的方法生成的文件在UTF-8編碼下察看是亂碼,而GBK正常(后來發(fā)現(xiàn)因?yàn)槲矣玫闹形牟僮飨到y(tǒng)所以用GBK查看正常)。
????? 當(dāng)然我把Freemarker的配置都改成了UTF-8,我的模版文件也是UTF-8編碼的。下面是原來的代碼
????public?void?setTemplatePath(Resource?templatePath)?{
????????this.templatePath?=?templatePath;
????????//設(shè)置freemarker的參數(shù)
????????freemarkerCfg?=?new?Configuration();
????????try?{
????????????freemarkerCfg.setDirectoryForTemplateLoading(this.templatePath.getFile());
????????????freemarkerCfg.setObjectWrapper(new?DefaultObjectWrapper());
????????????freemarkerCfg.setDefaultEncoding("UTF-8");
????????}?catch?(IOException?ex)?{
????????????throw?new?SystemException("No?Directory?found,please?check?you?config.");
????????}
????}
????/**
?????*?生成靜態(tài)文件
?????*?@param?templateFileName?模版名稱eg:(biz/order.ftl)
?????*?@param?propMap?用于處理模板的屬性O(shè)bject映射?
?????*?@param?htmlFilePath?要生成的靜態(tài)文件的路徑,相對設(shè)置中的根路徑,例如?"/biz/2006/5/"?
?????*?@param?htmlFileName?要生成的文件名,例如?"123.htm"?
?????*?@return
?????*/
????private?boolean?buildHtml(String?templateFileName,Map?propMap,?String?htmlFilePath,String?htmlFileName){
????????try?{
????????????Template?template?=?freemarkerCfg.getTemplate(templateFileName);
????????????template.setEncoding("UTF-8");
????????????//創(chuàng)建生成文件目錄
????????????creatDirs(buildPath.getFilename(),htmlFilePath);
????????????File?htmlFile?=?new?File(buildPath?+?htmlFilePath?+?htmlFileName);
????????????Writer?out?=?new?BufferedWriter(new?OutputStreamWriter(new?FileOutputStream(htmlFile)));
????????????template.process(propMap,out);
????????????out.flush();
????????????return?true;
????????}?catch?(TemplateException?ex){
????????????log.error("Build?Error"+templateFileName,ex);
????????????return?false;
????????}?catch?(IOException?e)?{
????????????log.error("Build?Error"+templateFileName,e);
????????????return?false;
????????}
????????
????}
下面是修改之后的代碼
????/**
?????*?生成靜態(tài)文件
?????*?@param?templateFileName?模版名稱eg:(biz/order.ftl)
?????*?@param?propMap?用于處理模板的屬性O(shè)bject映射?
?????*?@param?htmlFilePath?要生成的靜態(tài)文件的路徑,相對設(shè)置中的根路徑,例如?"/biz/2006/5/"?
?????*?@param?htmlFileName?要生成的文件名,例如?"123.htm"?
?????*?@return
?????*/
????private?boolean?buildHtml(String?templateFileName,Map?propMap,?String?htmlFilePath,String?htmlFileName){
????????try?{
????????????Template?template?=?freemarkerCfg.getTemplate(templateFileName);
????????????template.setEncoding("UTF-8");
????????????//創(chuàng)建生成文件目錄
????????????creatDirs(buildPath.getFilename(),htmlFilePath);
????????????File?htmlFile?=?new?File(buildPath?+?htmlFilePath?+?htmlFileName);
????????????Writer?out?=?new?BufferedWriter(new?OutputStreamWriter(new?FileOutputStream(htmlFile),"UTF-8"));
????????????template.process(propMap,out);
????????????out.flush();
????????????return?true;
????????}?catch?(TemplateException?ex){
????????????log.error("Build?Error"+templateFileName,ex);
????????????return?false;
????????}?catch?(IOException?e)?{
????????????log.error("Build?Error"+templateFileName,e);
????????????return?false;
????????}
????????
????}
原因就在于OutputStreamWriter的不同構(gòu)造方法
OutputStreamWriter(OutputStream?out)
??????????創(chuàng)建使用默認(rèn)字符編碼的 OutputStreamWriter。
OutputStreamWriter(OutputStream?out, String?charsetName)
??????????創(chuàng)建使用指定字符集的 OutputStreamWriter。
?這個是中文JDK的文檔說明,剛開始我使用默認(rèn)的構(gòu)造函數(shù),所以使用了系統(tǒng)默認(rèn)的編碼,GBK,所以在生成靜態(tài)文件的時候把UTF-8內(nèi)容用GBK編碼寫入了,所以在UTF-8下瀏覽就有問題。
還有關(guān)于修改模版文件同樣也要注意這個問題。
????public?String?loadTemplate(String?templateName)?{
????????StringBuffer?sb?=?new?StringBuffer();
????????try?{
????????????File?file?=?new?File(templatePath+"/"+templateName);
????????????BufferedReader?reader?=?new?BufferedReader(new?InputStreamReader(new?FileInputStream(file),"UTF-8"));
????????????String?line?=?reader.readLine();
????????????while(line?!=?null)????{
????????????????sb.append(line);
????????????????sb.append("\r\n");
????????????????line?=?reader.readLine();
????????????}
????????????reader.close();
????????}?catch?(IOException?e)?{
????????????throw?new?SystemException("Loading?template?Error:",e);
????????}
????????return?sb.toString();
????}
????public?void?saveTemplate(String?templateName,?String?templateContent)?{
????????try?{
????????????File?file?=?new?File(templatePath?+?"/"?+?templateName);
????????????Writer?out?=?new?BufferedWriter(new?OutputStreamWriter(new?FileOutputStream(file),"UTF-8"));
????????????out.write(templateContent);
????????????out.flush();
????????????//扔出templatesave事件
????????????TemplateSaveEvent?evt?=?new?TemplateSaveEvent();
????????????evt.setTemplateName(templateName);
????????????dispatchTemplateEvent(evt);
????????}?catch?(IOException?e)?{
????????????throw?new?SystemException("Write?template?Error",e);
????????}
????}
|
posted on 2006-06-21 10:46
莫多 閱讀(2863)
評論(0) 編輯 收藏 所屬分類:
Other 、
Spring