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();
}
}
}
阅读(923) | 评论(1) | 转发(0) |