2013-07-08 78 views
1

我无法通过ftp从此html文件中获取html文本。我使用美丽的汤通过http/https阅读html文件,但由于某些原因,我无法从ftp下载/读取。请帮忙!如何通过ftp url下载/阅读html文件?

这是网址。 a link

这是我的代码到目前为止。

BufferedReader reader = null; 
String total = ""; 
String line; 
ur = "ftp://ftp.legis.state.tx.us/bills/832/billtext/html/house_resolutions/HR00001_HR00099/HR00014I.htm" 
try { 
    URL url = new URL(ur); 
    URLConnection urlc = url.openConnection(); 
    InputStream is = urlc.getInputStream(); // To download 
    reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); 
     while ((line = reader.readLine()) != null) 
      total += reader.readLine(); 

} finally { 
    if (reader != null) 
     try { reader.close(); 
     } catch (IOException logOrIgnore) {} 
} 
+1

你可以发布错误的堆栈跟踪? –

回答

0

首先想到这是关系到一个错误的道路分辨率discussed here但这并没有帮助。

我不知道这里究竟发生了什么问题,但是我只能在这个ftp-server和MacOS Java 1.6.0_33-b03-424上重现这个错误。我无法用Java 1.7.0_25重现它。所以也许你会检查一下Java更新。

或者你可以使用commons FTPClient检索文件:

FTPClient client = new FTPClient(); 
client.connect("ftp.legis.state.tx.us"); 
client.enterLocalPassiveMode(); 
client.login("anonymous", ""); 
client.changeWorkingDirectory("bills/832/billtext/html/house_resolutions/HR00001_HR00099"); 
InputStream is = client.retrieveFileStream("HR00014I.htm"); 
1

此代码为我工作,爪哇1.7.0_25。请注意,您正在存储每两行中的一行,在条件和while循环的主体中都调用reader.readLine()

public static void main(String[] args) throws MalformedURLException, IOException { 
    BufferedReader reader = null; 
    String total = ""; 
    String line; 
    String ur = "ftp://ftp.legis.state.tx.us/bills/832/billtext/html/house_resolutions/HR00001_HR00099/HR00014I.htm"; 
    try { 
     URL url = new URL(ur); 
     URLConnection urlc = url.openConnection(); 
     InputStream is = urlc.getInputStream(); // To download 
     reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); 
     while ((line = reader.readLine()) != null) { 
      total += line; 
     } 
    } finally { 
     if (reader != null) { 
      try { 
       reader.close(); 
      } catch (IOException logOrIgnore) { 
      } 
     } 
    } 
}