2006-08-30
只有少量页面需要生成静态页面时,可以试试这个类
package
com.schedule;
import
java.io.BufferedReader;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
java.io.InputStreamReader;
import
java.io.OutputStreamWriter;
import
java.net.HttpURLConnection;
import
java.net.MalformedURLException;
import
java.net.URL;
import
java.util.HashMap;
import
java.util.Iterator;
import
java.util.Map;

public
class
StaticPageService
{
private
Map
<
String, String
>
urlMap;
private
String realPath;

public
void
createFile()
{
if
(urlMap
==
null
)
{
throw
new
NullPointerException(
"
The url map is should not be null!
"
);
}
Iterator
<
String
>
keys
=
urlMap.keySet().iterator();
String inputLine;
String enter
=
"
\n
"
;
StringBuffer buffer
=
new
StringBuffer();
try
{
while
(keys.hasNext())
{
String key
=
keys.next();
URL url
=
new
URL(key);
HttpURLConnection uc
=
(HttpURLConnection) url.openConnection();
BufferedReader in
=
new
BufferedReader(
new
InputStreamReader(uc.getInputStream()));
if
(buffer.length()
>
0
)buffer.delete(
0
,buffer.length());
while
((inputLine
=
in.readLine())
!=
null
)
{
buffer.append(inputLine);
buffer.append(enter);
}
String file
=
realPath
+
urlMap.get(key);
OutputStreamWriter fw
=
new
OutputStreamWriter(
new
FileOutputStream(file),
"
GBK
"
);
fw.write(buffer.toString());
in.close();
fw.close();
}
}
catch
(MalformedURLException e)
{
e.printStackTrace();
}
catch
(IOException e)
{
e.printStackTrace();
}
}
/** */
/**
*
@param
urlMap
* the urlMap to set
*/
public
void
setUrlMap(Map
<
String, String
>
urlMap)
{
this
.urlMap
=
urlMap;
}
/** */
/**
*
@param
realPath
* the realPath to set
*/
public
void
setRealPath(String realPath)
{
this
.realPath
=
realPath;
}
public
static
void
main(String[] args)
{
StaticPageService service
=
new
StaticPageService();
Map
<
String, String
>
urlMap
=
new
HashMap
<
String, String
>
();
//
map的key为要生成静态页面的url,value为生成后的静态文件保存的路径
urlMap.put(
"
http://localhost:8080/index.jsp
"
,
"
/temp/index1.html
"
);
urlMap.put(
"
http://www.yahoo.com.cn/
"
,
"
/temp/index2.html
"
);
//
此参数用来设置当前Web应用的真实路径
service.setRealPath(
"
d:
"
);
service.setUrlMap(urlMap);
service.createFile();
}
}
发表评论
- 浏览: 720 次
- 性别:

- 来自: 北京

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
用Grinder进行性能测试 ...
比之LoadRunner如和?
-- by nickcen -
用Grinder进行性能测试 ...
我出来的文件总是空的 代理我配置好了 无论我在浏览器做什么 脚本文件都是空的 ...
-- by eminem -
用Grinder进行性能测试 ...
配置好代理后,你在浏览器作的操作会被自动记录成脚本。
-- by zeroblue -
用Grinder进行性能测试 ...
我怎么没有成功呢?可以详细说明 怎么记录脚本吗?
-- by sandybuster -
人口、猪口与官口
这文章难得啊! 当想到我每月拿出工资的20%税收去做官场买卖和养官家的二奶三奶的 ...
-- by bruce.lu






评论排行榜