欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用java将动态网页生成静态网页示例

程序员文章站 2024-03-31 14:25:22
复制代码 代码如下:package com.tools;import java.io.*;import java.net.url; /** * title:动态...

复制代码 代码如下:

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);
}
}