Java中的FTPClient类存在问题

Java中的FTPClient类存在问题

本文介绍了Java中的FTPClient类存在问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是org.apache.commons.net.ftp.FTPClient,看到的行为很好,令人费解。



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

  1)正确地检索并存储FIRST文件在列表中
2)对于循环的x次连续迭代,列表项的计算结果为NULL(x在连续尝试中变化
3)管理检索列表中的恰好1个文件
4)报告它在列表
5)中的确切1个文件为空无限期挂起,报告没有进一步的活动。

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;
尝试{
System.out.println(GETTING+ files.size()+files);
(FTPFile file:files){
fileAsString =;
InputStream inStream = ftp.retrieveFileStream(file.getName());

if(inStream == null){
System.out.println(FtpUtil.mergeXMLFiles()无法初始化inStream for file:+ file.getName());

continue; //这是我查看文件的一部分[1-任意数字(通常在20左右)],然后在[x + 1]后经过[x + 2]多一次成功。 ((c = inStream.read())!= -1){

fileAsString + = Character.valueOf((char)c);


}
inStream.close();


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


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

有没有人看过类似的东西?我是新来的FTPClient,我做错了吗?

根据时,该方法无法打开数据连接时返回 null ,在这种情况下,您应该检查回复代码(例如,,)查看失败的原因。另外,你可以通过调用并验证转移确实成功。

I'm using org.apache.commons.net.ftp.FTPClient and seeing behavior that is, well... perplexing.

The method beneath intends to go through an FTPFile list, read them in and then do something with the contents. That's all working. What is not (really) working is that the FTPClient object does the following...

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

has anyone seen anything like this? I'm new to FTPClient, am I doing something wrong with it?

解决方案

According to the API for FTPClient.retrieveFileStream(), the method returns null when it cannot open the data connection, in which case you should check the reply code (e.g. getReplyCode(), getReplyString(), getReplyStrings()) to see why it failed. Also, you are suppose to finalize file transfers by calling completePendingCommand() and verifying that the transfer was indeed successful.

这篇关于Java中的FTPClient类存在问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 10:44