加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – org.apache.commons.net.ftp.FTPClient listFiles()的问

发布时间:2020-12-15 00:41:32 所属栏目:Java 来源:网络整理
导读:org.apache.commons.net.ftp.FTPClient的listFiles()方法适用于127.0.0.1上的Filezilla服务器,但在公共FTP服务器(如belnet.be)的根目录下返回null. 在下面的链接上有一个相同的问题,但enterRemotePassiveMode()似乎没有帮助. Apache Commons FTPClient.listF
org.apache.commons.net.ftp.FTPClient的listFiles()方法适用于127.0.0.1上的Filezilla服务器,但在公共FTP服务器(如belnet.be)的根目录下返回null.

在下面的链接上有一个相同的问题,但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

这是我使用我所做的包装器的代码(在包装器内测试产生相同的结果):

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()):

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,它按如下方式处理:

public class FTPUtils {

private FTPClientManager clientManager;

public FTPClientManager getFTPClientManager(){
    clientManager = new FTPClientManager();
    clientManager.setClient( new FTPClient() );

    return clientManager;
}

解决方法

每个FTP服务器都有不同的文件列表布局(是的,它不是FTP标准的一部分,它是愚蠢的),因此您必须使用正确的FTPFileEntryParser,通过手动指定它,或允许CommonsFTP自动检测它.

自动检测通常可以正常工作,但有时却没有,您必须明确指定它,例如

FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);

FTPClient client = FTPClient();
client.configure(conf);

这显式将预期的FTP服务器类型设置为UNIX.尝试各种类型,看看它如何.我试着找出自己,但ftp.belnet.be拒绝我的联系:(

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读