ZeroBlue

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

          ?

          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為要生成靜態(tài)頁面的url,value為生成后的靜態(tài)文件保存的路徑
          ????????urlMap.put( " http://localhost:8080/index.jsp " ,? " /temp/index1.html " );
          ????????urlMap.put(
          " http://www.yahoo.com.cn/ " ,? " /temp/index2.html " );
          ????????
          // 此參數(shù)用來設(shè)置當(dāng)前Web應(yīng)用的真實(shí)路徑
          ????????service.setRealPath( " d: " );
          ????????service.setUrlMap(urlMap);
          ????????service.createFile();
          ????}


          }


          評論

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

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

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

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

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


          網(wǎng)站導(dǎo)航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           
          主站蜘蛛池模板: 北海市| 玛沁县| 庆阳市| 信宜市| 中阳县| 开封市| 济阳县| 招远市| 来安县| 株洲市| 靖西县| 鸡东县| 永济市| 门源| 老河口市| 敖汉旗| 望城县| 娄烦县| 罗江县| 蒲江县| 牙克石市| 津市市| 江孜县| 宁夏| 汨罗市| 高雄市| 卓资县| 襄城县| 彭州市| 从化市| 自贡市| 谷城县| 巧家县| 应城市| 深圳市| 汾西县| 瓦房店市| 米脂县| 龙海市| 邢台县| 勐海县|