睫晋姬

Java里面的java.net.url类

  package sys.utils;

  import java.io.BufferedInputStream;

  import java.io.BufferedReader;

  import java.io.IOException;

  import java.io.InputStream;

  import java.io.InputStreamReader;

  import java.io.Reader;

  import java.net.URL;

  import java.net.URLConnection;

  import sun.misc.UCDecoder;

  public class NetUtils {

  /**

  * @param urls

  * @exception IOException

  * @see: 读一个URL的数据直到下一个字节为空。其中InputStream 类里面的read() 表示读取下一个字节的

  * */

  public void urlparse(String urls) throws IOException {

  URL url = new URL(urls);

  URLConnection uc = url.openConnection();

  InputStream in = uc.getInputStream();

  int c;

  while ((c = in.read()) != -1) {

  System.out.println(c);

  }

  in.close();

  }

  /**

  * @param URL

  * @see 读取URL所指定的网页内容

  * */

  public void urlparses(String urls) throws IOException {

  URL url = new URL(urls);

  Reader reader = new InputStreamReader(new BufferedInputStream(url.openStream()));

  int c;

  while ((c = reader.read()) != -1) {

  System.out.println((char)c);

  }

  reader.close();

  }

  /**

  * @param URL

  * @return String

  * @throws IOException

  * @ses  抓取指定URL地址的网页内容值并返回内容

  * @exception IOException

  * */

  public String  parseContent(String urls) throws IOException {

  URL url = new URL(urls);

  BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));

  String s = "";

  StringBuffer sb = new StringBuffer("");

  while ((s = br.readLine())!=null) {

  sb.append(s+"\r\n");

  }

  br.close();

  return sb.toString();

  }

  public static void main(String[] args) {

  try {

  System.out.println(new NetUtils().parseContent("http://www.sina.cn"));

  } catch (IOException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  }

  }

  }

posted on 2009-12-05 15:53 睫晋姬 阅读(203) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: