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

本文介绍了一个使用Java编写的静态页面批量生成工具。该工具通过输入URL和对应的本地保存路径,能够从远程服务器抓取HTML内容并将其保存为本地文件。文章提供了完整的代码示例,包括异常处理和字符集设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值