公共网络 FTP 问题

发布于 2024-09-27 04:00:41 字数 3996 浏览 1 评论 0原文

我遇到一个问题:我的 FTP 连接似乎正确且未收到错误,但文件未放置在 ftp 服务器上。

我正在使用 commons-net-ftp。

代码:

        int retCode = 0;

    FTPClient client = new FTPClient();
    client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));

    InputStream input = null;
    try
    {
        int replyCode;

        client.connect(pHostName);

        replyCode = client.getReplyCode();
        if (!FTPReply.isPositiveCompletion(replyCode))
        {
            client.disconnect();
            logInfo("ftpFile() - FTP Server refused connection");
            retCode = 1;
        }
        else
        {
            if(client.login(pUserName, pPassword))
            {
                //default is FTP.ASCII_FILE_TYPE
                if(this.isBinaryTransfer())
                {
                    client.setFileType(FTP.BINARY_FILE_TYPE);   
                }

                // Use passive mode as default because most of us are
                // behind firewalls these days.
                client.enterLocalPassiveMode();

                input = new FileInputStream(pLocalFileName);

                if(this.isRemoveRemoteFile())
                {
                    client.deleteFile(pRemoteFileName);
                    client.getReply();
                    this.logReplyStringInfo(client.getReplyStrings(), "Removing Remote File");
                }

                if(this.isOverwriteFile())
                {
                    replyCode = client.sendCommand("-O");
                    this.logReplyStringInfo(client.getReplyStrings(), "Overwrite File");
                }

                if(!client.storeFile(pRemoteFileName, input))
                {
                    logError("ftpFile() - Not able to store the file on the server" ); 
                    retCode = 6;
                }

                input.close();
                client.logout();
                client.disconnect();
            }
            else
            {
                client.logout();
                client.disconnect();
                retCode = 3;
            }
        }
    }
    catch (FileNotFoundException fileNotFoundException)
    {
        logError("ftpFile(String, String, String, String, String)", fileNotFoundException); //$NON-NLS-1$

        fileNotFoundException.printStackTrace();
        retCode = 5;
    }
    catch (FTPConnectionClosedException e)
    {
        logError("ftpFile(String, String, String, String, String)", e); //$NON-NLS-1$

        retCode = 4;
        e.printStackTrace();
    }
    catch (IOException e)
    {
        logError("ftpFile(String, String, String, String, String)", e); //$NON-NLS-1$

        e.printStackTrace();
        e.printStackTrace();
        retCode = 2;
    }
    finally
    {
        if (client.isConnected())
        {
            try
            {
                if(null != input)
                {
                    input.close();
                }
                client.disconnect();
            }
            catch (IOException f)
            {
                logWarning("ftpFile(String, String, String, String, String) - exception ignored", f); //$NON-NLS-1$
            }
        }
    }

日志文件跟踪:

2010-10-12 10:57:53,527 INFO [STDOUT] 230 登录成功
2010-10-12 10:57:53,527 信息 [标准输出] PASV
2010-10-12 10:57:53,576 INFO [STDOUT] 227 进入被动模式 (216,27,89,17,10,231)
2010-10-12 10:57:53,624 信息 [标准输出] STOR SharperImageFeed2.txt
2010-10-12 10:57:53,681 INFO [STDOUT] 150“/SharperImageFeed2.txt”文件准备好以 ASCII 模式接收
2010-10-12 10:57:54,337 INFO [STDOUT] 226 传输成功完成。
2010-10-12 10:57:54,337 信息 [标准输出] 退出
2010-10-12 10:57:54,384 INFO [STDOUT] 221 Windows FTP 服务器(WFTPD,德克萨斯帝国软件公司)说再见

对于问题是什么有什么建议吗?有什么可以运行的测试吗?

我可以使用 FileZilla 进行 ftp 和上传文件。


进行进一步的测试,当我从本地开发环境运行时,我能够执行成功的 FTP put - 这是 Windows (Eclipse/JBoss) 但是,当 FTP 从生产服务器 (Linux/JBoss) 运行时,跟踪表明它成功,但 FTP 服务器上未放置任何内容。

I am facing an issue where it appears that my FTP connection is correct and no errors are received, but the file is not placed on the ftp server.

I am using commons-net-ftp.

