JAVA读取网页内容串行的问题
首先代码如下:
Spider类
public class Spider implements Runnable{
HttpURLConnection huc;
InputStream is;
BufferedReader reader;
String url;
public Spider(String str){
try {
url=str;
} catch (Exception e) {
e.printStackTrace();
}
try {
huc=(HttpURLConnection)new URL(url).openConnection();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
new Thread(this).start();
}
public void run() {
try {
huc.setRequestMethod("GET");
huc.setRequestProperty("user-agent","mozilla/4.0 (compatible; msie 6.0; windows 2000)");
} catch (ProtocolException e) {
e.printStackTrace();
}
try {
huc.setUseCaches(true);
huc.connect();
} catch (IOException e) {
e.printStackTrace();
}
try {
is=huc.getInputStream();
reader=new BufferedReader(new InputStreamReader(is,huc.getContentType().equals("text-html; charset=gb2312")?"gb2312":"UTF-8"));
String str;
System.out.flush();
while((str=reader.readLine())!=null){
System.out.println(str);
System.out.flush();
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
reader.close();
is.close();
huc.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
return;
}
}
结果在输出的结果与右键点浏览器查看源码的结果比较发现:
输出的结果如下:
<html>
<title>XXXXXXXXXXXXX</title>
</head>
<body >
<head>
原本应该在第二行的<head>跑到了后面,发生了串行的现象,不只为何,而且串行的位置也不固定,也就是说每次运行得到的结果都不一致,求高人解答
Spider类
public class Spider implements Runnable{
HttpURLConnection huc;
InputStream is;
BufferedReader reader;
String url;
public Spider(String str){
try {
url=str;
} catch (Exception e) {
e.printStackTrace();
}
try {
huc=(HttpURLConnection)new URL(url).openConnection();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
new Thread(this).start();
}
public void run() {
try {
huc.setRequestMethod("GET");
huc.setRequestProperty("user-agent","mozilla/4.0 (compatible; msie 6.0; windows 2000)");
} catch (ProtocolException e) {
e.printStackTrace();
}
try {
huc.setUseCaches(true);
huc.connect();
} catch (IOException e) {
e.printStackTrace();
}
try {
is=huc.getInputStream();
reader=new BufferedReader(new InputStreamReader(is,huc.getContentType().equals("text-html; charset=gb2312")?"gb2312":"UTF-8"));
String str;
System.out.flush();
while((str=reader.readLine())!=null){
System.out.println(str);
System.out.flush();
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
reader.close();
is.close();
huc.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
return;
}
}
结果在输出的结果与右键点浏览器查看源码的结果比较发现:
输出的结果如下:
<html>
<title>XXXXXXXXXXXXX</title>
</head>
<body >
<head>
原本应该在第二行的<head>跑到了后面,发生了串行的现象,不只为何,而且串行的位置也不固定,也就是说每次运行得到的结果都不一致,求高人解答
...全文
相关推荐
JAVA通过url获取网页内容
JAVA通过url获取网页内容
Java读取网页内容并下载图片的实例
主要介绍了Java读取网页内容并下载图片的实例的相关资料,希望通过本文能帮助到大家,让大家实现这样的功能,需要的朋友可以参考下
java 读取网页内容的实例详解
主要介绍了java 读取网页内容的实例详解的相关资料,希望通过本文能帮助到大家,让大家学习理解这部分内容,需要的朋友可以参考下
Java串行程序并行化执行
使用CGLIB实现串行程序的异步并行执行,博客地址:http://blog.csdn.net/yangzl2008/article/details/50489583
Java抓取网页内容三种方式
Java抓取网页内容三种方式


Web 开发
