package com.lch.util;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Locale;
import java.util.Map;
import javax.servlet.ServletContext;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class FreeMarkerFactory {
private static FreeMarkerFactory fmf=new FreeMarkerFactory();
private FreeMarkerFactory(){}
public static FreeMarkerFactory getIntersten(){
return fmf;
}
public void init(String ftl,String htmlName,Map map,String fileName,ServletContext sc)
throws IOException, TemplateException{
Configuration cf=new Configuration();
cf.setServletContextForTemplateLoading(sc, "/"+fileName);
cf.setEncoding(Locale.getDefault(), "utf-8");
//cf.setDefaultEncoding("utf-8");
Template template;
template = cf.getTemplate(ftl);
template.setEncoding("utf-8");
String path=sc.getRealPath("/");
System.out.println(path);//涔熷氨鏄潤鎬侀〉闈㈢殑鍦板潃D:\apache-tomcat-6.0.16\webapps\test\
BufferedWriter buff= new BufferedWriter(new FileWriter(path+htmlName));
File htmlFile = new File(path+htmlName);
Writer out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(htmlFile),"utf-8"));
template.process(map,out);
buff.close();
out.flush();
out.close();
}/*
private static FreeMarkerFactory fmf=new FreeMarkerFactory();
private FreeMarkerFactory(){}
public static FreeMarkerFactory getIntersten(){
return fmf;
}
private final Log logger = LogFactory.getLog(getClass());
private Configuration freemarker_cfg = null ;
protected Configuration getFreeMarkerCFG()
{
if ( null == freemarker_cfg)
{
// Initialize the FreeMarker configuration;
// - Create a configuration instance
freemarker_cfg = new Configuration();
// - FreeMarker支持多种模板装载方式,可以查看API文档,都很简单:路径,根据Servlet上下文,classpath等等
// htmlskin是放在classpath下的一个目录
freemarker_cfg.setClassForTemplateLoading( this.getClass(), "/htmlskin");
}
return freemarker_cfg;
}
public boolean geneHtmlFile(String templateFileName,Map propMap, String htmlFilePath,String htmlFileName )
{
// @todo 从配置中取得要静态文件存放的根路径:需要改为自己的属性类调用
String sRootDir = " e:/webtest/htmlfile " ;
try
{
Template t = getFreeMarkerCFG().getTemplate(templateFileName);
// 如果根路径存在,则递归创建子目录
creatDirs(sRootDir,htmlFilePath);
File afile = new File(sRootDir + " / " + htmlFilePath + " / " + htmlFileName);
Writer out = new BufferedWriter( new OutputStreamWriter( new FileOutputStream(afile)));
t.process(propMap, out);
}
catch (TemplateException e)
{
logger.error( " Error while processing FreeMarker template " + templateFileName,e);
return false ;
}
catch (IOException e)
{
logger.error( " Error while generate Static Html File " + htmlFileName,e);
return false ;
}
return true ;
}
public static boolean creatDirs(String aParentDir, String aSubDir)
{
File aFile = new File(aParentDir);
if (aFile.exists())
{
File aSubFile = new File(aParentDir + aSubDir);
if ( ! aSubFile.exists())
{
return aSubFile.mkdirs();
}
else
{
return true ;
}
}
else
{
return false ;
}
}
*/
}
评论0
最新资源