免费在线a视频-免费在线观看a视频-免费在线观看大片影视大全-免费在线观看的视频-色播丁香-色播基地

java在tomcat里實現計劃任務定時生成SiteMap

:2019年12月12日 本站(微博
分享到:

站想要讓搜索引擎搜到,必須設置好keywords,description,title,做好SEO優化,同時sitemap.xml也是不可缺少的通過Google,得到一些幫助,把自己做的整理一下:第一、對文件進行查找:  &nbs...

站想要讓搜索引擎搜到,必須設置好keywords,description,title,做好SEO優化,同時sitemap.xml也是不可缺少的
通過Google,得到一些幫助,把自己做的整理一下:
第一、對文件進行查找:
 
    public class FileDemo {
      File myDir;
      File[] contents;
      List fileList;
      Iterator currentFileIt;
      File currentFile;
      String path;
     
      /**
       * 無參的構造函數
       * */
      public FileDemo() {
        path = new String("");
        fileList = new ArrayList();
      }
     
      /**
       * 有參的構造函數
       * */
      public FileDemo(String path) {
        this.path = path;
        fileList = new ArrayList();
      }
     
      /**
       * 設置要查看的文件路徑
       */
      public void setPath(String path) {
        this.path = path;
      }
     
      /***************************************************************************
       * 返回當前目錄的路徑
       */
      public String getDirectory() {
        return myDir.getPath();
      }
     
      public void refreshList() {
        if (this.path.equals(""))
          path = "c:\\";
        myDir = new File(path);
     
        fileList.clear();
        contents = myDir.listFiles();
        // 重新裝入路徑下文件
        for (int i = 0; i < contents.length; i++) {
          fileList.add(contents[i]);
        }
        currentFileIt = fileList.iterator();
      }
     
      /**
       * 指到下一個條目
       */
      public boolean nextFile() {
        while (currentFileIt.hasNext()) {
          currentFile = (File) currentFileIt.next();
          
          return true;
        }
        return false;
      }
     
      /**
       * 返回當前指向的文件對象的文件名稱
       */
      public String getFileName() {
        return currentFile.getName();
      }
     
      /**
       * 返回當前指向的文件對象的文件尺寸
       */
      public String getFileSize() {
        return convertFileSize(currentFile.length());
      }
     
      /**
       * 轉換文件尺寸為指定格式。
       */
      private String convertFileSize(long size) {
        int divisor = 1;
        String unit = "bytes";
        if (size >= 1024 * 1024) {
          divisor = 1024 * 1024;
          unit = "MB";
        } else if (size >= 1024) {
          divisor = 1024;
          unit = "KB";
        }
        if (divisor == 1)
          return size / divisor + " " + unit;
        String aftercomma = "" + 100 * (size % divisor) / divisor;
        if (aftercomma.length() == 1)
          aftercomma = "0" + aftercomma;
        return size / divisor + "." + aftercomma + " " + unit;
      }
     
      /**
       * 返回文件的最后修改日期
       */
      public String getFileTimeStamp() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = sdf.format(new Date(currentFile.lastModified()));
     
        return dateString;
      }
     
      /**
       * 返回文件是不是一個目錄
       */
      public boolean isDirectory() {
        return currentFile.isDirectory();
      }
     
    }
 
