从网页源文件中得到链接
这是一个从网页源文件中获取链接的实现代码。下面分享给大家:
导入Java . net . *;
导入Java . io . *;
导入Java . util . *;
class ScanPage{
私有静态字符串strPage
私有字符串strUrl
私有字符串文件名;
public void setURL(String strUrl){
this . strUrl = strUrl;
}
/从地址中获取文件名
public void set filename(){
int I;
char ch;
I = strurl . length();
ch = strurl . charat(-I);
while(ch!= '/' & & ch > 0)
ch = strurl . charat(-I);
fileName = strurl . substring(I);
}
/下载网页
Public Void down file()ThrowsioException {
URL URL = new URL(strurl);
InputStream is = URL . openstream();
output stream OS = new file output stream(fileName);
byte[]buffer =新字节[512];
int len;
while((len = is . read(buffer))!=-1)
os.write(buffer,0,len);
is . close();
OS . close();
}
/Read file
public void readfile()ThrowsioException {
string buffer sb = newstring buffer();
buffered reader in = new buffered reader(new file reader(fileName));
字符串s;
while((s = in.readLine())!= null){
sb . append(s);
sb . append(" \ n ");
}
in . close();
strPage = sb . tostring();
}
public String getTitle(){
return " ";
}
0条评论