Chinaunix首页 | 论坛 | 博客
  • 博客访问: 26318630
  • 博文数量: 2065
  • 博客积分: 10377
  • 博客等级: 上将
  • 技术积分: 21525
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-04 17:50
文章分类

全部博文(2065)

文章存档

2012年(2)

2011年(19)

2010年(1160)

2009年(969)

2008年(153)

分类: Java

2009-12-04 10:06:02

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(""));
           
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

 
阅读(886) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

hkebao2010-03-26 20:02:41

以后整理全部的小工具类。及常用的代码集合方便直接使用