ZeroBlue

          留出時間,讓自己微笑。
          posts - 6, comments - 12, trackbacks - 0, articles - 0
             :: 首頁 ::  :: 聯系 ::  :: 管理

          搜索

          •  

          最新評論

          ?

          package ?com.schedule;

          import ?java.io.BufferedReader;
          import ?java.io.FileOutputStream;
          import ?java.io.IOException;
          import ?java.io.InputStreamReader;
          import ?java.io.OutputStreamWriter;
          import ?java.net.HttpURLConnection;
          import ?java.net.MalformedURLException;
          import ?java.net.URL;
          import ?java.util.HashMap;
          import ?java.util.Iterator;
          import ?java.util.Map;

          public ? class ?StaticPageService? {
          ????
          private ?Map < String,?String > ?urlMap;

          ????
          private ?String?realPath;

          ????
          public ? void ?createFile()? {
          ????????
          if ?(urlMap? == ? null )? {
          ????????????
          throw ? new ?NullPointerException( " The?url?map?is?should?not?be?null! " );
          ????????}

          ????????Iterator
          < String > ?keys? = ?urlMap.keySet().iterator();
          ????????String?inputLine;
          ????????String?enter?
          = ? " \n " ;
          ????????StringBuffer?buffer?
          = ? new ?StringBuffer();
          ????????
          try ? {
          ????????????
          while ?(keys.hasNext())? {
          ????????????????String?key?
          = ?keys.next();
          ????????????????URL?url?
          = ? new ?URL(key);
          ????????????????HttpURLConnection?uc?
          = ?(HttpURLConnection)?url.openConnection();
          ????????????????BufferedReader?in?
          = ? new ?BufferedReader( new ?InputStreamReader(uc.getInputStream()));
          ????????????????
          if (buffer.length() > 0 )buffer.delete( 0 ,buffer.length());
          ????????????????
          while ?((inputLine? = ?in.readLine())? != ? null )? {
          ????????????????????buffer.append(inputLine);
          ????????????????????buffer.append(enter);
          ????????????????}

          ????????????????String?file?
          = ?realPath? + ?urlMap.get(key);
          ????????????????OutputStreamWriter?fw?
          = ? new ?OutputStreamWriter(
          ????????????????????????
          new ?FileOutputStream(file),? " GBK " );
          ????????????????fw.write(buffer.toString());
          ????????????????in.close();
          ????????????????fw.close();
          ????????????}

          ????????}
          ? catch ?(MalformedURLException?e)? {
          ????????????e.printStackTrace();
          ????????}
          ? catch ?(IOException?e)? {
          ????????????e.printStackTrace();
          ????????}

          ????}


          ????
          /**
          ?????*?
          @param ?urlMap
          ?????*????????????the?urlMap?to?set
          ?????
          */

          ????
          public ? void ?setUrlMap(Map < String,?String > ?urlMap)? {
          ????????
          this .urlMap? = ?urlMap;
          ????}


          ????
          /**
          ?????*?
          @param ?realPath
          ?????*????????????the?realPath?to?set
          ?????
          */

          ????
          public ? void ?setRealPath(String?realPath)? {
          ????????
          this .realPath? = ?realPath;
          ????}


          ????
          public ? static ? void ?main(String[]?args)? {
          ????????StaticPageService?service?
          = ? new ?StaticPageService();
          ????????Map
          < String,?String > ?urlMap? = ? new ?HashMap < String,?String > ();
          ????????
          // map的key為要生成靜態頁面的url,value為生成后的靜態文件保存的路徑
          ????????urlMap.put( " http://localhost:8080/index.jsp " ,? " /temp/index1.html " );
          ????????urlMap.put(
          " http://www.yahoo.com.cn/ " ,? " /temp/index2.html " );
          ????????
          // 此參數用來設置當前Web應用的真實路徑
          ????????service.setRealPath( " d: " );
          ????????service.setUrlMap(urlMap);
          ????????service.createFile();
          ????}


          }


          評論

          # re: 只有少量頁面需要生成靜態頁面時,可以試試這個類  回復  更多評論   

          2006-08-31 11:50 by leeight262
          用過了,不錯

          # re: 只有少量頁面需要生成靜態頁面時,可以試試這個類  回復  更多評論   

          2007-09-13 16:00 by zeroblue
          代碼保存之后,以下語句多了空格,刪掉即可:
          service.setRealPath( " d: " );
          urlMap.put( " http://localhost:8080/index.jsp " , " /temp/index1.html " );
          urlMap.put( " http://www.yahoo.com.cn/ " , " /temp/index2.html " );
          new FileOutputStream(file), " GBK " );

          blogjava提供的編輯器功能一堆,華而不實,非常不好用,我試著重新編輯,結果無功而返。

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 深圳市| 高唐县| 扎兰屯市| 金堂县| 陆丰市| 潞城市| 桂林市| 赞皇县| 商都县| 民县| 青州市| 岳池县| 福海县| 化德县| 廊坊市| 东城区| 塔城市| 达日县| 犍为县| 江永县| 廊坊市| 江川县| 郎溪县| 鄂托克旗| 元阳县| 克山县| 宕昌县| 黄石市| 区。| 尉氏县| 甘德县| 富阳市| 开阳县| 乡宁县| 衡阳县| 读书| 本溪| 开化县| 勃利县| 江山市| 清流县|