Code:

        int retCode = 0;

    FTPClient client = new FTPClient();
    client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));

    InputStream input = null;
    try
    {
        int replyCode;

        client.connect(pHostName);

        replyCode = client.getReplyCode();
        if (!FTPReply.isPositiveCompletion(replyCode))
        {
            client.disconnect();
            logInfo("ftpFile() - FTP Server refused connection");
            retCode = 1;
        }
        else
        {
            if(client.login(pUserName, pPassword))
            {
                //default is FTP.ASCII_FILE_TYPE
                if(this.isBinaryTransfer())
                {
                    client.setFileType(FTP.BINARY_FILE_TYPE);   
                }

                // Use passive mode as default because most of us are
                // behind firewalls these days.
                client.enterLocalPassiveMode();

                input = new FileInputStream(pLocalFileName);

                if(this.isRemoveRemoteFile())
                {
                    client.deleteFile(pRemoteFileName);
                    client.getReply();
                    this.logReplyStringInfo(client.getReplyStrings(), "Removing Remote File");
                }

                if(this.isOverwriteFile())
                {
                    replyCode = client.sendCommand("-O");
                    this.logReplyStringInfo(client.getReplyStrings(), "Overwrite File");
                }

                if(!client.storeFile(pRemoteFileName, input))
                {
                    logError("ftpFile() - Not able to store the file on the server" ); 
                    retCode = 6;
                }

                input.close();
                client.logout();
                client.disconnect();
            }
            else
            {
                client.logout();
                client.disconnect();
                retCode = 3;
            }
        }
    }
    catch (FileNotFoundException fileNotFoundException)
    {
        logError("ftpFile(String, String, String, String, String)", fileNotFoundException); //$NON-NLS-1$

        fileNotFoundException.printStackTrace();
        retCode = 5;
    }
    catch (FTPConnectionClosedException e)
    {
        logError("ftpFile(String, String, String, String, String)", e); //$NON-NLS-1$

        retCode = 4;
        e.printStackTrace();
    }
    catch (IOException e)
    {
        logError("ftpFile(String, String, String, String, String)", e); //$NON-NLS-1$

        e.printStackTrace();
        e.printStackTrace();
        retCode = 2;
    }
    finally
    {
        if (client.isConnected())
        {
            try
            {
                if(null != input)
                {
                    input.close();
                }
                client.disconnect();
            }
            catch (IOException f)
            {
                logWarning("ftpFile(String, String, String, String, String) - exception ignored", f); //$NON-NLS-1$
            }
        }
    }

Log File Trace:

2010-10-12 10:57:53,527 INFO [STDOUT] 230 Logged in successfully
2010-10-12 10:57:53,527 INFO [STDOUT] PASV
2010-10-12 10:57:53,576 INFO [STDOUT] 227 Entering Passive Mode (216,27,89,17,10,231)
2010-10-12 10:57:53,624 INFO [STDOUT] STOR SharperImageFeed2.txt
2010-10-12 10:57:53,681 INFO [STDOUT] 150 "/SharperImageFeed2.txt" file ready to receive in ASCII mode
2010-10-12 10:57:54,337 INFO [STDOUT] 226 Transfer finished successfully.
2010-10-12 10:57:54,337 INFO [STDOUT] QUIT
2010-10-12 10:57:54,384 INFO [STDOUT] 221 Windows FTP Server (WFTPD, by Texas Imperial Software) says goodbye

Any suggestions as to what the issue is? Any test that can be run?

I can ftp and upload a file using FileZilla.


Doing further testing I am able to execute a successful FTP put when I run from my local dev env - which is Windows (Eclipse/JBoss) But, when the FTP is run from the production servier (Linux/JBoss) The trace indicates that it is successful but nothing is placed on the FTP Server.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无所谓啦 2024-10-04 04:00:41

Commons FTP 的一些命令要求您调用completePendingCommand:

 if(!client.completePendingCommand()) {
     client.logout();
     client.disconnect();
     System.err.println("File transfer failed.");        
     System.exit(1);
 }

尝试在storeFile 之后添加以上内容。更多信息在这里:

http ://commons.apache.org/net/api/org/apache/commons/net/ftp/FTPClient.html#completePendingCommand()

A few of the commands with Commons FTP require you to call completePendingCommand:

 if(!client.completePendingCommand()) {
     client.logout();
     client.disconnect();
     System.err.println("File transfer failed.");        
     System.exit(1);
 }

Try adding the above after storeFile. More info here:

http://commons.apache.org/net/api/org/apache/commons/net/ftp/FTPClient.html#completePendingCommand()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文