在做web开发时,经常会遇到需要将一些动态页面静态化为html,提高访问的效率,减轻服务器压力。如百度音乐,中的歌曲,歌手,专辑,以及榜单全是静态化的页面。
下面两种方法都是通过文件流来实现的,注意转码问题。我在些就不详细解释,只接上源码
//把流输出
private static void getBuffer(String tofilename,String url) throws IOException{
InputStream l_urlStream = getHtmlSource(url);
BufferedInputStream bufferedInputStream=new BufferedInputStream(l_urlStream);
Reader isr=new InputStreamReader(bufferedInputStream,"utf-8");
PrintWriter out = null;
BufferedReader br = null;
//BufferedOutputStream bufferedOutputStream = null;
try{
br=new BufferedReader(isr);
// bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(tofilename));
String content;
out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(tofilename), "utf-8"));
while((content=br.readLine()) !=null){
out.println(content);
}
//bufferedOutputStream.flush();
}catch(Exception exception){
}finally{
try {
if(l_urlStream!=null){
l_urlStream.close();
}
if(bufferedInputStream!=null){
bufferedInputStream.close();
}
if(br!=null){
br.close();
}
if(out!=null){
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
//根据路径获得文件流
private static InputStream getHtmlSource(String getURL){
java.io.InputStream l_urlStream=null;
try{
java.net.URL l_url = new java.net.URL(getURL);
java.net.HttpURLConnection l_connection = (java.net.HttpURLConnection) l_url.openConnection();
l_connection.setRequestProperty("User-Agent","Mozilla/6.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
int state=l_connection.getResponseCode();
if(state==200){
l_connection.connect();
l_urlStream = l_connection.getInputStream();
}
}catch(Exception ex){
ex.printStackTrace();
}
return l_urlStream;
}
/**
* 生成(分类主页面)静态页面
* 方法名: goIndexStaticMain
* 方法描述: TODO
* 传入参数:
* 返回值类型: String
* 异常对象:
* @throws Exception
*/
public String goCateContentMain() throws Exception{
String webUrl = SystemConfig.getInstance().getCateContentMainUrl();
String tofilename = this.getRequest().getRealPath("/")+"page\\pageCategory\\categoryContent\\cateContentMain.html";
File file = new File(tofilename);
if(file.exists()){
file.delete();
}
file.createNewFile();
getBuffer(tofilename,webUrl);
this.outputDateinfo("生成成功!");
return NONE;
}
或
public static void createFile1(String path,String value) throws IOException{
File f = new File(path);
if(f.exists()){
f.delete();
}
FileOutputStream os = new FileOutputStream(f);
OutputStreamWriter out = new OutputStreamWriter(os,"utf-8");
out.write(value);
out.close();
os.close();
}
public static String getHtmlSource1(String getURL){
String html="";
try{
String sCurrentLine;
StringBuffer sTotalString = new StringBuffer("");
sCurrentLine="";
java.io.InputStream l_urlStream;
java.net.URL l_url = new java.net.URL(getURL);
java.net.HttpURLConnection l_connection = (java.net.HttpURLConnection) l_url.openConnection();
int state=l_connection.getResponseCode();
if(state==200){
l_connection.connect();
l_urlStream = l_connection.getInputStream();
java.io.BufferedReader l_reader = new java.io.BufferedReader(new java.io.InputStreamReader(l_urlStream,"utf-8"));
//得到返回的信息
while ((sCurrentLine = l_reader.readLine()) != null) {
sTotalString.append(sCurrentLine);
sTotalString.append("\n");
}
l_reader.close();
html = sTotalString.toString();
}else{
html=null;
}
l_connection.disconnect();
}catch(Exception ex){
ex.printStackTrace();
}
return html;
}