页面静态化

网站提高性能的方案有很多,网站架构方面考虑,最初的性能优化可以考虑提高单台服务器的配置。把数据库和代码分别部署在两台服务器,页面缓存,数据缓存,静态化,分布式,代码读写分离,负载均衡。这些东西都是大型网站发展所必须经历的升级过程,前两种方案非常容易实现,页面缓存J2EE中用的不多,数据缓存可以借助hibernate提供的第三方支持,分布式和读写分离,我的方案是使用EJB3,均衡负载可由Apache+Tomcat提供,本文简单说一下页面静态化技术,

也就是jsp通过html模板生成html页面,比较高级点的处理就是让它自动静态化,其实无论使用什么框架原理都是相通的。在这我提供一个纯java静态化的类,希望对大家有所帮助!

public static void convertHtml(String sUrl, String charset,
            String sSavePath, String sHtmlFile) throws IOException {
        int HttpResult;
        URL url = new URL(sUrl);
        URLConnection urlconn = url.openConnection();
        urlconn.connect();
        HttpURLConnection httpconn = (HttpURLConnection) urlconn;
        HttpResult = httpconn.getResponseCode();
        if (HttpResult != HttpURLConnection.HTTP_OK) {
        } else {
            InputStreamReader isr = new InputStreamReader(
                    httpconn.getInputStream(), charset);
            BufferedReader in = new BufferedReader(isr);
            String inputLine;
            if (!sSavePath.endsWith("/")) {
                sSavePath += "/";
            }
            FileOutputStream fout = new FileOutputStream(sSavePath + sHtmlFile);
            while ((inputLine = in.readLine()) != null) {
                System.out.println(inputLine);
                fout.write((inputLine + "/n").getBytes());
            }
            in.close();
            fout.close();
        }
    }

    public static void main(String[] args) throws IOException {
        StaticTest ru = new StaticTest();
        String filePath = ru.getClass().getResource(".").getPath().toString(); // 取得项目根目录
        convertHtml("http://zl.aisky.net", "UTF-8", filePath + "/",
                "index.html");
    }

猜你喜欢

转载自u012081441.iteye.com/blog/2382957