import java.net.*;
          import java.io.*;

          public class urlconnection
          {
              public static void main(String[] args)
              {
                  StringBuffer document = new StringBuffer();
                  try 
                  {
                      URL url = new URL("http://www.163.com");
                      URLConnection conn = url.openConnection();
                      BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                      String line = null;
                      while ((line = reader.readLine()) != null)
                      document.append(line + " ");
                      reader.close();
                  }
                  catch (MalformedURLException e) 
                  {
                      e.printStackTrace(); 
                  }
                  catch (IOException e)
                  {
                      e.printStackTrace(); 
                  }
                  System.out.println(document.toString());
              }
          }
          ==========================================================
          方法二:
          ==========================================================
          import java.net.*;
          import java.io.*;

          public class useSourceViewer
          {
              public static void main (String[] args) 
              {
                  if (args.length > 0) 
                  {
                      try 
                      {

                          URL u = new URL(args[0]);
                          InputStream in = u.openStream( );

                          in = new BufferedInputStream(in);

                          Reader r = new InputStreamReader(in);
                          int c;
                          while ((c = r.read( )) != -1) 
                          {
                              System.out.print((char) c);
                          }
                          Object o = u.getContent( );
                          System.out.println("I got a " + o.getClass().getName( ));
                      }
                      catch (MalformedURLException e) 
                      {
                          System.err.println(args[0] + " is not a parseable URL");
                      }
                      catch (IOException e) 
                      {
                          System.err.println(e);
                      }
                  } 
              }
          }

          posted on 2008-07-13 20:38 aisoft 閱讀(10413) 評論(5)  編輯  收藏 所屬分類: J2EE開發(fā)技術(shù)

          FeedBack:
          # re: Java 讀取網(wǎng)頁Html文件[未登錄]
          2008-07-14 20:17 | javaread.com
          如果要獲取網(wǎng)頁文本內(nèi)容,還可以配合htmlparser  回復(fù)  更多評論
            
          # re: Java 讀取網(wǎng)頁Html文件
          2008-08-18 19:36 | qq630202528
          怎么讀取源文件中【 和 】之間的內(nèi)容?  回復(fù)  更多評論
            
          # re: Java 讀取網(wǎng)頁Html文件[未登錄]
          2009-04-13 21:29 | GreatGhoul
          還是第一種方法好用.  回復(fù)  更多評論
            
          # re: Java 讀取網(wǎng)頁Html文件
          2010-12-30 16:07 | 路人
          這個方法是會出錯的,((line = reader.readLine()) != null 是可能出錯,有可能網(wǎng)頁沒有讀取完成就遇見這樣的空行 本人親自測試遇見過  回復(fù)  更多評論
            
          # re: Java 讀取網(wǎng)頁Html文件
          2011-12-10 15:27 | XU
          比較喜歡第一種方法  回復(fù)  更多評論
            
          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          隨筆檔案(2)

          文章分類(12)

          文章檔案(12)

          收藏夾

          搜索

          •  

          最新評論

          主站蜘蛛池模板: 辽源市| 孙吴县| 桦甸市| 镇宁| 彝良县| 正定县| 广饶县| 乐至县| 中江县| 房产| 建昌县| 会昌县| 黄浦区| 衡阳市| 嘉鱼县| 江华| 兴海县| 怀柔区| 留坝县| 华宁县| 竹溪县| 宁安市| 财经| 公安县| 连云港市| 承德县| 永兴县| 阿巴嘎旗| 洛隆县| 邵武市| 府谷县| 遂昌县| 增城市| 阜新| 南靖县| 新田县| 开远市| 镇赉县| 磐石市| 兰溪市| 黑河市|