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();
    }


}

评论
发表评论

您还没有登录,请登录后发表评论

zeroblue
搜索本博客
最近加入圈子
存档
最新评论