FreemarkerUtil
package zj.freemarker.util; import java.io.BufferedOutputStream; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.StringWriter; import java.util.Map; import zj.check.util.CheckUtil; import zj.common.exception.ServiceException; import zj.freemarker.bean.Freemarker; import zj.io.util.FileUtil; import freemarker.cache.ClassTemplateLoader; import freemarker.cache.FileTemplateLoader; import freemarker.cache.MultiTemplateLoader; import freemarker.cache.TemplateLoader; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; /** * FreemarkerUtil工具類 * * @version 1.00 (2014.09.15) * @author SHNKCS 張軍 {@link <a href=http://www.521shanshan.com>張軍個人網站</a> <a href=http://user.qzone.qq.com/360901061/>張軍QQ空間</a>} */ public class FreemarkerUtil { // private transient static final Logger logger = Logger.getLogger(TestXml.class); // /** // * 取得freemarker模板 // * // * @param name // * 模板名 // * @param path // * 模板路徑 // * @return 模板對象 // * @throws IOException // */ // public static final Template getTemplate(String name) throws IOException { // // 通過Freemarker的Configuration讀取相應的ftl // Configuration cfg = new Configuration(Configuration.getVersion()); // // // 設定去哪里去讀取相應的ftl模板文件 // cfg.setClassForTemplateLoading(FreemarkerUtil.class, "/ftl/mybatis"); // // cfg.setServletContextForTemplateLoading(servletContext, path); // // cfg.setDirectoryForTemplateLoading(new File("E:/versionManager/sources/java/zj-model/freemarker/freemarker-helloworld/src/main/resources/ftl")); // // 在模板文件目錄中找到名稱為name的文件 // Template temp = cfg.getTemplate(name); // return temp; // } /** * 取得freemarker模板 * * @param name * 模板名 * @param path * 模板路徑 * @author 張軍 * @date 2015-11-03 21:59:00 * @modifiyNote * @version 1.0 * @return 模板對象 * @throws IOException */ public static final Template getTemplates(Freemarker freemarker) throws IOException { // 通過Freemarker的Configuration讀取相應的ftl Configuration cfg = new Configuration(Configuration.getVersion()); // // 設定去哪里去讀取相應的ftl模板文件 // cfg.setClassForTemplateLoading(FreemarkerUtil.class, "/ftl/mybatis"); // cfg.setServletContextForTemplateLoading(servletContext, path); // cfg.setDirectoryForTemplateLoading(new File("E:/versionManager/sources/java/zj-model/freemarker/freemarker-helloworld/src/main/resources/ftl")); if (freemarker.getFtlPaths() == null) { freemarker.setFtlPaths(new String[] { "/ftl" }); } TemplateLoader[] loaders = null; if (Freemarker.LOADING_PATH_FILE.equals(freemarker.getLoadingPath())) { // 從文件中加載 loaders = new FileTemplateLoader[freemarker.getFtlPaths().length]; for (int i = 0; i < freemarker.getFtlPaths().length; i++) { loaders[i] = new FileTemplateLoader(new File(freemarker.getFtlPaths()[i])); } } else { loaders = new ClassTemplateLoader[freemarker.getFtlPaths().length]; for (int i = 0; i < freemarker.getFtlPaths().length; i++) { loaders[i] = new ClassTemplateLoader(FreemarkerUtil.class, freemarker.getFtlPaths()[i]); } } MultiTemplateLoader mtl = new MultiTemplateLoader(loaders); cfg.setTemplateLoader(mtl); // 在模板文件目錄中找到名稱為name的文件 Template temp = cfg.getTemplate(freemarker.getName(), freemarker.getCharsetName()); return temp; } /** * 寫入文件內容 * * @param freemarker * 模板對象值 * @author 張軍 * @date 2015-11-03 21:59:00 * @modifiyNote * @version 1.0 * @throws IOException * @throws TemplateException */ public static final void writeFile(Freemarker freemarker) { BufferedWriter bw = null; OutputStreamWriter osw = null; BufferedOutputStream bos = null; FileOutputStream fos = null; try { Map<String, Object> rootMap = freemarker.getRootMap(); File outFile = freemarker.getOutFile(); String charsetName = freemarker.getCharsetName(); boolean append = freemarker.isAppend(); FileUtil.createFolderOrFile(outFile, false); fos = new FileOutputStream(outFile, append); bos = new BufferedOutputStream(fos); if (CheckUtil.isNull(charsetName)) { charsetName = "UTF-8"; } osw = new OutputStreamWriter(bos, charsetName); bw = new BufferedWriter(osw); Template temp = getTemplates(freemarker); temp.process(rootMap, bw); bw.flush(); } catch (Exception e) { throw new ServiceException(e); } finally { if (fos != null) { try { fos.close(); fos = null; } catch (Exception e) { } } if (bos != null) { try { bos.close(); bos = null; } catch (Exception e) { } } if (osw != null) { try { osw.close(); osw = null; } catch (Exception e) { } } if (bw != null) { try { bw.close(); bw = null; } catch (Exception e) { } } // Runtime.getRuntime().gc(); // System.gc(); } } /** * 打印控制臺 * * @param freemarker * 模板對象值 * @author 張軍 * @date 2015-11-03 21:59:00 * @modifiyNote * @version 1.0 * @throws IOException * @throws TemplateException */ public static final String getContent(Freemarker freemarker) throws IOException, TemplateException { StringWriter sw = new StringWriter(); Template temp = getTemplates(freemarker); // 通過模板文件輸出到相應的流中 temp.process(freemarker.getRootMap(), sw); sw.flush(); String content = sw.toString(); sw.close(); return content; } /** * 打印控制臺 * * @param freemarker * 模板對象值 * @author 張軍 * @date 2015-11-03 21:59:00 * @modifiyNote * @version 1.0 * @throws IOException * @throws TemplateException */ public static final void printConsole(Freemarker freemarker) throws IOException, TemplateException { Template temp = getTemplates(freemarker); // 通過模板文件輸出到相應的流中 temp.process(freemarker.getRootMap(), new PrintWriter(System.out)); } }
Freemarker
package zj.freemarker.bean; import java.io.File; import java.util.Map; import lombok.Data; import zj.check.util.CheckUtil; /** * FreemarkerUtil工具類實體參數 * * @version 1.00 (2014.09.15) * @author SHNKCS 張軍 {@link <a href=http://www.521shanshan.com>張軍個人網站</a> <a href=http://user.qzone.qq.com/360901061/>張軍QQ空間</a>} */ @Data public class Freemarker { /** * 從class中加載 */ public final static String LOADING_PATH_CLASS = "class"; /** * 從file中加載 */ public final static String LOADING_PATH_FILE = "file"; /** * 從servlet中加載 */ public final static String LOADING_PATH_SERVLET = "servlet"; /** * 載入路徑方式 */ private String loadingPath; /** * 模板名 */ private String name; /** * 多個模板路徑 */ private String[] ftlPaths; /** * 頂層變量數據 */ private Map<String, Object> rootMap; /** * 輸出文件 */ private File outFile; /** * 編碼,默認UTF-8 */ private String charsetName = "UTF-8"; /** * 是否追加 */ private boolean append; /** * @Description * @author 張軍 * @date 2017年1月9日 下午5:09:02 * @version V1.0 * @return the loadingPath */ public String getLoadingPath() { return CheckUtil.isNull(loadingPath) ? LOADING_PATH_CLASS : loadingPath; } public void setFtlPaths(String... ftlPaths) { this.ftlPaths = ftlPaths; } }
本文為張軍原創文章,轉載無需和我聯系,但請注明來自張軍的軍軍小站,個人博客http://www.eyofj.com
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
