alexj
alexj

Reputation: 149

Apache FTPClient for Java show FTP Commands that were run

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 ?

Upvotes: 1

Views: 5813

Answers (3)

alexj
alexj

Reputation: 149

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

Upvotes: 0

jeroen_de_schutter
jeroen_de_schutter

Reputation: 1883

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

Upvotes: 6

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33544

- 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.

Upvotes: 0

Related Questions