Read a .txt file from an anonymous FTP page?

风流意气都作罢 提交于 2019-12-23 07:06:05

问题


My goal is to to convert a .txt file on an FTP page to a simple String for easy manipulation.

The specific .txt file is here: ftp://ftp.nasdaqtrader.com/SymbolDirectory/nasdaqlisted.txt. It is an anonymous FTP page, so when I use my computer's browser, there's no need for a username or password.

I've tried incorporating different codes and tips from the following sources:

  • Reading Text File From Server on Android
  • http://examples.javacodegeeks.com/core-java/apache/commons/net-commons/download-file-from-ftp-server/
  • How to read a text file via FTP?
  • Reading txt file from an ftp server and returning it from AsyncTask
  • unable to read file from ftp in android?
  • Howto do a simple ftp get file on Android
  • http://www.javaworld.com/article/2073325/java-app-dev/java-ftp-client-libraries-reviewed.html
  • http://developer.android.com/reference/java/net/URLConnection.html

None of what I've tried above helped. I'm not quite sure what I'm doing wrong.

To be clear, all I want to do is to get the plain text from the posted .txt file. I have no interest in downloading said file onto my device's memory.

If you could provide me with a step-by-step explanation on how to do this, I'd be very thankful.


回答1:


Ok. I've got it. For those who are in the same boat, the step-by-step answer is below:

  1. A lot of problems other users were encountering could be solved by having permissions for internet turned on in the manifest, but mine was a little more complicated. Turns out, the main trick is to not include ftp:// in the address in Java.* Also, when you are entering an FTP site, make sure you enter via the root page, so my original page of ftp://ftp.nasdaqtrader.com/SymbolDirectory/nasdaqlisted.txt becomes: ftp.nasdaqtrader.com.

  2. Make sure to download and include the right Apache Commons library in your project (here: http://commons.apache.org/proper/commons-net/download_net.cgi).

  3. Connect to the root page:

    FTPClient ftpClient = new FTPClient();

    ftpClient.connect("ftp.nasdaqtrader.com");

  4. Login anonymously, in this case, both username and password are "anonymous". This might be the case with many FTP pages, but I can't be sure:

    ftpClient.login("anonymous", "anonymous");

  5. Then, go to the correct directory (be careful about including/excluding the slashes):

    ftpClient.changeWorkingDirectory("/SymbolDirectory");

  6. Finally! You can now get the InputStream from the posted text file:

    InputStream is = new BufferedInputStream(ftpClient.retrieveFileStream("nasdaqlisted.txt"));

  7. Then, convert the InputStream into String and manipulate as needed. There are many ways to do this. One of which can be found here: How can I convert InputStream data to String in Android SOAP Webservices

*Source: Android FTP connection Failed




回答2:


If you get "425 Unable to build data connection: Connection timed out" error, then after connecting to ftp server, I would recommend you to set the local mode to passive mode by the following statement.

ftpClient.enterLocalPassiveMode();


来源:https://stackoverflow.com/questions/28068161/read-a-txt-file-from-an-anonymous-ftp-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!