從網(wǎng)頁源文件中得到鏈接

字號:

這是一個從網(wǎng)頁源文件中得到鏈接的實現(xiàn)代碼,和大家分享一下:
    import Java.net.*;
    import Java.io.*;
    import Java.util.*;
    class ScanPage{
     private static String strPage;
     private String strUrl;
     private String fileName;
     public void setURL(String strUrl){
     this.strUrl=strUrl;
     }
     //從地址中得到文件名
     public void setFileName(){
     int i;
     char ch;
     i=strUrl.length();
     ch=strUrl.charAt(--i);
     while(ch!=’/’ && ch>0)
     ch=strUrl.charAt(--i);
     fileName=strUrl.substring(i);
     }
     //下載網(wǎng)頁
     public void downFile()throws IOException{
     URL url =new URL(strUrl);
     InputStream is =url.openStream();
     OutputStream os =new FileOutputStream(fileName);
     byte[] buffer =new byte[512];
     int len;
     while((len =is.read(buffer))!=-1)
     os.write(buffer,0,len);
     is.close();
     os.close();
     }
     //讀文件
     public void readFile() throws IOException {
     StringBuffer sb = new StringBuffer();
     BufferedReader in =new BufferedReader(new FileReader(fileName));
     String s;
     while((s = in.readLine()) != null) {
     sb.append(s);
     sb.append("\n");
     }
     in.close();
     strPage=sb.toString();
     }
     public String getTitle(){
     return "";
     }