我有以下问题。我按照文档here 并配置了 SFTPInboundAdapter。 ssh客户端开始下载文件时出现错误:
org.springframework.messaging.MessagingException: Failure occurred while copying 'remote-dir/MyFile.csv' from the remote to the local directory
根本原因是通用的,并且服务器的响应似乎很糟糕:
Caused by: SFTP error (SSH_FX_FAILURE): Failure
在本地目录中我可以看到文件已创建:
remote-dir/MyFile.csv.writing
,但是下载没有完成,文件大小为零。
由于错误是通用的,我启用了
org.apache.sshd.*
的调试级别。在引发异常之前,这是日志:
...
2024-05-06T16:19:21.445+02:00 DEBUG 21336 --- [ scheduling-1] o.a.sshd.common.io.nio2.Nio2Session : writeBuffer(Nio2Session[local=/my_ip, remote=sftp.server]) writing 80 bytes
2024-05-06T16:19:21.454+02:00 DEBUG 21336 --- []-nio2-thread-2] o.a.s.client.session.ClientSessionImpl : doHandleMessage(ClientSessionImpl[[email protected]]) process #190 SSH_MSG_CHANNEL_DATA
2024-05-06T16:19:21.455+02:00 DEBUG 21336 --- []-nio2-thread-2] o.a.sshd.common.channel.LocalWindow : Consume LocalWindow[client](SftpChannelSubsystem[id=0, recipient=0]-ClientSessionImpl[[email protected]][sftp]) by 2491 down to 2083722
2024-05-06T16:19:21.455+02:00 DEBUG 21336 --- []-nio2-thread-2] i.DefaultSftpClient$SftpChannelSubsystem : handleData(SftpChannelSubsystem[id=0, recipient=0]-ClientSessionImpl[[email protected]][sftp]) SSH_MSG_CHANNEL_DATA len=2491
2024-05-06T16:19:21.455+02:00 DEBUG 21336 --- [ scheduling-1] o.a.sshd.common.channel.RemoteWindow : Consume RemoteWindow[client](SftpChannelSubsystem[id=0, recipient=0]-ClientSessionImpl[[email protected]][sftp]) by 16 down to 2094190
2024-05-06T16:19:21.455+02:00 DEBUG 21336 --- [ scheduling-1] o.a.s.client.session.ClientSessionImpl : encode(ClientSessionImpl[[email protected]]) packet #198 sending command=94[SSH_MSG_CHANNEL_DATA] len=25
2024-05-06T16:19:21.456+02:00 DEBUG 21336 --- [ scheduling-1] o.a.sshd.common.io.nio2.Nio2Session : writeBuffer(Nio2Session[local=/my_ip, remote=sftp.server]) writing 80 bytes
2024-05-06T16:19:21.464+02:00 DEBUG 21336 --- []-nio2-thread-4] o.a.s.client.session.ClientSessionImpl : doHandleMessage(ClientSessionImpl[[email protected]]) process #191 SSH_MSG_CHANNEL_DATA
2024-05-06T16:19:21.464+02:00 DEBUG 21336 --- []-nio2-thread-4] o.a.sshd.common.channel.LocalWindow : Consume LocalWindow[client](SftpChannelSubsystem[id=0, recipient=0]-ClientSessionImpl[[email protected]][sftp]) by 32 down to 2083690
2024-05-06T16:19:21.464+02:00 DEBUG 21336 --- []-nio2-thread-4] i.DefaultSftpClient$SftpChannelSubsystem : handleData(SftpChannelSubsystem[id=0, recipient=0]-ClientSessionImpl[[email protected]][sftp]) SSH_MSG_CHANNEL_DATA len=32
2024-05-06T16:19:21.467+02:00 DEBUG 21336 --- [ scheduling-1] o.a.s.s.c.impl.SftpDirEntryIterator : close(/remote_directory) handle=/remote_directory: 687831
2024-05-06T16:19:21.468+02:00 DEBUG 21336 --- [ scheduling-1] o.a.sshd.common.channel.RemoteWindow : Consume RemoteWindow[client](SftpChannelSubsystem[id=0, recipient=0]-ClientSessionImpl[[email protected]][sftp]) by 16 down to 2094174
2024-05-06T16:19:21.468+02:00 DEBUG 21336 --- [ scheduling-1] o.a.s.client.session.ClientSessionImpl : encode(ClientSessionImpl[[email protected]]) packet #199 sending command=94[SSH_MSG_CHANNEL_DATA] len=25
2024-05-06T16:19:21.468+02:00 DEBUG 21336 --- [ scheduling-1] o.a.sshd.common.io.nio2.Nio2Session : writeBuffer(Nio2Session[local=/my_ip, remote=sftp.server]) writing 80 bytes
2024-05-06T16:19:21.480+02:00 DEBUG 21336 --- []-nio2-thread-6] o.a.s.client.session.ClientSessionImpl : doHandleMessage(ClientSessionImpl[[email protected]]) process #192 SSH_MSG_CHANNEL_DATA
2024-05-06T16:19:21.480+02:00 DEBUG 21336 --- []-nio2-thread-6] o.a.sshd.common.channel.LocalWindow : Consume LocalWindow[client](SftpChannelSubsystem[id=0, recipient=0]-ClientSessionImpl[[email protected]][sftp]) by 28 down to 2083662
2024-05-06T16:19:21.480+02:00 DEBUG 21336 --- []-nio2-thread-6] i.DefaultSftpClient$SftpChannelSubsystem : handleData(SftpChannelSubsystem[id=0, recipient=0]-ClientSessionImpl[[email protected]][sftp]) SSH_MSG_CHANNEL_DATA len=28
2024-05-06T16:19:21.491+02:00 DEBUG 21336 --- [ scheduling-1] o.a.sshd.common.channel.RemoteWindow : Consume RemoteWindow[client](SftpChannelSubsystem[id=0, recipient=0]-ClientSessionImpl[[email protected]][sftp]) by 109 down to 2094065
2024-05-06T16:19:21.491+02:00 DEBUG 21336 --- [ scheduling-1] o.a.s.client.session.ClientSessionImpl : encode(ClientSessionImpl[[email protected]]) packet #200 sending command=94[SSH_MSG_CHANNEL_DATA] len=118
2024-05-06T16:19:21.491+02:00 DEBUG 21336 --- [ scheduling-1] o.a.sshd.common.io.nio2.Nio2Session : writeBuffer(Nio2Session[local=/my_ip, remote=sftp.server]) writing 176 bytes
2024-05-06T16:19:21.882+02:00 DEBUG 21336 --- []-nio2-thread-8] o.a.s.client.session.ClientSessionImpl : doHandleMessage(ClientSessionImpl[[email protected]]) process #193 SSH_MSG_CHANNEL_DATA
2024-05-06T16:19:21.884+02:00 DEBUG 21336 --- []-nio2-thread-8] o.a.sshd.common.channel.LocalWindow : Consume LocalWindow[client](SftpChannelSubsystem[id=0, recipient=0]-ClientSessionImpl[[email protected]][sftp]) by 28 down to 2083634
2024-05-06T16:19:21.884+02:00 DEBUG 21336 --- []-nio2-thread-8] i.DefaultSftpClient$SftpChannelSubsystem : handleData(SftpChannelSubsystem[id=0, recipient=0]-ClientSessionImpl[[email protected]][sftp]) SSH_MSG_CHANNEL_DATA len=28
引起我注意的是
SftpDirEntryIterator
的日志,它似乎正在关闭通道。我怀疑这是错误的原因,即使我不确定,因为我没有看到任何ClosedChannelException
!
我确信客户端出了问题。我可以使用 FileZilla 下载该文件。
更新:
我尝试使用
SftpRemoteFileTemplate
:
sftpTemplate.get("remote-dir/MyFile.csv") {
inputStream -> inputStream.use {
localDir.mkdir()
FileUtils.copyInputStreamToFile(inputStream, localDir)
}}
在这种情况下我也得到
SFTP error (SSH_FX_FAILURE): Failure
。这次没有记录任何close
消息。因此,关于 SftpDirEntryIterator
的日志的注释是不值得的。该问题仅在下载过程中发生。我尝试使用 Sftp 模板列出远程目录中的所有文件。此操作完成且没有错误。
错误是由于远程目录路径中缺少
/
:
sftpInboundFileSynchronizer.setRemoteDirectory("remote_folder") // Wrong
sftpInboundFileSynchronizer.setRemoteDirectory("/remote_folder")
不幸的是,我只是通过“更改并尝试”解决了问题,因为 sftp 服务器返回的错误太笼统,加上客户端的行为不一致。虽然即使没有
/
它也可以列出目录,但这在下载文件时是必要的...