我与 FTP 服务器的连接出现问题。我有一个应用程序,它必须从文件中读取数据。我有在本地磁盘上搜索文件的代码,但我必须更改它,因为我的所有数据都在 FTP 服务器上。 此时我使用:
FileChannel fc = new FileInputStream("C:/Data/" + nameFile)
.getChannel();
其中 nameFile 是我的文件的名称。我创建 channel ,在其中从本地磁盘的文件加载数据。我可以更改可以在 FTP 服务器上搜索文件的代码吗?
最佳答案
我不完全理解你的帖子,但听起来你正在寻找一些代码来检查远程 FTP 服务器上是否存在文件,对吗?如果是这样,那么您将需要执行以下操作:
连接到服务器并进行身份验证。
导航到远程系统上的目录
执行远程系统的目录列表
检查目录列表中是否有任何文件与您正在查找的文件匹配。
我已经使用 Secure FTP Factory 成功完成了此操作,网址为 http://www.jscape.com/products/components/java/secure-ftp-factory/
示例代码
Ftp ftp = new Ftp(hostname,username,pass);
ftp.connect();
// get directory listing
Enumeration listing = ftp.getDirListing();
// enumerate thru listing
while(listing.hasMoreElements()) {
FtpFile file = (FtpFile)listing.nextElement();
// check to see if filename matches
System.out.println("Filename: " + file.getFilename());
}
关于Java 和 FTP 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8694802/