【问题标题】:Problem with FTPClient class in javajava中FTPClient类的问题
【发布时间】:2010-10-05 04:37:54
【问题描述】:

我正在使用 org.apache.commons.net.ftp.FTPClient 并看到行为,嗯...令人困惑。

下面的方法打算通过一个 FTPFile 列表,读入它们,然后对内容做一些事情。这一切都在工作。 (真正)不起作用的是 FTPClient 对象执行以下操作...

1) Properly retrieves and stores the FIRST file in the list  
2) List item evaluates to NULL for x number of successive iterations of the loop (x varies on successive attempts  
3) manages to retrieve exactly 1 more file in the list  
4) reports that it is null for exactly 1 more file in the list  
5) hangs indefinitely, reporting no further activity.

public static String mergeXMLFiles(List<FTPFile> files, String rootElementNodeName, FTPClient ftp){
        String ret = null;
        String fileAsString   = null; 
        //InputStream inStream;
        int c;

        if(files == null || rootElementNodeName == null)
            return null;
        try {
            System.out.println("GETTING " + files.size() + " files");
            for (FTPFile file : files) {
                fileAsString = "";
                InputStream inStream = ftp.retrieveFileStream(file.getName());

                if(inStream == null){
                    System.out.println("FtpUtil.mergeXMLFiles() couldn't initialize inStream for file:" + file.getName());

                    continue;//THIS IS THE PART THAT I SEE FOR files [1 - arbitrary number (usually around 20)] and then 1 more time for [x + 2] after [x + 1] passes successfully.
                }
                while((c = inStream.read()) != -1){

                    fileAsString += Character.valueOf((char)c);
                }
                inStream.close();


                System.out.println("FILE:" + file.getName() + "\n" + fileAsString);
            }


        } catch (Exception e) {
            System.out.println("FtpUtil.mergeXMLFiles() failed:" + e);
        }
        return ret;
    }

有人见过这样的吗?我是 FTPClient 的新手,我做错了什么吗?

【问题讨论】:

  • 您能否编辑您的帖子,而不是使用列表项的代码视图,而是使用列表项提供的简码?让它更具可读性:)

标签: java ftp apache-commons


【解决方案1】:

根据FTPClient.retrieveFileStream()的API,该方法在无法打开数据连接时返回null,此时应查看回复码(如getReplyCode()getReplyString()getReplyStrings())看看为什么失败。此外,您应该通过调用completePendingCommand() 并验证传输确实成功来完成文件传输。

【讨论】:

  • completePendingComand()!!!!!!你是一位绅士和一位学者,我正在检查回复代码(在前面的方法中,我做了所有的连接垃圾),一切都很好。我忽略了指定 completePendingCommand 重要性的文档。非常感谢。
  • 感谢 Zach Scrivena 的回答!被这个问题困扰了很长时间:)
【解决方案2】:

当我在“retrieve”命令之后添加时它工作正常:

        int response = client.getReply();
        if (response != FTPReply.CLOSING_DATA_CONNECTION){
            //TODO 
        }

【讨论】:

    猜你喜欢
    • 2011-08-30
    • 2013-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-01
    • 2015-01-07
    • 2011-08-21
    相关资源
    最近更新 更多