?? makefile.java.svn-base
字號:
package com.pure.freemarker;
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 com.pure.domain.News;
import com.pure.sys.Constant;
import com.pure.util.PureUtil;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class MakeFile {
/**
* 生成新的html文件
*
* @param news
* @param templateParam
* @return
*/
public String make(News news, TemplateParam templateParam) {
Configuration cfg = new Configuration();
// 項(xiàng)目真實(shí)路徑
String realPath = templateParam.getRealPath();
// 新聞模板路徑
String templatePath = realPath + Constant.NEWSTEMPLATEPATH;
String cDateStr = "news/" + PureUtil.getDateFormatStr("yyyyMMdd");
String filePostfix = ".html";
// 靜態(tài)新聞生成文件存放路徑
String path = realPath + cDateStr;
String fileTimeName = PureUtil.getDateFormatStr("yyyyMMddhhmmss");
// 寫到數(shù)據(jù)庫地路徑
String returnFileName = cDateStr + "/" + fileTimeName + filePostfix;
String fileName = "";
File newsDir = new File(path);
fileName = path + "/" + fileTimeName + filePostfix;
if (!newsDir.exists()) {
newsDir.mkdirs();
fileName = path + "/" + fileTimeName + filePostfix;
}
try {
cfg.setDirectoryForTemplateLoading(new File(templatePath));
cfg.setObjectWrapper(new DefaultObjectWrapper());
Template newsTemplate = cfg.getTemplate(templateParam.getTemplateName());
newsTemplate.setEncoding("GBK");
Map root = new HashMap();
root.put("news", news);
Writer out = new OutputStreamWriter(new FileOutputStream(fileName));
try {
newsTemplate.process(root, out);
} catch (TemplateException e) {
e.printStackTrace();
}
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
return returnFileName;
}
/**
* 重新生成html文件,使用原來的文件名
*
* @param news
* @param templateParam
*/
public void update(News news, TemplateParam templateParam) {
Configuration cfg = new Configuration();
// 項(xiàng)目真實(shí)路徑
String realPath = templateParam.getRealPath();
// 新聞模板路徑
String templatePath = realPath + Constant.NEWSTEMPLATEPATH;
String fileName = news.getFilename();
// 原來的路徑,當(dāng)刪除原來的目錄時(shí),重建目錄
String path = realPath+ fileName.substring(0, fileName.lastIndexOf("/"));
fileName = realPath + news.getFilename();
File oldDir = new File(path);
if (!oldDir.exists()) {
oldDir.mkdirs();
}
try {
cfg.setDirectoryForTemplateLoading(new File(templatePath));
cfg.setObjectWrapper(new DefaultObjectWrapper());
Template newsTemplate = cfg.getTemplate(templateParam.getTemplateName());
newsTemplate.setEncoding("GBK");
Map root = new HashMap();
root.put("news", news);
Writer out = new OutputStreamWriter(new FileOutputStream(fileName));
try {
newsTemplate.process(root, out);
} catch (TemplateException e) {
e.printStackTrace();
}
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -