framework freemarker / velocity

CMS 静态化参考

JAVA静态化,如何生成html问题

http://hi.csdn.net/yuezu1026/

http://topic.csdn.net/u/20080829/14/7ee4965f-f0d3-4c84-a2fa-10d44b735d63.html

http://hi.csdn.net/yuezu1026/

中国灯饰商贸网

www.lighting86.com.cn

中山市旅游网

http://www.86760.com/

public class Test {

    //config.getDriverName(),config.getUrl(),config.getUserName(),config.getPassword());
    public static void main(String[] args) {
        Config config = ConfigResolver.parseXML(null);
       
        String jspPage = config.getIndexFrom();//jsp文件地址
        String htmlPage = config.getIndexTo();//html文件名 
        if(jspPage!=null && htmlPage!=null ){
            createHtml(jspPage,htmlPage); //处理站点首页
        }
       
        jspPage = config.getGroupZhengquanFrom();//jsp文件地址
        htmlPage = config.getGroupZhengquanTo();//html文件名
        if(jspPage!=null && htmlPage!=null ){
            createHtml(jspPage,htmlPage); //处理证券圈子页面
        }
public static void createHtml(String jspPage,String htmlPage){
        System.out.println(jspPage+"----------------start");
        BufferedReader in = null;
        BufferedWriter out= null;
        try{
            in = new BufferedReader(new InputStreamReader(new FileInputStream(htmlPage)));
            out = getOut(htmlPage+"_old", true); //备分原文件
            copyFile(in,out); //备分原文件
        }catch(Exception e){
            e.printStackTrace();
        }       
       
        in = getIn(jspPage);
        out = getOut(htmlPage+"_back", true);
        generateHtml(in, out,jspPage);//生成静态页面
        close(in, out);
       
        try{
            in=new BufferedReader(new InputStreamReader(new FileInputStream(htmlPage+"_back")));
        }catch(Exception e){
            e.printStackTrace();
        }
        out = getOut(htmlPage, true);
        copyFile(in,out); //文件拷贝
        System.out.println(jspPage+"----------------end");
    }

    public final static void generateHtml(BufferedReader in, BufferedWriter out,String jspPage) {
        boolean flag=true;

        if (out == null)
            return;//不生成静态页面
        try {
            out.write(" <%@ page language=\"java\" %>\r\n");
            out.write(" <%@ page contentType=\"text/html; charset=GBK\"%>\r\n");
            String c;
            while ((c = in.readLine()) != null) {
//                if(c.trim().equals(" <!--portalheaderbegin-->")){           
//                    flag=false;
//                }
//                if(c.trim().equals(" <!--portalheaderend-->")){
//                    if(jspPage.indexOf("channel.jsp")!=-1){ //如果是频道页面
//                        c=" <jsp:include page='/include/portalheader.jsp?channelId="+jspPage.split("type_id=")[1]+"' />\r\n";
//                    }else if(jspPage.indexOf("zhengquan_group_act.jsp")!=-1){ //证券圈
//                        c=" <jsp:include page='/include/portalheader.jsp?groupId=43' />\r\n";
//                    }else if(jspPage.indexOf("group_general.jsp")!=-1){ //其他圈子
//                        c=" <jsp:include page='/include/portalheader.jsp?groupId="+jspPage.split("type_id=")[1]+"' />\r\n";
//                    }else if(jspPage.indexOf("group_index")!=-1){ //圈子首页
//                        c=" <jsp:include page='/include/portalheader.jsp?groupId=group' />\r\n";
//                    }else if(jspPage.indexOf("community_index")!=-1){ //社区首页
//                        c=" <jsp:include page='/include/portalheader.jsp?shequId=abc123' />\r\n";
//                    }else{
//                        c=" <jsp:include page='/include/portalheader.jsp' />\r\n";
//                    }
//                    flag=true;
//                }
               
                if(c.trim().equals(" <!--portalheaderbegin-->")){           
                    flag=false;
                }
               
                if(c.trim().equals(" <!--portalheaderend-->")){
                    if(jspPage.indexOf("channel.jsp")!=-1){ //如果是频道页面
                        //c=" <jsp:include page='/include/portalheader.jsp?channelId="+jspPage.split("type_id=")[1]+"' />\r\n";
                        c=" <jsp:include page='/permissionFilter.jsp' />\r\n";
                    }else if(jspPage.indexOf("zhengquan_group_act.jsp")!=-1){ //证券圈
                        c=" <jsp:include page='/include/portalheader.jsp?groupId=43' />\r\n";
                    }else if(jspPage.indexOf("group_general.jsp")!=-1){ //其他圈子
                        //c=" <jsp:include page='/include/portalheader.jsp?groupId="+jspPage.split("type_id=")[1]+"' />\r\n";
                        c=" <jsp:include page='/permissionFilter.jsp' />\r\n";
                    }else if(jspPage.indexOf("group_index")!=-1){ //圈子首页
                        c=" <jsp:include page='/include/portalheader.jsp?groupId=group' />\r\n";
                    }else if(jspPage.indexOf("community_index")!=-1){ //社区首页
                        c=" <jsp:include page='/include/portalheader.jsp?shequId=abc123' />\r\n";
                    }else if(jspPage.indexOf("portal/index_act.jsp")!=-1){ //首页
                        //c=" <jsp:include page='/include/portalheader.jsp?index=yes' />\r\n";
                        c=" <jsp:include page='/permissionFilter.jsp' />\r\n";
                    }else if(jspPage.indexOf("portal/blogershare_act.jsp")!=-1){ //分享页
                        c=" <jsp:include page='/permissionFilter.jsp' />\r\n";
                    }else{
                        c=" <jsp:include page='/include/portalheader.jsp' />\r\n";
                    }
                    flag=true;
                }
                if(flag&&c.length()>0){
                    out.write(c+"\r\n");
                }
            }
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
public final static void copyFile(BufferedReader from, BufferedWriter to) {
        if (to == null || from == null)
            return;//不生成静态页面
        try {
            int c;
                while ((c = from.read()) != -1) {
                    to.write((char)c);
            }
            to.flush();
            close(from, to);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public final static BufferedReader getIn(String jspPage) {

        BufferedReader in = null;
        URL url = null;
        URLConnection urlConn = null;
        try {
            url = new URL(jspPage);
            urlConn = url.openConnection();//建立http连接
            in = new BufferedReader(new InputStreamReader(urlConn
                    .getInputStream(), "GBK"));//设置Encoding,必须设置,否则显示中文会有问题
        } catch (IOException e) {
            e.printStackTrace();
           
        }finally{
            urlConn=null;
            url=null;
        }
       

        return in;
    }

    public final static BufferedWriter getOut(String htmlPage, boolean flag) {

        BufferedWriter out = null;

        try {
            File htmlFile = new File(htmlPage);
            if (flag) {
                htmlFile.createNewFile();
            } else {//如果flag为false则不覆盖文件
                if (htmlFile.exists()) {//如果文件已经存在则返回null
                    return null;
                } else {//建立新文件
                    htmlFile.createNewFile();
                }
            }
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(htmlFile), "GBK"));//设置Encoding
        } catch (IOException e) {
            e.printStackTrace();
        }

        return out;

    }//flag为真则覆盖原文件

    public final static void close(BufferedReader in, BufferedWriter out) {

        try {
            in.close();
            if (out != null)
                out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
public class ConfigResolver {
    private static String xmlPath = "config/config.xml";

    /**
    * 获取配置文件对象信息
    *
    * @param fileName
    * @return
    * @throws Exception
    */
    public static Config parseXML(String fileName) {
        if (null != fileName) {
            xmlPath = fileName;
        }
        Config configFile = new Config();
        try {
            XMLConfiguration parser = new XMLConfiguration();
            parser.setFile(new File(xmlPath));
            parser.load();

            //首页原
            String indexFrom = (String) parser.getProperty("indexFrom");
            configFile.setIndexFrom(indexFrom);
            //首页目标
            String indexTo = (String) parser.getProperty("indexTo");
            configFile.setIndexTo(indexTo);

            //圈子页原
            String groupZhengquanFrom = (String) parser
                    .getProperty("groupZhengquanFrom");
            configFile.setGroupZhengquanFrom(groupZhengquanFrom);
            //圈子页目标
            String groupZhengquanTo = (String) parser
                    .getProperty("groupZhengquanTo");
            configFile.setGroupZhengquanTo(groupZhengquanTo);

            //频道页原
            String channelFrom = (String) parser.getProperty("channelFrom");
            configFile.setChannelFrom(channelFrom);
            //频道页目标
            String channelTo = (String) parser.getProperty("channelTo");
            configFile.setChannelTo(channelTo);

            String channelIds = (String) parser.getProperty("channelIds");
            configFile.setChannelIds(channelIds);

            //大杂烩原
            String dazahuiFrom = (String) parser.getProperty("dazahuiFrom");
            configFile.setDazahuiFrom(dazahuiFrom);
            //大杂烩目标
            String dazahuiTo = (String) parser.getProperty("dazahuiTo");
            configFile.setDazahuiTo(dazahuiTo);

            //别人的订阅,关注原
            String anthorTakenFrom = (String) parser
                    .getProperty("anthorTakenFrom");
            configFile.setAnthorTakenFrom(anthorTakenFrom);
            //别人的订阅,关注目标
            String anthorTakenTo = (String) parser.getProperty("anthorTakenTo");
            configFile.setAnthorTakenTo(anthorTakenTo);

            //房产圈子页原
            String fangChanFrom = (String) parser
                    .getProperty("groupFangchanFrom");
            configFile.setGroupFangchanFrom(fangChanFrom);
            //房产圈子页目标
            String fangChanTo = (String) parser.getProperty("groupFangchanTo");
            configFile.setGroupFangchanTo(fangChanTo);
            String groupIds = (String) parser.getProperty("groupIds");
            configFile.setGroupIds(groupIds);

            //首页用圈子文章list原
            String quanziArticleListFrom = (String) parser.getProperty("quanziArticleListFrom");
            configFile.setQuanziArticleListFrom(quanziArticleListFrom);
            //首页用圈子文章list目标
            String quanziArticleListTo = (String) parser.getProperty("quanziArticleListTo");
            configFile.setQuanziArticleListTo(quanziArticleListTo);
           
            String quanziIds = (String) parser.getProperty("quanziIds");
            configFile.setQuanziIds(quanziIds);
           
           
            //圈子首页原
            String groupIndexFrom = (String) parser.getProperty("groupIndexFrom");
            configFile.setGroupIndexFrom(groupIndexFrom);
            //圈子首页目标
            String groupIndexTo = (String) parser.getProperty("groupIndexTo");
            configFile.setGroupIndexTo(groupIndexTo);
           
            //社区首页原
            String shequIndexFrom = (String) parser.getProperty("shequIndexFrom");
            configFile.setShequIndexFrom(shequIndexFrom);
            //社区首页目标
            String shequIndexTo = (String) parser.getProperty("shequIndexTo");
            configFile.setShequIndexTo(shequIndexTo);
           
            //热点排名周源
            String articleRankWeekFrom = (String) parser.getProperty("articleRankWeekFrom");
            configFile.setArticleRankWeekFrom(articleRankWeekFrom);
            //热点排名周目标
            String articleRankWeekTo = (String) parser.getProperty("articleRankWeekTo");
            configFile.setArticleRankWeekTo(articleRankWeekTo);
           
            //热点排名月源
            String articleRankMonthFrom = (String) parser.getProperty("articleRankMonthFrom");
            configFile.setArticleRankMonthFrom(articleRankMonthFrom);
            //热点排名月目标
            String articleRankMonthTo = (String) parser.getProperty("articleRankMonthTo");
            configFile.setArticleRankMonthTo(articleRankMonthTo);
           
            //图片首页more源
            String photoIndexMoreFrom = (String) parser.getProperty("photoIndexMoreFrom");
            configFile.setPhotoIndexMoreFrom(photoIndexMoreFrom);
            //图片首页more目标
            String photoIndexMoreTo = (String) parser.getProperty("photoIndexMoreTo");
            configFile.setPhotoIndexMoreTo(photoIndexMoreTo);
           
            //博客分享源
            String blogerShareFrom = (String) parser.getProperty("blogerShareFrom");
            configFile.setBlogerShareFrom(blogerShareFrom);
            //博客分享目标
            String blogerShareTo = (String) parser.getProperty("blogerShareTo");
            configFile.setBlogerShareTo(blogerShareTo);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return configFile;
    }
}

end

原文地址:https://www.cnblogs.com/lindows/p/14390487.html