log4j插入数据库,并生成HTML(最终版)

在师父的讲解下,搞定了。本文章只用于分享学习经验,没有其他用意。希望大家培养开源的精神。

可能有有些一露的地方,大家看下前两篇文章。


public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

UserDao dao=new DaoImp();
List<Log4j> all=new ArrayList();

int pageCount = 4;//设置页数

int pages=dao.getPages(pageCount);
String webpagestr=createWebPage(pages);



for(int i=0;i<pages;i++){

all=dao.all(pageCount*i-i, pageCount);
creatHtml(request, all, i,webpagestr);
}

request.setAttribute("list", all);

return mapping.findForward("suce");
}

private String createWebPage(int pages) {
//设定连接页
StringBuilder webpagestr = new StringBuilder();
final String pagelink="<a href=";
for(int n=0;n<pages;n++){
//拼接连接
webpagestr.append(pagelink).append("log").append(n).append(".html>")
.append(n).append("</a>")
.append(" ");
}
String sss= webpagestr.toString();
return sss;
}

protected void creatHtml(HttpServletRequest request, List<Log4j> all,int pages,String webpagestr)
throws FileNotFoundException, IOException {
//------生成HTML-------------------------------

String filePath = "";
filePath = request.getRealPath("/")+"template.html";

/******** begin ************/

final String beginForHtmlTag = "<###for###>";
final String endForHtmlTag = "</###for###>";

FileInputStream input=new FileInputStream(filePath);
int length=input.available();
byte[] b=new byte[length];
input.read(b); //读取模板

String result = new String(b);//获取内容
int beginIndex = result.indexOf(beginForHtmlTag);
int endIndex = result.indexOf(endForHtmlTag);
String begin = result.substring(0, beginIndex);
String end = result.substring(endIndex+endForHtmlTag.length());

String table=result.substring(beginIndex+beginForHtmlTag.length(), endIndex);//get table
/**
* 替换所有
*/
StringBuilder content=new StringBuilder();
for(Log4j log4j:all){
String temp = table.replace("###username###",log4j.getUsername());
temp = temp.replace("###classs###",log4j.getClasss());
temp = temp.replace("###method###",log4j.getMethod());
temp = temp.replace("###leve###",log4j.getLogLevel());
temp = temp.replace("###mess###",log4j.getMessage());
temp = temp.replace("###titme###",log4j.getCreateTime());
content.append(temp);
}
result=begin+content+"<br />current pages:"+pages+"__"+webpagestr+end;
/******** end ************/

// 根据时间得文件名
// Calendar calendar = Calendar.getInstance();
// String fileame = String.valueOf(calendar.getTimeInMillis()) +".html";

String fileame ="log"+pages+".html";
//生成html路径
fileame = request.getRealPath("/")+fileame;//生成的html文件保存路径

//写入
FileOutputStream fileoutputstream = new FileOutputStream(fileame);
byte tag_bytes[] = result.getBytes();
fileoutputstream.write(tag_bytes);
fileoutputstream.close();


//---------------------------
}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值