java - 无法使用 spring 集成将文件上传到 sftp 服务器
问题描述
嗨,我在尝试使用 spring 集成将 csv 文件上传到 SFTP 服务器时遇到以下异常,无法弄清楚发生了什么错误,但在服务器上我可以看到类似10132_1234.csv.writing的文件,但写入操作从不完成,而通过winscp我可以毫无问题地复制,请帮助解决这个问题,
Caused by: org.springframework.messaging.MessagingException: Failed
to write to 'file-location/10132_1234.csv.writing' while uploading
the file; nested exception is
org.springframework.core.NestedIOException: failed to write file;
nested exception is 3: Forbidden at
org.springframework.integration.file.remote.RemoteFileTemplate.sendFileToRemoteDirectory(RemoteFileTemplate.java:560)
at
org.springframework.integration.file.remote.RemoteFileTemplate.doSend(RemoteFileTemplate.java:337)
... 88 common frames omitted`enter code here`
Caused by: org.springframework.core.NestedIOException: failed to
write file; nested exception is 3: Forbidden at
org.springframework.integration.sftp.session.SftpSession.write(SftpSession.java:177)
at
org.springframework.integration.file.remote.session.CachingSessionFactory$CachedSession.write(CachingSessionFactory.java:235)
at
org.springframework.integration.file.remote.RemoteFileTemplate.doSend(RemoteFileTemplate.java:568)
at
org.springframework.integration.file.remote.RemoteFileTemplate.sendFileToRemoteDirectory(RemoteFileTemplate.java:557)
... 89 common frames omitted
Caused by: com.jcraft.jsch.SftpException: Forbidden at
com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2873)
at com.jcraft.jsch.ChannelSftp._put(ChannelSftp.java:594) at
com.jcraft.jsch.ChannelSftp.put(ChannelSftp.java:540) at
com.jcraft.jsch.ChannelSftp.put(ChannelSftp.java:492) at
org.springframework.integration.sftp.session.SftpSession.write(SftpSession.java:174)
... 92 common frames omitted
这是我的上传代码:
@Bean
public SessionFactory<LsEntry> sftpSessionFactory() {
DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
factory.setHost(sftpHost);
factory.setPort(sftpPort);
factory.setUser(sftpUser);
if (sftpPrivateKey != null) {
factory.setPrivateKey(sftpPrivateKey);
factory.setPrivateKeyPassphrase(sftpPrivateKeyPassphrase);
} else {
factory.setPassword(sftpPasword);
}
factory.setAllowUnknownKeys(true);
return new CachingSessionFactory<>(factory);
}
@Bean
@ServiceActivator(inputChannel = "toSftpChannel")
public MessageHandler handler() {
logger.info("Entering into MessageHandler for uploading the file to SFTP server");
SftpMessageHandler handler = new SftpMessageHandler(sftpSessionFactory());
handler.setRemoteDirectoryExpression(new LiteralExpression(sftpRemoteDirectory));
handler.setFileNameGenerator(message -> {
if (message.getPayload() instanceof File) {
return ((File) message.getPayload()).getName();
} else {
throw new IllegalArgumentException("File expected as payload.");
}
});
return handler;
}
@MessagingGateway
public interface UploadGateway {
@Gateway(requestChannel = "toSftpChannel")
void upload(File file);
}
解决方案
只需检查您是否提供了有效的文件路径作为目标。它应该是它写入上游文件内容的确切文件路径
springframework.integration.sftp.session.SftpSession.write(InputStream inputStream, String destination)
推荐阅读
- java - 如何在 OSGi 框架停止之前销毁所有 OSGi 蓝图组件?
- laravel - 全屏日历Vue+Laravel - 如何处理?
- .net - 如何从 .NET 中的 SQL Server 表中读取简单的用户定义类型?
- android - 使用来自 json 文件 kotlin 的对象数组访问对象
- python - 我将如何使用 NIST 的 json 漏洞源来检查我的计算机是否存在漏洞?
- c# - 如何使用 c#linq 从列表中删除逗号分隔的相同值
- validation - 来自 DTO 的数据注释未在视图中验证
- c - 从双向链表段错误中删除两个项目
- asp.net-mvc - 将文件下载到特定目录
- asp.net-mvc - 模型的结构与表不同,由于类型不同,更新数据库有问题