六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 51|回复: 0

首页静态页面生成

[复制链接]

升级  62%

7

主题

7

主题

7

主题

童生

Rank: 1

积分
31
 楼主| 发表于 2013-2-7 03:40:51 | 显示全部楼层 |阅读模式
public static void generatorHtml() {
String html = "";
String filePath = ServletActionContext.getServletContext().getRealPath(
"/index.html");
File file = new File(filePath);
if (file.exists()) {
file.delete();
}
try {
HttpServletRequest request = ServletActionContext.getRequest();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort();
URL url = new URL(basePath + "/index_doQuery.do");
URLConnection uc = (HttpURLConnection) url.openConnection();
uc.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");

uc.setConnectTimeout(3000);
InputStream in = uc.getInputStream();

int n;
StringBuffer buffer = new StringBuffer();

BufferedReader reader = new BufferedReader(new InputStreamReader(
in, "utf-8"));
while ((n = reader.read()) != -1) {
buffer.append((char) n);
}
html = new String(buffer.toString());

file.createNewFile();
FileOutputStream o = new FileOutputStream(filePath);
o.write(html.getBytes("UTF-8"));
} catch (IOException e) {

e.printStackTrace();
}

}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表