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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2576647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:51:59  来源:igfitidea点击:

Issue with org.apache.commons.net.ftp.FTPClient listFiles()

javaapache-commonsftp-client

提问by James P.

The listFiles()method of org.apache.commons.net.ftp.FTPClientworks fine with Filezilla server on 127.0.0.1 but returns nullon the root directory of public FTP servers such as belnet.be.

listFiles()方法org.apache.commons.net.ftp.FTPClient在 127.0.0.1null上的Filezilla 服务器上工作正常,但返回到公共 FTP 服务器的根目录,例如 belnet.be。

There is an identical question on the link below but enterRemotePassiveMode()doesn't seem to help. Apache Commons FTPClient.listFiles

下面的链接上有一个相同的问题,但enterRemotePassiveMode()似乎没有帮助。 Apache Commons FTPClient.listFiles

Could it be an issue with list parsing? If so, how can go about solving this?

这可能是列表解析的问题吗?如果是这样,如何解决这个问题?

Edit: Here's a directory cache dump:

编辑:这是一个目录缓存转储:

FileZilla Directory Cache Dump

FileZilla 目录缓存转储

Dumping 1 cached directories

转储 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" );
    }
}

Produces this output:

产生这个输出:

Connected to FTP
Login succesful
utils.ftp.FTPClientManager$Connection$LoggedInManager@156ee8e
null
No files found
Disconnection successful

Inside the wrapper (attempted using both listNames()and listFiles()):

在包装器内部(尝试同时使用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;
        }

As for FTPClient, it is handled as follows:

至于FTPClient,处理如下:

public class FTPUtils {

private FTPClientManager clientManager;

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

    return clientManager;
}

回答by skaffman

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.

每个 FTP 服务器都有不同的文件列表布局(是的,它不是 FTP 标准的一部分,它是愚蠢的),因此您必须使用正确的FTPFileEntryParser.

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

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.beis refusing my connections :(

这将预期的 FTP 服务器类型明确设置为 UNIX。尝试各种类型,看看效果如何。我试图找出自己,但ftp.belnet.be拒绝我的联系:(

回答by Stephen C

Have you tried checking that you can list the files using normal FTP client? (For some reason, I cannot even connect to the FTP port of "belnet.be".)

您是否尝试检查是否可以使用普通 FTP 客户端列出文件?(由于某种原因,我什至无法连接到“belnet.be”的 FTP 端口。)

EDIT

编辑

According to the javadoc for listFiles(), the parsing is done using the FTPFileEntryParserinstance provided by the parser factory. You probably need to figure out which of the parsers matches the FTP server's LIST output and configure the factory accordingly.

根据listFiles()的 javadoc ,解析是使用解析器工厂提供的FTPFileEntryParser实例完成的。您可能需要找出哪些解析器与 FTP 服务器的 LIST 输出匹配并相应地配置工厂。

回答by Raja

There was a parsing issue in earlier version of apache-Commons-net , the SYSTcommand which returns the server type when returns null( abruptly ) was not handled in parsingException . Try using the latest jarof apache-commons-net it may solve your problem.

在早期版本的 apache-Commons-net 中存在解析问题,当返回 null(突然)时返回服务器类型的SYST命令没有在 parsingException 中处理。尝试使用最新的 apache-commons-net jar可能会解决您的问题。