Enable logging in Apache Commons Net for FTP protocol

牧云@^-^@ 提交于 2019-11-26 17:25:27

问题


Apache Commons Net library does not seem to send anything to any "logger".

Can I somehow obtain a log file from an (FTP) session, for debugging purposes? For example raw FTP commands and responses from the server, like this:

220 Welcome
USER *******
331 Password required for ...
PASS *******
230 Logged on
TYPE I
200 Type set to I
QUIT
221 Goodbye

回答1:


All protocol implementations in Apache Commons Net, including FTPClient, derive from SocketClient, which has a method addProtocolCommandListener. You can pass it an implementation of ProtocolCommandListener to implement logging.

There's a ready-made implementation PrintCommandListener, which prints the protocol log to provided PrintStream.

With a code like this:

ftpClient.addProtocolCommandListener(
    new PrintCommandListener(
        new PrintWriter(new OutputStreamWriter(System.out, "UTF-8")), true));

..., you will get exactly the output that you have asked for.



来源:https://stackoverflow.com/questions/53426062/enable-logging-in-apache-commons-net-for-ftp-protocol

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