09-01
06

Java生成静态页面的方法

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();
                }
            }
        }
}


文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: 静态化
相关日志:
评论: 0 | 引用: 0 | 查看次数: 486
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