java 資料探勘 URL 內容流

iteye_8877發表於2009-08-06


package com.url;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
*
*/
public class JavaUrl2 {

public static boolean checkWebsite(String checkUrl) {
try {
URL url = new URL(checkUrl);
HttpURLConnection httpsConn = (HttpURLConnection) url.openConnection();
// 取得該連線的輸入流,以讀取響應內容
InputStream ins = httpsConn.getInputStream();
BufferedReader breader = new BufferedReader(new InputStreamReader(ins));
String info = breader.readLine();
long nowTime = new java.util.Date().getTime();
while (info != null) {
if (new java.util.Date().getTime() - nowTime > 10000)
return false;// 達到10秒就認為超時
info = breader.readLine();
if(info!=null && info.indexOf("<a href=/"mp3")!=-1){
info = info.substring(info.indexOf("<a href=/"mp3")+9,info.indexOf(".mp3/">")+4);
System.out.print(checkUrl.substring(0,checkUrl.lastIndexOf("/")+1));
System.out.println(info);
return true;
}
}
} catch (Exception e) {
System.out.println("The error is:" + e.getMessage());
e.printStackTrace();
return false;
}
return false;
}
/**
* create time:2009-8-5 下午02:33:45
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] strArr = {
"http://www.baidu.com",
};
for(int i=0;i<strArr.length;i++){
checkWebsite(strArr[i]);
}
}

}

相關文章