【问题标题】:Unable to download files from FTP server无法从 FTP 服务器下载文件
【发布时间】:2015-03-21 07:47:30
【问题描述】:

我创建了从 FTP 服务器下载文件的 Java 函数。它在我的本地机器上运行良好。但我需要在 linux 服务器下运行它(意味着另一个主机和端口)。而且函数报错

The collection, array, map, iterator, or enumeration portion of a for statement cannot be null

导致在一行代码中:

for(String f : ftpNames) {
    ftpclient.retrieveFile(f, os); // os is OutputStream
}

所以它看不到文件... 我加了

ftpclient.enterRemotePassiveMode();

ftpclient.getPassiveHost() 返回227 Entering Passive Mode (x,x,x,x,204,15)

尝试通过 shell 列出和下载它们 - 它有效。

我应该如何修改我的代码来解决问题?谢谢。

UPD。我从我试图从中获取文件的 FTP 服务器获取日志,并且有这样的字符串:

 425 Cannot open data connection

完整代码:

static boolean ftpFilesDownload(String ip, int port, String login, String passwd, String ftpdir, String localdir) throws IOException {
        Boolean result = false;

        FTPClient client = new FTPClient();
        String separator = File.separator;


        try {                             
            client.connect(ip, port);    
            System.out.println(client.getReplyString());
            client.login(login, passwd);
            System.out.println(client.getReplyString());

            client.setControlKeepAliveTimeout(1000*60*5);
            client.setControlKeepAliveReplyTimeout(1000*60*5);
            client.setFileType(FTP.BINARY_FILE_TYPE);
            System.out.println("client setFileType success");    

            client.changeWorkingDirectory(ftpdir);
            System.out.println(client.getReplyString());
            client.printWorkingDirectory();
            System.out.println("directory changed");                                 

            FTPFile[] ftpFiles = client.listFiles();
            System.out.println(ftpFiles);                          

            String[] ftpNames = client.listNames();
            System.out.println("the files are " + Arrays.toString(ftpNames)); // so null here...
              for(String f : ftpNames) {                
            String localfile = localdir + f;
            OutputStream os = new FileOutputStream(localfile);
            try  {
                result = client.retrieveFile(f, os);
                System.out.println("DOWNLOADING STARTED);
                System.out.println(client.getReplyString());
                client.noop();
            }
            catch(Exception e) {
                System.out.println(e);                    
                result = false;
            }
            finally {
                if(os != null)
                    os.close();
                }
            }
            client.logout();
            System.out.println(client.getReplyString());
        }
        catch(Exception e)
        {
            System.out.println(e);
            result = false;
        }
        finally
        {
            try
            {
                client.disconnect();
            }
            catch(Exception e)
            {
                System.out.println(e);
                result = false;
            }
        }
        return result;
    }

【问题讨论】:

  • 你是如何设置ftpNames的值的?这就是它所抱怨的。
  • String[] ftpNames = ftpclient.listNames();
  • 我似乎无法通过 android 应用程序链接到它,但在 Google 上搜索“java ftpclient listfiles null”会发现一个看起来很有用的 * 答案,标题为“java - org. apache.commons.net.ftp.FTPClient 列表文件”。它建议您需要指定一个特定的条目解析器,因为没有单一的标准。
  • 谢谢...但还是不行

标签: java shell ftp


【解决方案1】:

尝试使用 ftpclient.enterLocalActiveMode();

【讨论】:

    【解决方案2】:

    正如错误消息所解释的,您正在尝试迭代 null 对象。您应该检查这一点(或确保使用空的 Iterable)

    如果这是一个执行(错误)状态,我会明确检查并抛出某种运行时异常,例如:

    if (ftpNames == null) {
        throw new IllegalArgumentException("Cannot use a null set of FTP servers");
    }
    
    for (String f : ftpNames) {
        ftpclient.retrieveFile(f, os); // os is OutputStream
    }
    

    或者,您可以尝试不使用 FTP 服务器继续,但似乎没有意义。

    【讨论】:

    • 我可以看到ftpNames 为空。我不知道是什么原因造成的
    • 请您添加有关 ftpclient 是什么的详细信息(即您使用的是哪个 ftp 客户端库)?
    • 您还没有发布导入,所以仍然不清楚您使用的是什么客户端库,所以我无法查找 javadoc。
    最近更新 更多