CMS 内容页sitemap.xml 获取

来源:互联网 发布:js null===undefined 编辑:程序博客网 时间:2024/04/25 06:47
package com.tidemedia.cutv;

import java.io.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import tidemedia.cms.base.MessageException;
import tidemedia.cms.base.TableUtil;
import tidemedia.cms.system.Channel;
import tidemedia.cms.system.CmsCache;
import tidemedia.cms.system.Document;
import tidemedia.cms.system.Site;
import tidemedia.cms.util.FileUtil;
import tidemedia.cms.util.Util;

public class ExportSiteMapXMLDate {

    public ExportSiteMapXMLDate() {

    }

    public void emportXml() throws MessageException, SQLException, JSONException {
        // 新闻,城市,科教,影视,生活,评论,娱乐,房产,旅游
        String content = "[{\"channelid\":\"169,170\"},{\"channelid\":\"8800\"}]";
        JSONArray ja = new JSONArray(content);
        for(int i=0 ;i<ja.length();i++){
            JSONObject jb =ja.getJSONObject(i);
            String channelid = jb.getString("channelid");
            emportXml_(channelid);
            
        }
    
    }

    public void emportXml_(String channelids) throws MessageException,
            SQLException {
        String channelid[] = channelids.split(",");
        Channel channel = CmsCache.getChannel(Util.parseInt(channelid[0]));
        System.out.println("siteid="+channel.getSite().getSiteFolder());
    
        
        long t1 = System.currentTimeMillis();//获得当前时间毫秒
        String xmlfilepath = channel.getSite().getSiteFolder();
        if (!xmlfilepath.endsWith("/")) {
            xmlfilepath = xmlfilepath + "/";
        }
        if (!xmlfilepath.startsWith("/")) {
            xmlfilepath = "/" + xmlfilepath;
        }
        String xmlfile = xmlfilepath + "test_sitemap.xml";
        String strxml = "";
        try {
            strxml = (new StringBuilder(String.valueOf(strxml))).append(
                    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n").toString();
            strxml = (new StringBuilder(String.valueOf(strxml))).append(
                    "<sitemapindex>").toString();
            strxml = (new StringBuilder(String.valueOf(strxml))).append(
                    "<sitemap>").toString();
            long now = System.currentTimeMillis(); // 现在经过的毫秒数
            long time = now / 1000;// 现在经过的秒数
            long time2 = time - 2 * 24 * 60 * 60;// 少两天的天数
         
            /**
             * 内容页循环
             */
            for (int i = 0; i < channelid.length; i++) {
                TableUtil tu = new TableUtil();
                //Channel ch = CmsCache.getChannel(channelid[i]);
                Channel ch = CmsCache.getChannel(Util.parseInt(channelid[i]));


                //System.out.println("time2--->" + time2);

                String sql = "select GlobalID from "
                        + ch.getTableName()
                        + " where Status=1 and ChannelCode like '"+ch.getChannelCode()+"%' and CreateDate>"
                        + time2;
                System.out.println("sql----->" + sql);
                ResultSet rs = tu.executeQuery(sql);
                Document doc = null;
                while (rs.next()) {
                    int globalid = rs.getInt("GlobalID");
                    doc = new Document(globalid);
                     System.out.println("globalid--->"+globalid);
                    strxml = (new StringBuilder(String.valueOf(strxml)))
                            .append("<loc>").append(doc.getHttpHref()).append(
                                    "</loc>").toString();
                }
                System.out.println("loc--->" + doc.getHttpHref());
                tu.closeRs(rs);
            }
            
            strxml = (new StringBuilder(String.valueOf(strxml))).append(
                    "</sitemap>").toString();
            strxml = (new StringBuilder(String.valueOf(strxml))).append(
                    "</sitemapindex>").toString();

            PrintWriter out1 = new PrintWriter(new BufferedWriter(
                    new FileWriter(xmlfile)));
            out1.write(strxml);
            out1.close();
            int SiteId = channel.getSite().getId();
            Site site = CmsCache.getSite(SiteId);
            String SiteFolder = site.getSiteFolder();
            FileUtil fileutil = new FileUtil();
            fileutil
                    .PublishFiles("test_sitemap.xml", "/", SiteFolder, 13, site);
        } catch (IOException e) {
            e.printStackTrace();
        }
        File xml_file = new File(xmlfile);
        int filesize = (int) (xml_file.length() / 1024L);
        long t2 = System.currentTimeMillis();
        long lasttime = t2 - t1;
        System.out.println("---------end-----");
    }
}

原创粉丝点击