1

我正在尝试实现 2 个文件的 SFTP 文件上传,该文件必须按特定顺序进行 - 首先是一个 pdf 文件,然后在成功上传一个包含有关 pdf 的元信息的文本文件之后。

我遵循了线程中的建议,但无法使其正常工作。

我的 Spring Boot 配置:

@Bean
public SessionFactory<LsEntry> sftpSessionFactory() {
    final DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
    final Properties jschProps = new Properties();
    jschProps.put("StrictHostKeyChecking", "no");
    jschProps.put("PreferredAuthentications", "publickey,password");
    factory.setSessionConfig(jschProps);

    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
@BridgeTo
public MessageChannel toSftpChannel() {
    return new PublishSubscribeChannel();
}


@Bean
@ServiceActivator(inputChannel = "toSftpChannel")
@Order(0)
public MessageHandler handler() {
    final SftpMessageHandler handler = new SftpMessageHandler(sftpSessionFactory());
    handler.setRemoteDirectoryExpression(new LiteralExpression(sftpRemoteDirectory));
    handler.setFileNameGenerator(message -> {
        if (message.getPayload() instanceof byte[]) {
            return (String) message.getHeaders().get("filename");
        } else {
            throw new IllegalArgumentException("File expected as payload.");
        }
    });
    return handler;
}

@ServiceActivator(inputChannel = "toSftpChannel")
@Order(1)
public String transferComplete(@Payload byte[] file, @Header("filename") String filename) {
    return "The SFTP transfer complete for file: " + filename;
}

@MessagingGateway
public interface UploadGateway {

    @Gateway(requestChannel = "toSftpChannel")
    String upload(@Payload byte[] file, @Header("filename") String filename);

}

我的测试用例:

final String pdfStatus = uploadGateway.upload(content, documentName);
log.info("Upload of {} completed, {}.", documentName, pdfStatus);

从网关上传调用的返回,我希望得到确认上传的字符串,例如“文件的 SFTP 传输完成:...”,但我得到了上传文件的返回内容,以字节 [] 表示:

Upload of 123456789.1.pdf completed, 37,80,68,70,45,49,46,54,13,37,-30,-29,-49,-45,13,10,50,55,53,32,48,32,111,98,106,13,60,60,47,76,105,110,101,97,114,105,122,101,100,32,49,47,76,32,50,53,52,55,49,48,47,79,32,50,55,55,47,69,32,49,49,49,55,55,55,47,78,32,49,47,84,32,50,53,52,51,53,57,47,72,32,91,32,49,49,57,55,32,53,51,55,93,62,62,13,101,110,100,111,98,106,13,32,32,32,32,32,32,32,32,32,32,32,32,13,10,52,55,49,32,48,32,111,98,106,13,60,60,47,68,101,99,111,100,101,80,97,114,109,115,60,60,47,67,111,108,117,109,110,115,32,53,47,80,114,101,100,105,99,116,111,114,32,49,50,62,62,47,70,105,108,116,101,114,47,70,108,97,116,101,68,101,99,111,100,101,47,73,68,91,60,57,66,53,49,56,54,69,70,53,66,56,66,49,50,52,49,65,56,50,49,55,50,54,56,65,65,54,52,65,57,70,54,62,60,68,52,50,68,51,55,54,53,54,65,67,48,55,54,52,65,65,53,52,66,52,57,51,50,56,52,56,68,66 etc.

我错过了什么?

4

1 回答 1

0

我认为@Order(0) 不能与@Bean.

要修复它,您应该在该 bean 定义中执行此操作:

final SftpMessageHandler handler = new SftpMessageHandler(sftpSessionFactory());
handler.setOrder(0);

有关详细信息,请参阅参考手册

在消费者@Bean定义上使用这些注解时,如果 bean 定义返回适当的MessageHandler(取决于注解类型),则必须在定义本身上设置诸如outputChannel等属性。requiresReplyMessageHandler @Bean

换句话说:如果你可以使用 setter,你就必须这样做。我们不处理这种情况下的注释,因为不能保证什么应该优先。所以,为了避免这种混淆,我们只给你留下了二传手的选择。

更新

我看到了你的问题,它就在这里:

@Bean
@BridgeTo
public MessageChannel toSftpChannel() {
    return new PublishSubscribeChannel();
}

日志证实了这一点:

Adding {bridge:dmsSftpConfig.toSftpChannel.bridgeTo} as a subscriber to the 'toSftpChannel' channel
Channel 'org.springframework.context.support.GenericApplicationContext@b3d0f7.toSftpChannel' has 3 subscriber(s).
started dmsSftpConfig.toSftpChannel.bridgeTo

所以,你真的还有一个订阅者,toSftpChannel它是一个BridgeHandler带有replyChannel标题的输出。并且默认订单就像private volatile int order = Ordered.LOWEST_PRECEDENCE;这个成为第一个订阅者一样,而这个byte[]正是因为它是一个payload请求而返回给您。

您需要决定是否真的需要这样的桥梁。虽然没有解决方法@Order......

于 2018-02-26T15:25:51.043 回答