7

このlistFiles()方法はorg.apache.commons.net.ftp.FTPClient、127.0.0.1のFilezillaサーバーで正常に機能しますがnull、belnet.beなどのパブリックFTPサーバーのルートディレクトリに戻ります。

以下のリンクにも同じ質問がありますが、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;
}
4

3 に答える 3

8

各 FTP サーバーには異なるファイル リスト レイアウトがあります (はい、FTP 標準の一部ではなく、馬鹿げています)、FTPFileEntryParser手動で指定するか、CommonsFTP が自動検出できるようにして、正しい を使用する必要があります。

通常、自動検出は正常に機能しますが、そうでない場合もあり、明示的に指定する必要があります。

FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);

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

これにより、予想される FTP サーバーの種類が明示的に UNIX に設定されます。いろいろなタイプを試してみて、様子を見てください。私は自分自身を見つけようとしましたftp.belnet.beが、私のつながりを拒否しています:(

于 2010-04-05T09:23:22.430 に答える
2

通常の FTP クライアントを使用してファイルを一覧表示できることを確認してみましたか? (なぜか「belnet.be」のFTPポートにも接続できません。)

編集

listFiles()の javadoc によると、解析はパーサー ファクトリによって提供されるFTPFileEntryParserインスタンスを使用して行われます。おそらく、どのパーサーが FTP サーバーの LIST 出力と一致するかを把握し、それに応じてファクトリを構成する必要があります。

于 2010-04-05T02:44:50.103 に答える
1

以前のバージョンの apache-Commons-net には解析の問題がありました。null を返したときにサーバー タイプを返すSYSTコマンドが(突然) parsingException で処理されませんでした。apache-commons-net の最新の jarを使用してみてください。問題が解決する場合があります。

于 2013-04-10T10:45:25.990 に答える