为 FTP 协议启用 Apache Commons Net 中的日志记录

Mar*_*ryl 3 java ftp logging apache-commons-net

Apache Commons Net 库似乎没有向任何“记录器”发送任何内容。

我可以以某种方式从(FTP)会话中获取日志文件以进行调试吗?例如来自服务器的原始 FTP 命令和响应,如下所示:

220 Welcome
USER *******
331 Password required for ...
PASS *******
230 Logged on
TYPE I
200 Type set to I
QUIT
221 Goodbye
Run Code Online (Sandbox Code Playgroud)

Mar*_*ryl 5

Apache Commons Net 中的所有协议实现,包括FTPClient、派生自SocketClient、具有方法addProtocolCommandListener. 您可以传递一个实现ProtocolCommandListener来实现日志记录。

有一个现成的实现PrintCommandListener,它将协议日志打印到提供的PrintStream

使用这样的代码:

ftpClient.addProtocolCommandListener(
    new PrintCommandListener(
        new PrintWriter(new OutputStreamWriter(System.out, "UTF-8")), true));
Run Code Online (Sandbox Code Playgroud)

...,您将获得您要求的准确输出。