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提供的編輯器功能一堆,華而不實,非常不好用,我試著重新編輯,結果無功而返。

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


          網站導航:
           
          主站蜘蛛池模板: 响水县| 南丹县| 淮滨县| 汝南县| 卓资县| 萝北县| 达孜县| 天峻县| 汉沽区| 涿州市| 亳州市| 广德县| 陈巴尔虎旗| 庐江县| 兰西县| 山阴县| 盘锦市| 赤水市| 龙州县| 阳曲县| 且末县| 天门市| 梅河口市| 同仁县| 盱眙县| 乡城县| 杭锦后旗| 福贡县| 清水河县| 永胜县| 丰台区| 洪泽县| 江西省| 道孚县| 加查县| 白朗县| 岐山县| 双峰县| 什邡市| 揭东县| 咸丰县|