从匿名FTP页面读取.txt文件? [英] Read a .txt file from an anonymous FTP page?

查看:129
本文介绍了从匿名FTP页面读取.txt文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是将FTP页面上的.txt文件转换为简单的String,以便于操作.

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

特定的.txt文件位于此处: ftp://ftp.nasdaqtrader.com/SymbolDirectory /nasdaqlisted.txt .这是一个匿名FTP页面,因此,当我使用计算机的浏览器时,不需要用户名或密码.

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.

要清楚,我要做的就是从发布的.txt文件中获取纯文本.我没有兴趣将上述文件下载到设备的内存中.

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.

推荐答案

好.我懂了.对于同一条船上的乘客,分步解答如下:

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

  1. 其他用户遇到的许多问题可以通过在清单中打开Internet权限来解决,但我的问题要复杂一些.事实证明,主要技巧是在Java中的地址中不要包含ftp://.*另外,当您进入FTP站点时,请确保通过 root 页面输入,因此 ftp://ftp.nasdaqtrader.com/SymbolDirectory/nasdaqlisted.txt 变为:ftp.nasdaqtrader.com.

  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.

确保下载正确的Apache Commons库并将其包含在您的项目中(此处:

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).

连接到根页面:

FTPClient ftpClient = new FTPClient();

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

匿名登录,在这种情况下,用户名和密码均为匿名".许多FTP页面可能都是这种情况,但是我不确定:

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");

然后,转到正确的目录(请注意包括/不包括斜杠):

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

ftpClient.changeWorkingDirectory("/SymbolDirectory");

最后!现在,您可以从发布的文本文件中获取InputStream:

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

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

然后,将InputStream转换为String并根据需要进行操作.有很多方法可以做到这一点.可以在这里找到其中之一:如何在Android SOAP Web服务中将InputStream数据转换为String

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

*来源: Android FTP连接失败

这篇关于从匿名FTP页面读取.txt文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