当前位置: 首页 > 面试题库 >

org.apache.commons.net.ftp.FTPClient listFiles()出现问题

仲绍晖
2023-03-14
问题内容

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

下面的链接上有一个相同的问题,但enterRemotePassiveMode()似乎无济于事。 Apache CommonsFTPClient.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拒绝我的联系:(



 类似资料:
  • 问题内容: 我在package.json中添加了proxy,效果很好,但是在npm run build之后,CORS问题再次浮出水面,有人知道在React中npm run build之后如何处理CORS问题。 我试图使用各种方法在axios请求中添加标头。但是,我未能在axios请求中添加’Access-Control-Allow- Origin’:’*’。我的代码如下: package.json

  • 我的代码(来自例如https://pypi.org/project/selenium/) 从selenium导入webdriver 浏览器=WebDriver.Firefox() 错误跟踪(最近一次调用):文件“C:\users\saltlake\appdata\local\programs\python\python38\lib\site-packages\selenium\webdriver\

  • 我可以通过本地J控制台访问Mbean,但无法从远程主机访问MBEANS。 我的配置: 远程进程:服务:jmx:远程处理-jmx://10.32.222.111:4447 我将用户添加到管理和应用领域 测试=2b1be81e1da41d4ea647bd82fc8c2bc9 但当我尝试连接它说:连接失败:重试 当我使用远程进程为:10.32.222.111:4447在服务器上提示警告: 16: 29:

  • 问题内容: 现有2个版本的java系统,1.6版本无法卸载(dll无法运行) 求大神拯救!小白一枚想学习安卓系统 问题答案: 把你想用的那个版本设置环境变量,设置JAVA_HOME=”你安装的JDK路径”

  • 问题内容: 我似乎无法弄清楚为什么我会继续得到一个。 代码如下。 问题答案: 是保留关键字,您需要将其包装在方括号中: 我建议重命名该列,以避免每次引用时都使用方括号。

  • 本文向大家介绍解决wx.onMenuShareTimeline出现的问题,包括了解决wx.onMenuShareTimeline出现的问题的使用技巧和注意事项,需要的朋友参考一下 wx.onMenuShareTimeline使用注意事项 我在开发测试过程中,发现使用wx.onMenuShareTimeline无效果,没有显示我定义的图片、title和链接,经过调试发现原因如下: 1.图片大小要大于