分享

JAVA用URL下载网页为静态页面...

 知识储备库_wucl 2011-06-22

JAVA用URL下载网页为静态页面

关键字: java, url, 静态页面

1.可以作为项目的动态页面生成静态页面的功能

2.可以当做JSP小偷

3.可以当做全文网摘

Java代码  收藏代码
  1. package richin.reflect;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStreamReader;  
  7. import java.net.HttpURLConnection;  
  8. import java.net.MalformedURLException;  
  9. import java.net.URL;  
  10. import java.net.URLConnection;  
  11.   
  12. public class PageStatic {  
  13.     public static void convert2Html(String sFilePath,String sSavePath,String sHtmlFile) throws IOException  
  14.     {  
  15.         PageStatic ru = new PageStatic();   
  16.         String baseHref="<base href='"+sFilePath+"'></base>";  
  17.         String filePath = ru.getClass().getResource("/richin/reflect/PageStatic.class").getPath().toString(); //取得项目根目录  
  18.         filePath = filePath.substring(1, filePath.indexOf("WEB-INF"));   
  19.       
  20.         int HttpResult;   
  21.         String SavePath = filePath + sSavePath; //保存路径   
  22.         URL url=new URL(sFilePath);   
  23.         URLConnection urlconn=url.openConnection(); //抽象类 URLConnection 是所有类的超类,它代表应用程序和 URL 之间的通信链接,通过在 URL 上调用 openConnection 方法创建连接对象   
  24.         urlconn.addRequestProperty("Accept-Language""zh-cn");  
  25.         urlconn.connect(); //使用 connect 方法建立到远程对象的实际连接   
  26.         HttpURLConnection httpconn=(HttpURLConnection)urlconn; //每个 HttpURLConnection 实例都可用于生成单个请求,但是其他实例可以透明地共享连接到 HTTP 服务器的基础网络   
  27.   
  28.         HttpResult=httpconn.getResponseCode(); //getResponseCode可以从 HTTP 响应消息获取状态码   
  29.   
  30.         if(HttpResult!=HttpURLConnection.HTTP_OK) {   
  31.         } else {   
  32.             String charset=httpconn.getContentType();  
  33.             //System.out.println(charset);  
  34.             String charType=charset.substring(charset.lastIndexOf("=")+1);  
  35.             //System.out.println(charType);  
  36.             if("text/html".equals(charType))  
  37.                 charType="GBK";  
  38.         InputStreamReader isr = new InputStreamReader(httpconn.getInputStream(),charType);   
  39.         BufferedReader in = new BufferedReader(isr);   
  40.         String inputLine;   
  41.         if(!SavePath.endsWith("/")) {   
  42.         SavePath+="/";   
  43.         }   
  44.         FileOutputStream fout = new FileOutputStream(SavePath+sHtmlFile);   
  45.         while ((inputLine = in.readLine()) != null)   
  46.         {   
  47.         //System.out.println(inputLine);  
  48.         if(inputLine.toLowerCase().equals("<head>"))  
  49.         inputLine=inputLine+System.getProperty("line.separator")+baseHref;//加入basehref  
  50.         inputLine=inputLine+System.getProperty("line.separator");  //换行  
  51.         fout.write(inputLine.getBytes());   
  52.         }   
  53.         in.close();   
  54.         fout.close();   
  55.         }   
  56.   
  57.     }  
  58.     /** 
  59.      * @param args 
  60.      * @throws IOException  
  61.      */  
  62.     public static void main(String[] args) throws IOException {  
  63.         // TODO Auto-generated method stub  
  64.         convert2Html("http://www.51cto.com/art/200807/79080.htm","/","a4.htm");   
  65.     }  
  66.   
  67. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多