📄 makefile.java
字号:
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();
// 项目真实路径
String realPath = templateParam.getRealPath();
// 新闻模板路径
String templatePath = realPath + Constant.NEWSTEMPLATEPATH;
String cDateStr = "news/" + PureUtil.getDateFormatStr("yyyyMMdd");
String filePostfix = ".html";
// 静态新闻生成文件存放路径
String path = realPath + cDateStr;
String fileTimeName = PureUtil.getDateFormatStr("yyyyMMddhhmmss");
// 写到数据库地路径
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();
// 项目真实路径
String realPath = templateParam.getRealPath();
// 新闻模板路径
String templatePath = realPath + Constant.NEWSTEMPLATEPATH;
String fileName = news.getFilename();
// 原来的路径,当删除原来的目录时,重建目录
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();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -