小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

JAVA用URL下載網(wǎng)頁為靜態(tài)頁面...

 知識儲備庫_wucl 2011-06-22

JAVA用URL下載網(wǎng)頁為靜態(tài)頁面

關(guān)鍵字: java, url, 靜態(tài)頁面

1.可以作為項目的動態(tài)頁面生成靜態(tài)頁面的功能

2.可以當(dāng)做JSP小偷

3.可以當(dāng)做全文網(wǎng)摘

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 是所有類的超類,它代表應(yīng)用程序和 URL 之間的通信鏈接,通過在 URL 上調(diào)用 openConnection 方法創(chuàng)建連接對象   
  24.         urlconn.addRequestProperty("Accept-Language""zh-cn");  
  25.         urlconn.connect(); //使用 connect 方法建立到遠(yuǎn)程對象的實際連接   
  26.         HttpURLConnection httpconn=(HttpURLConnection)urlconn; //每個 HttpURLConnection 實例都可用于生成單個請求,但是其他實例可以透明地共享連接到 HTTP 服務(wù)器的基礎(chǔ)網(wǎng)絡(luò)   
  27.   
  28.         HttpResult=httpconn.getResponseCode(); //getResponseCode可以從 HTTP 響應(yīng)消息獲取狀態(tài)碼   
  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. }  

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多