import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class FreeMarkerUtil {
private static Configuration config = null;
/** 初始化属性文件 */
public static ResourceBundle properties = GetProperties.getPropertiesValue("file");
/**
* 如果目录不存在,则自动创建
*
* @param path
* @return boolean 是否成功
*/
private static boolean creatDirs(String path) {
File aFile = new File(path);
if (!aFile.exists()) {
return aFile.mkdirs();
} else {
return true;
}
}
public static void main(String[] args) {
Map<String, Object> contextMap = new HashMap<String, Object>();
contextMap.put("title", "沪指跌0.63%失守3100点");
contextMap.put("sourse", "*****");
contextMap.put("times", "09:26 14:20");
geneHtmlFile(contextMap, "article4.html", properties.getString("articlePath"), "147.html");
}
/**
* 模板生成静态html的方法
*
* @author zhy
* @param templateFileName (模板文件名)
* @param templateFilePath (指定模板目录)
* @param contextMap (用于处理模板的属性Object映射)
* @param htmlFilePath (指定生成静态html的目录)
* @param htmlFileName (生成的静态文件名)
*/
public static void geneHtmlFile(Map<String, Object> contextMap, String htmlFileName,
String modulePath, String moduleName) {
try {
Template t = getFreeMarkerCFG(modulePath).getTemplate(moduleName);
// 如果根路径存在,则递归创建子目录
creatDirs(modulePath);
File afile = new File(modulePath + "/" + htmlFileName);
FileOutputStream fileInput = new FileOutputStream(afile);
OutputStreamWriter outWrite = new OutputStreamWriter(fileInput, "utf-8");
Writer out = new BufferedWriter(outWrite);
t.process(contextMap, out);
out.flush();
out.close();
} catch (TemplateException e) {
System.out.print(e.getMessage());
} catch (IOException e) {
System.out.print(e.getMessage());
} catch (Exception e) {
System.out.print(e.getMessage());
}
}
/**
* 获取freemarker的配置,freemarker本身支持classpath,目录或从ServletContext获取.
*
* @author zhy
* @param templateFilePath 获取模板路径
* @return Configuration 返回freemaker的配置属性
* @throws Exception
*/
private static Configuration getFreeMarkerCFG(String templateFilePath) throws Exception {
if (null == config) {
config = new Configuration(Configuration.getVersion());
try {
config.setDirectoryForTemplateLoading(new File(templateFilePath));
config.setObjectWrapper(new DefaultObjectWrapper(Configuration.getVersion()));
config.setDefaultEncoding("UTF-8");
} catch (Exception ex) {
throw ex;
}
}
return config;
}
}
获取属性文件
import java.util.ResourceBundle;
public class GetProperties {
/**
* 根据属性文件的位置获取属性文件
*
* @author zhy
* @param propertiesPosition 属性文件的位置
* @return
*/
public static ResourceBundle getPropertiesValue(String propertiesPosition) {
return ResourceBundle.getBundle(propertiesPosition);
}
/**
* 根据属性文件的位置获取属性文件中的值
*
* @author zhy
* @param propertiesPosition 属性文件的位置
* @param propertieName 要读取的属性文件中的属性
* @return
*/
public static Object getPropertiesValue(String propertiesPosition, String propertieName) {
return ResourceBundle.getBundle(propertiesPosition).getObject(propertieName);
}
}