最近有遇到一个档案系统的对接:要求审批意见需要以HTML的格式上传
我们的思路是需要一个JSP模板页面,然后对数据进行替换并转换:
<html>
<head>
<title>###title###</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<table width="1000" border="1" align="center">
<tr>
<td>###titleName###</td>
<td>###title###</td>
</tr>
<tr>
<td>###adviceName###</td>
<td>###advice###</td>
</tr>
</table>
</body>
</html>
我们来看下代码:
/**
* 根据本地模板生成静态页面
* @param JspFile jsp路经
* @param HtmlFile html路经
* @return
*/
public boolean JspToHtmlFile(String JspFile, String HtmlFile,String title,String advice) {
File file=new File(HtmlFile);
//如果文件夹不存在
if(!file.exists()){
writeLog("=======开始创建文件夹============");
//创建文件夹
file.mkdirs();
writeLog("=======创建文件夹结束============");
}
String str = "";
try {
String tempStr = "";
InputStreamReader isr = new InputStreamReader(new FileInputStream(JspFile), "utf-8");
BufferedReader br = new BufferedReader(isr);
while ((tempStr = br.readLine()) != null){
str = str + tempStr +" ";
}
System.out.println(str);
} catch (IOException e) {
e.printStackTrace();
return false;
}
try {
str = str.replaceAll("###titleName###","标题");
str = str.replaceAll("###adviceName###","审批意见");
str = str.replaceAll("###title###",title);
str = str.replaceAll("###advice###", advice);
//必须设置编码格式不然会出现乱码(编码都需要与页面的一致)
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(HtmlFile+"/"+title+".html"),"utf-8"));
bufferedWriter.write(str);
bufferedWriter.newLine();//换行
/* * 刷新该流的缓冲。
* 关键的一行代码。如果没有加这行代码。数据只是保存在缓冲区中。没有写进文件。
* 加了这行才能将数据写入目的地。 * */
bufferedWriter.flush();
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
效果如下:
参考:http://t.zoukankan.com/lanliying-p-4496895.html