/**
* @title FreeMarkerUtl
* @description 模板文件工具类
* @author maohuidong
* @date 2017-07-05
*/
public class FreeMarkerUtl {

/**
* @function genHtmlPage
* @param templatePath:模板路径 tempalteFile:模板文件名称 root:模板文件需要渲染的数据 genFilePath:生成的文件路径和名称
* @description 用户提交问卷
* @return (无)
* @throws Exception
* @author maohuidong
* @date 2017-07-05
*/
public static Boolean genHtmlPage(String templatePath,String tempalteFile,Map<Object, Object> root,String genFilePath){
try {
Configuration configuration = new Configuration();
//模板文件的路径
configuration.setDirectoryForTemplateLoading(new File(templatePath));
//字符集
configuration.setDefaultEncoding("utf-8");

//获取模板
Template template = configuration.getTemplate(tempalteFile, "utf-8");

//创建writer对象
File file = new File(genFilePath);
Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),"utf-8"));
template.process(root, writer);
writer.flush();
writer.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return false;
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
} catch (TemplateException e) {
e.printStackTrace();
return false;
}
return true;
}
}

posted on 2017-11-17 20:21  毛会懂  阅读(152)  评论(0编辑  收藏  举报