只有少量页面需要生成静态页面时,可以试试这个类

 

None.gif package  com.schedule;
None.gif
None.gif
import  java.io.BufferedReader;
None.gif
import  java.io.FileOutputStream;
None.gif
import  java.io.IOException;
None.gif
import  java.io.InputStreamReader;
None.gif
import  java.io.OutputStreamWriter;
None.gif
import  java.net.HttpURLConnection;
None.gif
import  java.net.MalformedURLException;
None.gif
import  java.net.URL;
None.gif
import  java.util.HashMap;
None.gif
import  java.util.Iterator;
None.gif
import  java.util.Map;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  StaticPageService  dot.gif {
InBlock.gif    
private  Map < String, String >  urlMap;
InBlock.gif
InBlock.gif    
private  String realPath;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public   void  createFile()  dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if  (urlMap  ==   null dot.gif {
InBlock.gif            
throw   new  NullPointerException( " The url map is should not be null! " );
ExpandedSubBlockEnd.gif        }

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

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

ExpandedSubBlockStart.gifContractedSubBlock.gif        }
  catch  (MalformedURLException e)  dot.gif {
InBlock.gif            e.printStackTrace();
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
  catch  (IOException e)  dot.gif {
InBlock.gif            e.printStackTrace();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** */ /**
InBlock.gif     * 
@param  urlMap
InBlock.gif     *            the urlMap to set
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public   void  setUrlMap(Map < String, String >  urlMap)  dot.gif {
InBlock.gif        
this .urlMap  =  urlMap;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** */ /**
InBlock.gif     * 
@param  realPath
InBlock.gif     *            the realPath to set
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public   void  setRealPath(String realPath)  dot.gif {
InBlock.gif        
this .realPath  =  realPath;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public   static   void  main(String[] args)  dot.gif {
InBlock.gif        StaticPageService service 
=   new  StaticPageService();
InBlock.gif        Map
< String, String >  urlMap  =   new  HashMap < String, String > ();
InBlock.gif        
// map的key为要生成静态页面的url,value为生成后的静态文件保存的路径
InBlock.gif
        urlMap.put( " http://localhost:8080/index.jsp " " /temp/index1.html " );
InBlock.gif        urlMap.put(
" http://www.yahoo.com.cn/ " " /temp/index2.html " );
InBlock.gif        
// 此参数用来设置当前Web应用的真实路径
InBlock.gif
        service.setRealPath( " d: " );
InBlock.gif        service.setUrlMap(urlMap);
InBlock.gif        service.createFile();
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值