try{
Calendar cldr = Calendar.getInstance();
askUrl+="t="+(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(cldr.getTime()));
URL destURL = new URL(askUrl);
InputStream in = destURL.openStream();
BufferedReader dataReader = new BufferedReader(new InputStreamReader(in,"utf-8"));
String fileline=null;
String reTxt="";
while((fileline = dataReader.readLine()) != null){
if(fileline.indexOf("keyword=")>0){//该行包含开奖号
//根据读取值给reTxt赋值
break;
}
}
dataReader.close();
destURL=null;in=null;dataReader=null;cldr=null;
return reTxt;
}catch(IOException e){
log.write(askUrl+"读取异常!");
return "IO-ERR";
}
1.上面抓取网页内容的new URL() openStream()方式的使用是否正确,比如dataReader.close();是否将连接关闭完毕,是否有遗漏的地方,比如连接超时等。(我的这段代码是在servlet中的,每隔15秒抓一次,运行一段时间后就停止工作了)
2.有没有更保险的抓数据的方法。在url连接,读取,关闭连接方面更有优势更稳定的
Calendar cldr = Calendar.getInstance();
askUrl+="t="+(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(cldr.getTime()));
URL destURL = new URL(askUrl);
InputStream in = destURL.openStream();
BufferedReader dataReader = new BufferedReader(new InputStreamReader(in,"utf-8"));
String fileline=null;
String reTxt="";
while((fileline = dataReader.readLine()) != null){
if(fileline.indexOf("keyword=")>0){//该行包含开奖号
//根据读取值给reTxt赋值
break;
}
}
dataReader.close();
destURL=null;in=null;dataReader=null;cldr=null;
return reTxt;
}catch(IOException e){
log.write(askUrl+"读取异常!");
return "IO-ERR";
}
1.上面抓取网页内容的new URL() openStream()方式的使用是否正确,比如dataReader.close();是否将连接关闭完毕,是否有遗漏的地方,比如连接超时等。(我的这段代码是在servlet中的,每隔15秒抓一次,运行一段时间后就停止工作了)
2.有没有更保险的抓数据的方法。在url连接,读取,关闭连接方面更有优势更稳定的
URL url = new URL("www.baidu.com");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStream in = conn.getInputStream();
Reader rd = new InputStreamReader(in);
int c;
while ((c = rd.read()) != -1)
System.out.print((char) c);
in.close();