Apache FTPClient for Java show FTP Commands that were run

风流意气都作罢 提交于 2019-12-14 03:18:54

问题


Hello I am using Apache Commons FTP Client and I want to show the FTP Commands that the FTP Client uses so like when I use changeWorkingDirectory it should show me the FTP Command that it used like: CODEOFCOMMAND CHD .....

or when I upload a File it should show me: CODEOFCOMMAND PUT ....

Is there any possibility to do this ?


回答1:


You can find it in the Apache Commons Net FAQ :

Q: How do I debug FTP applications?

A: You can add a protocol command listener; for example:

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



回答2:


- Its one of the most important aspect of Object Oriented Programming to hide the implementation from the implementer (In this case the Programmer).

- And as you are using Apache's commons library for the ftp, you are permitted to use the functionality, were as the implementation is hidden.




回答3:


Here for the people that also need it:

First do:

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

but because I am using a JTextArea in a GUI and I need the output there I hat to redirect the output I did it by creating these Methods (Replace txtLog with your 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));
    }


来源:https://stackoverflow.com/questions/13305387/apache-ftpclient-for-java-show-ftp-commands-that-were-run

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!