问题描述
org.apache.commons.net.ftp.FTPClient
的listFiles()
方法在127.0.0.1上的Filezilla服务器上可以正常工作,但在公共FTP服务器(如belnet.be)的根目录上返回null
.
The listFiles()
method of org.apache.commons.net.ftp.FTPClient
works fine with Filezilla server on 127.0.0.1 but returns null
on the root directory of public FTP servers such as belnet.be.
下面的链接上有一个相同的问题,但enterRemotePassiveMode()
似乎无济于事. Apache Commons FTPClient.listFiles
There is an identical question on the link below but enterRemotePassiveMode()
doesn't seem to help.Apache Commons FTPClient.listFiles
列表解析是否会引起问题?如果是这样,该如何解决呢?
Could it be an issue with list parsing? If so, how can go about solving this?
这是目录缓存转储:
FileZilla目录缓存转储
FileZilla Directory Cache Dump
转储1个缓存目录
Entry 1:
Path: /
Server: [email protected]:21, type: 4096
Directory contains 7 items:
lrw-r--r-- ftp ftp D 28 2009-06-17 debian
lrw-r--r-- ftp ftp D 31 2009-06-17 debian-cd
-rw-r--r-- ftp ftp 0 2010-03-04 13:30 keepalive.txt
drwxr-xr-x ftp ftp D 4096 2010-02-18 14:22 mirror
lrw-r--r-- ftp ftp D 6 2009-06-17 mirrors
drwxr-xr-x ftp ftp D 4096 2009-06-23 packages
lrw-r--r-- ftp ftp D 1 2009-06-17 pub
这是我使用包装器编写的代码(在包装器中进行测试会产生相同的结果):
Here's my code using a wrapper I've made (testing inside the wrapper produces the same results):
public static void main(String[] args) {
FTPUtils ftpUtils = new FTPUtils();
String ftpURL = "ftp.belnet.be";
Connection connection = ftpUtils.getFTPClientManager().getConnection( ftpURL );
if( connection == null ){
System.out.println( "Could not connect" );
return;
}
FTPClientManager manager = connection.getFptClientManager();
FTPClient client = manager.getClient();
try {
client.enterRemotePassiveMode();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if( connection != null ){
System.out.println( "Connected to FTP" );
connection.login("Anonymous", "Anonymous");
if( connection.isLoggedIn() ){
System.out.println( "Login successful" );
LoggedInManager loggedin = connection.getLoggedInManager();
System.out.println( loggedin );
String[] fileList = loggedin.getFileList();
System.out.println( loggedin.getWorkingDirectory() );
if( fileList == null || fileList.length == 0 )
System.out.println( "No files found" );
else{
for (String name : fileList ) {
System.out.println( name );
}
}
connection.disconnect();
if( connection.isDisconnected() )
System.out.println( "Disconnection successful" );
else
System.out.println( "Error disconnecting" );
}else{
System.out.println( "Unable to login" );
}
} else {
System.out.println( "Could not connect" );
}
}
产生此输出:
Connected to FTP
Login succesful
utils.ftp.FTPClientManager$Connection$LoggedInManager@156ee8e
null
No files found
Disconnection successful
包装器内部(同时使用listNames()
和listFiles()
进行尝试):
Inside the wrapper (attempted using both listNames()
and listFiles()
):
public String[] getFileList() {
String[] fileList = null;
FTPFile[] ftpFiles = null;
try {
ftpFiles = client.listFiles();
//fileList = client.listNames();
//System.out.println( client.listNames() );
} catch (IOException e) {
return null;
}
fileList = new String[ ftpFiles.length ];
for( int i = 0; i < ftpFiles.length; i++ ){
fileList[ i ] = ftpFiles[ i ].getName();
}
return fileList;
}
对于FTPClient,它的处理方式如下:
As for FTPClient, it is handled as follows:
public class FTPUtils {
private FTPClientManager clientManager;
public FTPClientManager getFTPClientManager(){
clientManager = new FTPClientManager();
clientManager.setClient( new FTPClient() );
return clientManager;
}
推荐答案
每个FTP服务器具有不同的文件列表布局(是的,它不是FTP标准的一部分,它是愚蠢的),因此您必须使用正确的FTPFileEntryParser
,或者通过手动指定它,或者允许CommonsFTP自动检测它.
Each FTP server has a different file list layout (yes, it's not part of the FTP standard, it's dumb), and so you have to use the correct FTPFileEntryParser
, either by specifying it manually, or allowing CommonsFTP to auto-detect it.
自动检测通常可以正常工作,但有时不能正常工作,您必须明确指定它,例如
Auto-detection usually works fine, but sometimes it doesn't, and you have to specify it explicitly, e.g.
FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
FTPClient client = FTPClient();
client.configure(conf);
这将预期的FTP服务器类型显式设置为UNIX.尝试各种类型,看看情况如何.我试图找出自己的名字,但ftp.belnet.be
拒绝了我的联系:(
This explicitly sets the expected FTP server type to UNIX. Try the various types, see how it goes. I tried finding out myself, but ftp.belnet.be
is refusing my connections :(
这篇关于org.apache.commons.net.ftp.FTPClient listFiles()出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!