第二、創建一個任務類,繼承TimerTask
    public class XMLParese extends TimerTask{
      
      private ServletContext context;
      
      public XMLParese(ServletContext context){
        this.context=context;
      }
      
      @Override
      public void run() {
        // TODO Auto-generated method stub
        createSiteMap();
        
      }
      public void createSiteMap() {
     
        String priority = "0.75";// 級 別
        String changefreq = "daily";// "weekly";//頻 率
        String xmlpath = "e:/sitemap.xml";// sitemap名稱以及位置    
        String homeurl = "http://www.jsedu114.com"; // 欄目首頁
        String []directory={"promotion","news","brand","goods","services","shop","winelive"};
        FileDemo fp = new FileDemo();
        try {
          Document document = DocumentHelper.createDocument();
          Element urlsetElement = document.addElement("urlset");
          urlsetElement.addAttribute("xmlns ",
              "http://www.sitemaps.org/schemas/sitemap/0.9"); // "xmlns
                                      // "必須要有空格,否則會報錯
          urlsetElement.addAttribute("xmlns",
              "http://www.sitemaps.org/schemas/sitemap/0.9");
     
          urlsetElement.addAttribute("xmlns:xsi",
              "http://www.w3.org/2001/XMLSchema-instance");
     
          urlsetElement.addAttribute("xsi:schemaLocation",
              "http://www.sitemaps.org/schemas/sitemap/0.9 " +
     
              "http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd");
          
          //創建url根元素
          Element urlElement ;
          //為url根元素創建loc網頁地址,lastmod更新時間,changefreq更改頻率和priority級別
          Element locElement ;
          Element lastmodElement;
          Element changefreqElement;
          Element priorityElement;
          fp.setPath("D:\\Program Files\\Apache Software Foundation\\Tomcat 6.0\\webapps\\ROOT\\");
          fp.refreshList();
          while (fp.nextFile()) {
            if (!fp.isDirectory()) {
              homeurl="http://www.jsedu114.com/";
              String f=fp.getFileName();
              String fname=f.substring(f.lastIndexOf("."));
              if((fname.equals(".html")||fname.equals(".htm")) && !f.equals("login.html")){
                urlElement = urlsetElement.addElement("url");
                locElement = urlElement.addElement("loc");
                lastmodElement = urlElement.addElement("lastmod");
                changefreqElement = urlElement.addElement("changefreq");
                priorityElement = urlElement.addElement("priority");
                
                //導航賦值
                homeurl = homeurl + fp.getFileName();
                locElement.setText(homeurl);
                lastmodElement.setText(fp.getFileTimeStamp());// 這里時間是你更新時間,這里暫時統一
                changefreqElement.setText(changefreq);
                priorityElement.setText(priority);
              }
            }
          }
          
                //各個目錄下的文件
          for(int i=0;i<directory.length;i++){
            fp.setPath("D:\\Program Files\\Apache Software Foundation\\Tomcat 6.0\\webapps\\ROOT\\"+directory[i]+"\\");
            fp.refreshList();
            while (fp.nextFile()) {
              homeurl="http://www.jsedu114.com/"+directory[i]+"/";              
              urlElement = urlsetElement.addElement("url");
              locElement = urlElement.addElement("loc");
              lastmodElement = urlElement.addElement("lastmod");
              changefreqElement = urlElement.addElement("changefreq");
              priorityElement = urlElement.addElement("priority");              
              
              homeurl = homeurl + fp.getFileName();
              locElement.setText(homeurl);
              lastmodElement.setText(fp.getFileTimeStamp());// 這里時間是你更新時間,這里暫時統一
              changefreqElement.setText(changefreq);
              priorityElement.setText(priority);
            }
          }
     
          XMLWriter writer = new XMLWriter(new FileOutputStream(new File(
              xmlpath)));
          writer.write(document);
          writer.close();
          document = null;
          // 格式化
          formatXMLFile(xmlpath, "UTF-8");
        } catch (Exception ex) {
          ex.printStackTrace();
     
        }
     
      }
     
      /**
       * 格式化XML文檔,并解決中文問題
       * @param xmlpath:xml文件路徑
       * @param charSet:格式化的字符集
       * @return
       */
      public static boolean formatXMLFile(String xmlpath, String charSet) {
     
        boolean returnValue = false;
        try {
     
          SAXReader saxReader = new SAXReader();
          Document document = saxReader.read(new File(xmlpath));
          XMLWriter output = null;
          OutputFormat format = OutputFormat.createPrettyPrint();
          format.setEncoding(charSet);
          output = new XMLWriter(new FileWriter(new File(xmlpath)), format);
          output.write(document);
          output.close();
          document = null;
          saxReader = null;
          returnValue = true;
        } catch (Exception ex) {
          ex.printStackTrace();
        }
        return returnValue;
      }
     
    }
 
 第三、創建一個任務監聽類,實現ServletContextListener 接口
 
    public class TimerListener implements ServletContextListener {
      // 設置啟動時間為23點;
      private static final int hours = 23;
      private static final int minutes = 0;
      private static final int seconds = 0;
      private Timer timer = null;
     
      public void contextDestroyed(ServletContextEvent event) {
        timer.cancel();
        event.getServletContext().log("定時器銷毀");
      }
     
      public void contextInitialized(ServletContextEvent event) {
            timer=new Timer(true);
            event.getServletContext().log("定時器已啟動");
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY, hours);
            calendar.set(Calendar.MINUTE, minutes);
            calendar.set(Calendar.SECOND, seconds);
            
            //SiteMap生成
            //間隔時間1天生成一次
            timer.schedule(new XMLParese(event.getServletContext()),calendar.getTime(),
                1*24*60*60*1000);
      }
    }
 
