【问题标题】:Not able to upload file to sftp server using spring integration无法使用 spring 集成将文件上传到 sftp 服务器
【发布时间】:2020-02-03 20:13:23
【问题描述】:

嗨,我在尝试使用 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);

}

【问题讨论】:

  • 禁止可能意味着您可能没有写入权限?
  • 听起来你没有权限写入远程文件。
  • 3: Forbidden 中的3 表示the authenticated user does not have sufficient permissions to perform the operation。代码 (3) 和单词 Forbidden 来自服务器 - 查看服务器日志。
  • 您很可能使用了错误的路径。尝试绝对路径。
  • 只是想一想,我最近不得不创建一个 SFTP 连接器,我发现 Spring-integration 很麻烦,所以我直接使用了底层的 JSch 库(他们使用)并且事情进展得更加顺利。我知道这并不能解决您的具体问题,但从长远来看,它可能会为您节省时间。在我的情况下,我还希望灵活地动态创建连接,这更容易直接使用库。我确信一切都可以通过 spring-integration 直接完成,但在我的情况下不值得开销。

标签: java spring spring-boot spring-integration sftp


【解决方案1】:

只需检查您是否提供了有效的文件路径作为目标。它应该是它写入上游文件内容的确切文件路径

springframework.integration.sftp.session.SftpSession.write(InputStream inputStream, String destination)

【讨论】:

    猜你喜欢
    • 2016-01-28
    • 2018-07-11
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    • 2020-04-18
    • 1970-01-01
    • 2022-07-27
    • 2015-05-20
    相关资源
    最近更新 更多