09-01
06
Java生成静态页面的方法
作者:Java伴侣 日期:2009-01-06
package com.klstudio.util;
import java.io.*;
import java.net.*;
public class Tools {
final static Object lock = new Object();
public static void makeHtml(String page, String filePath){
makeHtml(page,filePath,"UTF-8");
}
public static void makeHtml(String page, String filePath,String chartset) {
synchronized (lock) {
HttpURLConnection huc = null;
BufferedReader br = null;
BufferedWriter bw = null;
try {
huc = (HttpURLConnection)new URL(page).openConnection();
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
System.setProperty("sun.net.client.defaultReadTimeout", "30000");
huc.connect();
InputStream stream = huc.getInputStream();
bw = new BufferedWriter(new OutputStreamWriter (new FileOutputStream(filePath),chartset));
br = new BufferedReader(new InputStreamReader(stream, chartset));
String line;
while((line = br.readLine())!= null){
if(line.trim().length() > 0){
bw.write(line);
bw.newLine();
}
}
}catch (Exception e) {
e.printStackTrace();
}finally {
try {
br.close();
bw.close();
huc.disconnect();
}catch (Exception e) {
e.printStackTrace();
}
}
}
}
import java.io.*;
import java.net.*;
public class Tools {
final static Object lock = new Object();
public static void makeHtml(String page, String filePath){
makeHtml(page,filePath,"UTF-8");
}
public static void makeHtml(String page, String filePath,String chartset) {
synchronized (lock) {
HttpURLConnection huc = null;
BufferedReader br = null;
BufferedWriter bw = null;
try {
huc = (HttpURLConnection)new URL(page).openConnection();
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
System.setProperty("sun.net.client.defaultReadTimeout", "30000");
huc.connect();
InputStream stream = huc.getInputStream();
bw = new BufferedWriter(new OutputStreamWriter (new FileOutputStream(filePath),chartset));
br = new BufferedReader(new InputStreamReader(stream, chartset));
String line;
while((line = br.readLine())!= null){
if(line.trim().length() > 0){
bw.write(line);
bw.newLine();
}
}
}catch (Exception e) {
e.printStackTrace();
}finally {
try {
br.close();
bw.close();
huc.disconnect();
}catch (Exception e) {
e.printStackTrace();
}
}
}
}
评论: 0 | 引用: 0 | 查看次数: 486
发表评论