实现原理:从数据库中查询出所有的文章信息,将文章信息转成XML文档,再通过XSLT文档对其转换成HTML文档后进行下载。
1、将文章信息转成XML文档
private static Document articlesToXML(List<Article> articles, String title) {
Document doc = DocumentHelper.createDocument();
Element root = doc.addElement("articles").addAttribute("title", title);
for (Article article : articles) {
Element articleEle = root.addElement("article");
articleEle.addElement("title").setText(article.getTitle());
articleEle.addElement("content").setText(article.getContent());
}
return doc;
}
转换后的XML类似于如下
<?xml version="1.0" encoding="UTF-8"?> <articles title="我的博客"> <article> <title>文章标题</title> <content>文章内容</content> </article> </articles>
2、将XML文档转成HTML
public static byte[] articlesToHTML(List<Article> articles, String title) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
TransformerFactory factory = TransformerFactory.newInstance();
InputStream in = ArticleUtils.class.getClassLoader().getResourceAsStream("article.xsl");
Source xlst = new StreamSource(in);
Transformer transformer = factory.newTransformer(xlst);
Source xmlSource = new DocumentSource(articlesToXML(articles, title));
Result outputTarget = new StreamResult(out);
transformer.transform(xmlSource, outputTarget);
in.close();
} catch (Exception e) {
throw new RuntimeException("文章集合转HTML数据失败", e);
}
return out.toByteArray();
}
XSLT文档
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>
<xsl:value-of select="/articles[@title]/@title" />
</title>
<style type="text/css">
body{
background-color: #F5F5F5;
color: #797979;
}
h2{
color: #2B9B70;
margin: 15px;
}
.article{
margin: 15px;
background-color: white;
}
.article_title {
color: #2B9B70;
font-size: 20px;
border-bottom: 1px dashed #2B9B70;
padding: 10px;
}
.article_content {
padding: 8px 15px;
line-height: 1.8;
}
</style>
</head>
<body>
<h2><xsl:value-of select="/articles[@title]/@title" /></h2>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
<xsl:template match="article">
<div class="article">
<div class="article_title">
<xsl:value-of select="title" disable-output-escaping="yes" />
</div>
<div class="article_content">
<xsl:value-of select="content" disable-output-escaping="yes" />
</div>
</div>
</xsl:template>
</xsl:stylesheet>
3、实现下载 -- Struts2
public class ExportAction extends BaseAction {
@Resource(name = "articleService")
private ArticleService articleService;
// 文件名
public String getFileName() throws IOException {
String blogTitle = (String) application.get("blog_title");
String fileName = blogTitle + "-" + DateUtils.getDate(new Date(), "yyyyMMddHHmmss") + ".html";
// 解决火狐和IE文件名编码差异
HttpServletRequest request = ServletActionContext.getRequest();
String agent = request.getHeader("user-agent");
if (agent.contains("Firefox")) {
fileName = "=?UTF-8?B?" + new BASE64Encoder().encode(fileName.getBytes("UTF-8")) + "?=";
} else {
fileName = URLEncoder.encode(fileName, "UTF-8");
}
return fileName;
}
// 下载流
public InputStream getTargetFile() {
String blogTitle = (String) application.get("blog_title");
byte[] bytes = ArticleUtils.articlesToHTML(articleService.findAllArticles(), blogTitle);
return new ByteArrayInputStream(bytes);
}
}
Struts2配置文件
<action name="export" class="exportAction">
<result type="stream">
<param name="inputName">targetFile</param>
<param name="contentType">text/html</param>
<param name="contentDisposition">attachment;filename=${fileName}</param>
</result>
</action>原文:http://my.oschina.net/harmel/blog/492534