使用java将动态网页生成静态网页示例
作者:
package com.tools;
import java.io.*;
import java.net.URL;
/**
* Title:动态页面静态化
*/
public class GoToHtml {
/**
*
* @param page
* 存放静态页面的本地文件路径(c,d,e,f,g)
* @param url_addr
* 所要生成的静态页的URL地址(http://)
* @return
*/
public boolean PrintPage(String page, String url_addr) {
System.out.println("page" + page);
String newPage = "";
// 判断输入的本地路径是否是以。jsp结尾的
if (page.endsWith(".html")) {
System.out.println("this is end with xxx.html");
int bias = page.lastIndexOf("/");// bias(斜线的意思)获取最后一个斜线的位置
System.out.println("the last / at :" + bias);
newPage = page.substring(0, bias);
System.out.println("newPage:" + newPage);
}
// 如果文件夹不存在就创建一个
File ff = new File(newPage);
ff.mkdirs();
URL url;
String rLine = null;
PrintWriter fileOut = null;
InputStream ins = null;
try {
url = new URL(url_addr);
System.out.println(url+".......");
ins = url.openStream();
BufferedReader bReader = new BufferedReader(new InputStreamReader(
ins, "utf-8"));// 获取编码为gb2312的文件
FileOutputStream out = new FileOutputStream(page);
OutputStreamWriter writer = new OutputStreamWriter(out, "utf-8");
fileOut = new PrintWriter(writer);
// 循环取取数据,并写入目标文件中
while ((rLine = bReader.readLine()) != null) {
String tmp_rLine = rLine;
System.out.println(tmp_rLine);
int str_len = tmp_rLine.length();
if (str_len > 0) {
fileOut.println(tmp_rLine);
fileOut.flush();
}
tmp_rLine = null;
}
url = null;
return true;
} catch (IOException e) {
System.out.println("error: " + e.getMessage());
e.printStackTrace();
return false;
} catch (Exception es) {
System.out.println(es.getMessage());
return false;
} finally {// 关闭资源
fileOut.close();
try {
ins.close();
} catch (IOException ex) {
// 关闭输入流出错
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
GoToHtml gth = new GoToHtml();
String accresstoken="W67K0vH4k5YpH3DpK75JkmEchvW_utYBTqjd-vb8VXRFD3AovHhK_ApJQ7JMouhIytsRU3_VOJ2iXrCcLUAb_6-HblSmXzz_dwqTuP3908aOIoukKRNxLLaGh0aC7rnvsazyByXwi8670us-RYD1vA";
String media="Y9kK2wmpS4byzxIyFz7_NQ6gyi8Ev7-hyZG5HbaDKOqOBLfs9JIt8_x2S4zoVc1g";
String urlString="http://file.api.weixin.qq.com/cgi-bin/media/get?access_token="+accresstoken+"&media_id="+media;
//gth.PrintPage("D:/eclipse/newNum.html","http://localhost/prime/user/userFind_update.jsp?id=93");
gth.PrintPage("D:/newNum.html",urlString);
}
}