java 生成html_Java生成静态HTML文件

importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.io.OutputStreamWriter;importjava.net.HttpURLConnection;importjava.net.URL;public classMakeHtml {public static booleanJspToHtmlFile(String filePath, String saveHtmlFile) {

String str= "";

StringBuffer buffer= newStringBuffer();/*try {

String tempStr = "";

InputStreamReader isr = new InputStreamReader(new FileInputStream(filePath), "UTF-8");

BufferedReader br = new BufferedReader(isr);

while ((tempStr = br.readLine()) != null){

str = str + tempStr +"\n";

}

} catch (IOException e) {

e.printStackTrace();

return false;

}*/

try{

URL url= newURL(filePath);

HttpURLConnection httpUrl=(HttpURLConnection)url.openConnection();

InputStream is=httpUrl.getInputStream();

BufferedReader br= new BufferedReader(new InputStreamReader(is,"utf-8"));

StringBuilder sb= newStringBuilder();

String line;while ((line = br.readLine()) != null) {

buffer.append(line+"\n");

}

is.close();

br.close();

}catch(Exception e) {

}try{

str= buffer.toString().replaceAll("###imageUrl###","http://img.zcool.cn/community/0142135541fe180000019ae9b8cf86.jpg@1280w_1l_2o_100sh.png");//必须设置编码格式不然会出现乱码

BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(saveHtmlFile),"UTF-8"));

bufferedWriter.write(str);

bufferedWriter.newLine();//换行

bufferedWriter.flush();

bufferedWriter.close();

}catch(Exception e) {

e.printStackTrace();return false;

}return true;

}public static voidmain(String[] args) {long begin =System.currentTimeMillis();

String url= "http://xxx/banner/modelHtml.html";//模板文件地址

String savepath = "d:/" + begin + ".html";//生成文件地址

JspToHtmlFile(url, savepath);

System.out.println("用时:" + (System.currentTimeMillis() - begin) + "ms");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值