【问题标题】:Apache FTPClient for Java show FTP Commands that were runApache FTPClient for Java 显示已运行的 FTP 命令
【发布时间】:2012-11-09 09:34:50
【问题描述】:

您好,我正在使用 Apache Commons FTP 客户端,我想显示 FTP 客户端使用的 FTP 命令,例如当我使用 changeWorkingDirectory 时,它应该显示它使用的 FTP 命令,例如:CODEOFCOMMAND CHD .....

或者当我上传文件时,它应该显示:CODEOFCOMMAND PUT ....

有没有可能做到这一点?

【问题讨论】:

    标签: java apache ftp client


    【解决方案1】:

    您可以在Apache Commons Net FAQ 中找到它:

    问:如何调试 FTP 应用程序?

    A:可以添加协议命令监听;例如:

    ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
    

    【讨论】:

    • 是的,我已经找到了,但我会接受答案,因为我也这样做了:)
    【解决方案2】:

    - 这是Object Oriented Programming最重要的方面之一,对实施者(在本例中为程序员)隐藏实施 .

    -当您使用Apache's commons library 来表示ftp 时,您可以使用该功能,因为实现是隐藏的。

    【讨论】:

    • 好点。但是对于调试,查看实现可能很有用。在 Hibernate 中,您还可以记录生成的 SQL 代码。
    【解决方案3】:

    这里给需要它的人:

    先做:

    redirectSystemStreams();
    ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
    

    但是因为我在 GUI 中使用 JTextArea 并且我需要那里的输出,所以我可以通过创建这些方法来重定向输出(将 txtLog 替换为您的 TextArea):

    private void updateTextArea(final String text) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    txtLog.append(text);
                }
            });
        }
    
        private void redirectSystemStreams() {
            OutputStream out = new OutputStream() {
                @Override
                public void write(int b) throws IOException {
                    updateTextArea(String.valueOf((char) b));
                }
    
                @Override
                public void write(byte[] b, int off, int len) throws IOException {
                    updateTextArea(new String(b, off, len));
                }
    
                @Override
                public void write(byte[] b) throws IOException {
                    write(b, 0, b.length);
                }
            };
    
            System.setOut(new PrintStream(out, true));
            System.setErr(new PrintStream(out, true));
        }
    

    【讨论】:

      猜你喜欢
      • 2020-09-24
      • 1970-01-01
      • 2022-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多