首页 > 代码库 > 通过FreeMarkerclasspath加载方式生成静态页面

通过FreeMarkerclasspath加载方式生成静态页面

package htmlskin;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class FreeMarkerTest {
	
	private Configuration cfg;// FreeMarker配置实例
	
	public static void main(String[] args) {
		FreeMarkerTest test = new FreeMarkerTest();
		
		Map<String, Object> root = new HashMap<String, Object>();
		root.put("message", "Hello FreeMarker.");
		String ftl = "/ftl/test.ftl";
		boolean flag = test.genericHtmlFile(ftl, root);
		if(flag){
			System.out.println("创建静态HTML页面成功!");
		}else{
			System.out.println("创建静态HTML页面失败!");
		}
	}
	
	/**
	 * 获取FreeMarker配置实例
	 * @return
	 */
	public Configuration getConfiguration() {
		if (null == cfg) {
			cfg = new Configuration();
			//通过classpath加载方式
			cfg.setClassForTemplateLoading(this.getClass(), "/htmlskin");
		}
		return cfg;
	}
	
	/**
	 * 生成HTML页面
	 * @param ftl FreeMarker模版文件
	 * @param root 模版数据
	 * @return true->生成静态页面成功 false->生成静态页面失败
	 */
	public boolean genericHtmlFile(String ftl, Map<String, Object> root) {
		try {
			Template template = getConfiguration().getTemplate(ftl);
			String projectPath = System.getProperty("user.dir");
			String htmlPath = projectPath + File.separator + "html";
			String path = createDirs(htmlPath, genericFilePath());
			String name = genericFileName();
			// System.out.println(path+name);
			
			File file = new File(path + name);
			Writer out = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(file)));
			template.process(root, out);
		} catch (FileNotFoundException e) {
			return false;
		} catch (IOException e) {
			return false;
		} catch (TemplateException e) {
			return false;
		}
		return true;
	}
	
	/**
	 * 生成存储文件子路径
	 * 格式:\yyyy\MM\dd	 * @return 文件子路径
	 */
	public String genericFilePath() {
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);// 年
		int month = calendar.get(Calendar.MONTH);// 月
		int day = calendar.get(Calendar.DAY_OF_MONTH);// 日
		
		String year_str = "";
		String month_str = "";
		String day_str = "";

		year_str = String.valueOf(year);
		month_str = month < 10 ? "0" + month : month + "";
		day_str = day < 10 ? "0" + day : day + "";
		
		// 拼接子路径
		StringBuffer sb = new StringBuffer();
		sb.append(File.separator);
		sb.append(year_str);
		sb.append(File.separator);
		sb.append(month_str);
		sb.append(File.separator);
		sb.append(day_str);
		sb.append(File.separator);
		return sb.toString();
	}
	
	/**
	 * 生成文件名称
	 * 格式:yyyyMMddHHmmss4位随机数
	 * @return 文件名称
	 */
	public String genericFileName() {
		//创建日历类
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);//年
		int month = calendar.get(Calendar.MONTH);//月
		int day = calendar.get(Calendar.DAY_OF_MONTH);//日
		int hour = calendar.get(Calendar.HOUR_OF_DAY);//时
		int minus = calendar.get(Calendar.MINUTE);//分
		int second = calendar.get(Calendar.SECOND);//秒
		//创建生成随机数类
		Random random = new Random();

		String year_str = year + "";
		String month_str = month < 10 ? "0" + month : month + "";
		String day_str = day < 10 ? "0" + day : day + "";
		String hour_str = hour < 10 ? "0" + hour : hour + "";
		String minus_str = minus < 10 ? "0" + minus : minus + "";
		String second_str = second < 10 ? "0" + second : second + "";
		// System.out.println("year=" + year + ",month=" + month + ",day=" + day
		// + ",hour=" + hour + ",minus=" + minus + ",second=" + second);
		
		StringBuffer sb = new StringBuffer();
		sb.append(year_str);
		sb.append(month_str);
		sb.append(day_str);
		sb.append(hour_str);
		sb.append(minus_str);
		sb.append(second_str);
		sb.append(random.nextInt(8999) + 1000);
		
		return sb.toString() + ".html";
	}
	
	/**
	 * 创建目录
	 * @param parentDir 父目录
	 * @param subDir 子目录
	 * @return 存储文件全目录
	 */
	public String createDirs(String parentDir, String subDir) {
		//System.out.println("父目录:" + parentDir);
		//System.out.println("子目录:" + parentDir + subDir);
		String path = parentDir + subDir;
		
		File parentFile = new File(parentDir);
		if (parentFile.exists()) {
			File subFile = new File(path);
			if (!subFile.exists()) {
				subFile.mkdirs();
			}
		}
		return path;
	}
}