第四、在web中添加一個監聽器
       web.xml里的配置
 
<!-- 定時器 -->
<listener>
  <listener-class>timer.TimerListener</listener-class>
</listener>

[我要糾錯]
[ 編輯:宋聰喬 &發表于江蘇 ]
關鍵詞: 想要 搜索引擎 必須 設置 keywords

來源:本文內容搜集或轉自各大網絡平臺,并已注明來源、出處,如果轉載侵犯您的版權或非授權發布,請聯系小編,我們會及時審核處理。
聲明:江蘇教育黃頁對文中觀點保持中立,對所包含內容的準確性、可靠性或者完整性不提供任何明示或暗示的保證,不對文章觀點負責,僅作分享之用,文章版權及插圖屬于原作者。

點個贊
0
踩一腳
0

您在閱讀:java在tomcat里實現計劃任務定時生成SiteMap

Copyright©2013-2025 ?JSedu114 All Rights Reserved. 江蘇教育信息綜合發布查詢平臺保留所有權利

蘇公網安備32010402000125 蘇ICP備14051488號-3技術支持:南京博盛藍睿網絡科技有限公司

南京思必達教育科技有限公司版權所有   百度統計

主站蜘蛛池模板: 奇米影视一区二区三区 | 亚洲精品第四页中文字幕 | 天天天操操操 | 狠狠色狠狠色综合久久一 | haose08永久免费视频 | 国产亚洲sss在线播放 | 日韩黄色大全 | 欧美一级美片在线观看免费 | 亚洲精品视频专区 | 黄色免费看视频 | 日本一区二区不卡视频 | 一区二区三区亚洲视频 | 国产免费观看嫩草影院 | 日韩无毛 | 国产你懂的在线 | 男18视频在线观看 | 欧美日韩中文一区 | 色噜噜狠狠狠狠色综合久 | 美女一级a毛片免费观看 | 新午夜影院 | 黄色a∨ | 日日摸夜夜添夜夜免费视 | ww久久 | 我亲爱的朋友们免费看 | 高清色视频 | 午夜性爽快 | 国产精品久久久久9999高清 | 日韩特黄特色大片免费视频 | 免费人成在线观看播放国产 | 欧美午夜性刺激在线观看免费 | 日本一区二区三区视频在线 | 中文字幕视频一区 | 欧美成人爽片 | 国产欧美日韩另类va在线 | 亚洲欧美一区在线 | 视频一本大道香蕉久在线播放 | 黄篇免费看| 午夜欧美福利视频 | 艾草在线精品视频播放 | 日本视频网站在线www色 | 日本三级网站在线观看 |
最熱文章
最新文章
  • 阿里云上云鉅惠,云產品享最低成本,有需要聯系,
  • 卡爾蔡司鏡片優惠店,鏡片價格低
  • 蘋果原裝手機殼