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 閱讀(10411) 評論(5)  編輯  收藏 所屬分類: J2EE開發技術

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

          隨筆檔案(2)

          文章分類(12)

          文章檔案(12)

          收藏夾

          搜索

          •  

          最新評論

          主站蜘蛛池模板: 绥芬河市| 常宁市| 湘潭县| 桑植县| 米脂县| 河北区| 肇源县| 观塘区| 汾西县| 井研县| 鄱阳县| 莒南县| 项城市| 汕尾市| 峨眉山市| 西和县| 江西省| 漯河市| 陆丰市| 青田县| 青冈县| 鸡泽县| 靖边县| 连云港市| 长子县| 南充市| 集安市| 贵南县| 巢湖市| 彭阳县| 郎溪县| 定安县| 康定县| 鸡西市| 泰宁县| 旌德县| 稷山县| 巴彦淖尔市| 元阳县| 澄城县| 榕江县|