下面就是我在实际项目中用到的静态页面生成方法(jdk 1.4)
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();
}
}
}
}