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 閱讀(10410) 評論(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ù)  更多評論
            
          <2011年12月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          隨筆檔案(2)

          文章分類(12)

          文章檔案(12)

          收藏夾

          搜索

          •  

          最新評論

          主站蜘蛛池模板: 皋兰县| 锦州市| 阿尔山市| 呈贡县| 平阴县| 辛集市| 抚远县| 石城县| 深圳市| 华坪县| 宜川县| 贞丰县| 忻城县| 定日县| 哈巴河县| 本溪市| 台南市| 塘沽区| 重庆市| 泸水县| 大英县| 长葛市| 沂源县| 延庆县| 聂拉木县| 固镇县| 阿勒泰市| 西畴县| 长乐市| 雅安市| 河北省| 夏津县| 和田市| 东阳市| 竹溪县| 始兴县| 砚山县| 宁陕县| 定日县| 仁怀市| 会同县|