Jam*_* P. 7 java ftp-client apache-commons
该listFiles()
方法org.apache.commons.net.ftp.FTPClient
在127.0.0.1 null
上与Filezilla服务器一起正常工作,但返回到公共FTP服务器的根目录,如belnet.be.
在下面的链接上有一个相同的问题,但enterRemotePassiveMode()
似乎没有帮助.
Apache Commons FTPClient.listFiles
这可能是列表解析的问题吗?如果是这样,怎么能解决这个问题呢?
编辑:这是一个目录缓存转储:
FileZilla目录缓存转储
转储1个缓存目录
Entry 1:
Path: /
Server: anonymous@ftp.belnet.be: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
Run Code Online (Sandbox Code Playgroud)
这是我使用我所做的包装器的代码(在包装器内测试会产生相同的结果):
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" );
}
}
Run Code Online (Sandbox Code Playgroud)
生成此输出:
Connected to FTP
Login succesful
utils.ftp.FTPClientManager$Connection$LoggedInManager@156ee8e
null
No files found
Disconnection successful
Run Code Online (Sandbox Code Playgroud)
在包装器内部(尝试使用listNames()
和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;
}
Run Code Online (Sandbox Code Playgroud)
至于FTPClient,它按如下方式处理:
public class FTPUtils {
private FTPClientManager clientManager;
public FTPClientManager getFTPClientManager(){
clientManager = new FTPClientManager();
clientManager.setClient( new FTPClient() );
return clientManager;
}
Run Code Online (Sandbox Code Playgroud)
每个FTP服务器都有不同的文件列表布局(是的,它不是FTP标准的一部分,它是愚蠢的),所以你必须使用正确的FTPFileEntryParser
,通过手动指定,或允许CommonsFTP自动检测它.
自动检测通常可以正常工作,但有时却没有,您必须明确指定它,例如
FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
FTPClient client = FTPClient();
client.configure(conf);
Run Code Online (Sandbox Code Playgroud)
这显式将预期的FTP服务器类型设置为UNIX.尝试各种类型,看看它是怎么回事.我试着找出自己,但ftp.belnet.be
拒绝我的关系:(
归档时间: |
|
查看次数: |
15792 次 |
最近记录: |