日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

FreemarkerUtil工具類

張軍 9974 0



張軍博客


張軍博客

  • 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>&nbsp;&nbsp;&nbsp;&nbsp;<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>&nbsp;&nbsp;&nbsp;&nbsp;<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;
	}
}




更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 石林| 张家界市| 乐东| 广德县| 油尖旺区| 福清市| 铜山县| 抚远县| 类乌齐县| 繁昌县| 西畴县| 土默特左旗| 翁牛特旗| 德钦县| 浠水县| 喜德县| 西青区| 辽宁省| 菏泽市| 巴中市| 泸水县| 商南县| 富平县| 芷江| 余江县| 松潘县| 德钦县| 正定县| 天长市| 齐河县| 广安市| 金阳县| 民丰县| 资中县| 防城港市| 确山县| 清苑县| 临汾市| 鄂托克前旗| 双鸭山市| 乌苏市|