欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java 項(xiàng)目生成靜態(tài)頁面的代碼

 更新時(shí)間:2009年07月01日 01:44:23   作者:  
第一次做項(xiàng)目需要生成靜態(tài)頁面,網(wǎng)上很多大牛對將網(wǎng)頁生成靜態(tài)頁面有很多異議。說一下我的看法。
不外乎有以下因素:
1、從頁面加載時(shí)間來看:靜態(tài)頁面不需要與數(shù)據(jù)庫建立連接,尤其是訪問數(shù)據(jù)量較大的頁面,這種頁面大多要查很多結(jié)果集,因此建立連接次數(shù)就增多了,時(shí)間不可觀,而靜態(tài)頁面則省去了這些時(shí)間。
2、從便于搜索引擎抓取的角度來講:搜索引擎更喜歡靜態(tài)的網(wǎng)頁,靜態(tài)網(wǎng)頁與動態(tài)網(wǎng)頁相比,搜索引擎更喜歡靜的,更便于抓取,搜索引擎SEO排名更容易提高,一些大門戶站頁面大多都采用靜態(tài)或偽靜態(tài)網(wǎng)頁來顯示,更便于搜索引擎抓取與排名。
3、從安全性來看:靜態(tài)網(wǎng)頁不宜遭到黑客攻擊,因?yàn)楹诳筒恢滥愕木W(wǎng)站的后臺、網(wǎng)站采用程序、數(shù)據(jù)庫的地址。
4、從穩(wěn)定性來看:哪天數(shù)據(jù)庫服務(wù)器掛了,動態(tài)網(wǎng)頁就拜拜了!而要運(yùn)行一個靜態(tài)網(wǎng)頁的發(fā)布服務(wù)器,相信大家都知道配置不是太高也行的吧?呵呵。

因此,我認(rèn)為,生成靜態(tài)頁面具有可行性。

那么怎么把動態(tài)網(wǎng)頁的代碼生成靜態(tài)網(wǎng)頁呢?又存在哪呢?原理其實(shí)很簡單。
1、利用Freemark模板生成靜態(tài)頁面,網(wǎng)上搜一下大把大把的代碼隨你挑,我就不在這里啰嗦了。
我很討厭這種方式,因?yàn)閷τ谝粋€數(shù)據(jù)量較大的頁面來講工作量太大,要寫模板,語法又比較怪異,不流行!
2、也是我偶爾想起來的。用Java中URLConnection抓取某個URL網(wǎng)頁源碼(這是原理核心)生成html文件,就是這么簡單!就是這么Easy!

代碼奉上!

1)、以下是捕捉網(wǎng)頁源碼程序:
復(fù)制代碼 代碼如下:

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

/**
* @author Xing,XiuDong
*/
public class HTMLGenerator {

    public static final String generate(final String url) {
        if (StringUtils.isBlank(url)) {
            return null;
        }

        Pattern pattern = Pattern.compile("(http://|https://){1}[\\w\\.\\-/:]+");
        Matcher matcher = pattern.matcher(url);
        if (!matcher.find()) {
            return null;
        }

        StringBuffer sb = new StringBuffer();

        try {
            URL _url = new URL(url);
            URLConnection urlConnection = _url.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                sb.append(inputLine);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return sb.toString();
    }

    /**
     * Test Code
     * Target : http://www.google.cn/
     */
    public static void main(String[] args) throws IOException {
        String src = HTMLGenerator.generate("http://www.google.cn/");

        File file = new File("C:" + File.separator + "index.html");
        FileUtils.writeStringToFile(file, src, "UTF-8");
    }

}

2)、將源碼寫入Html文件,這個需要根據(jù)用戶的需求了,我根據(jù)我項(xiàng)目中遇到的情況寫了以下代碼:
復(fù)制代碼 代碼如下:

    /**
     * generite html source code
     *
     * @author Xing,XiuDong
     * @date 2009.06.22
     * @param request
     * @param url
     * @param toWebRoot
     * @param encoding
     * @throws IOException
     */
    public void genHtml(HttpServletRequest request, String url, boolean toWebRoot, String encoding) throws IOException {

        if (null == url) {
            url = request.getRequestURL().toString();
        }

        String contextPath = request.getContextPath();
        String seq = StringUtils.substring(String.valueOf(new Date().getTime()), -6);

        String ctxPath = super.getServlet().getServletContext().getRealPath(File.separator);
        if (!ctxPath.endsWith(File.separator)) {
            ctxPath += File.separator;
        }

        String filePath = StringUtils.substringAfter(url, contextPath);
        filePath = filePath.replaceAll("\\.(do|jsp|html|shtml)$", ".html");

        String savePath = "";
        String autoCreatedDateDir = "";
        if (!toWebRoot) {
            savePath = StringUtils.join(new String[] { "files", "history", "" }, File.separator);

            String[] folderPatterns = new String[] { "yyyy", "MM", "dd", "" };
            autoCreatedDateDir = DateFormatUtils.format(new Date(), StringUtils.join(folderPatterns, File.separator));

            filePath = StringUtils.substringBefore(filePath, ".html") + "-" + seq + ".html";
        }

        File file = new File(ctxPath + savePath + autoCreatedDateDir + filePath);
        FileUtils.writeStringToFile(file, HTMLGenerator.generate(url), encoding);
    }

來源:http://blog.csdn.net/xxd851116

相關(guān)文章

最新評論