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

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


          網站導航:
           
          主站蜘蛛池模板: 峡江县| 凭祥市| 饶阳县| 舞钢市| 信丰县| 稻城县| 社旗县| 安乡县| 湟源县| 虞城县| 永修县| 九龙坡区| 应用必备| 泊头市| 景宁| 沧源| 江孜县| 武穴市| 潜山县| 松原市| 吉隆县| 诸城市| 乳源| 徐闻县| 泰来县| 甘泉县| 英山县| 前郭尔| 蚌埠市| 堆龙德庆县| 抚宁县| 巫溪县| 横山县| 梨树县| 平度市| 太白县| 富平县| 凯里市| 昂仁县| 贵德县| 西安市|