原文地址
http://webservices.ctocio.com.cn/java/128/9331628.shtml
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;
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();
}
/**
* 抓取指定URL地址的网页内容值并返回内容
*/
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 + "rn");
}
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();
}
}
}
本文介绍如何使用Java语言解析URL并获取网页内容,包括URL的打开、数据读取及关闭等操作。
2万+

被折叠的 条评论
为什么被折叠?



