【问题标题】:How to get response from uploading file to sftp?如何从上传文件到 sftp 获得响应?
【发布时间】:2019-08-16 12:48:27
【问题描述】:

如果文件成功上传到 sftp,我如何获得响应?这是我的代码

@Bean
public SessionFactory<LsEntry> axisSftpSessionFactory() {
    DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
    factory.setHost(axisSftpProperties.getSftpHost());
    factory.setPort(axisSftpProperties.getSftpPort());
    factory.setUser(axisSftpProperties.getSftpUser());
    factory.setPassword(axisSftpProperties.getSftpPassword());
    factory.setAllowUnknownKeys(true);
    return new CachingSessionFactory<>(factory);
}

/**
 * Handler message handler.
 *
 * @return the message handler
 */
@Bean
@ServiceActivator(inputChannel = TO_SFTP_CHANNEL)
public MessageHandler handler() {
    SftpMessageHandler handler = new SftpMessageHandler(axisSftpSessionFactory());
    handler.setRemoteDirectoryExpression(new LiteralExpression(axisSftpProperties.getSftpRemoteDirectory()));
    handler.setFileNameGenerator(message -> (String) message.getHeaders().get(FILENAME));
    return handler;
}


@Component
@MessagingGateway
public interface UploadGateway {


    @Gateway(requestChannel = TO_SFTP_CHANNEL)
    String upload(@Header(FILENAME) String filename, @Payload byte[] bytes);
}

这里的想法是,如果文件没有成功上传到 sftp 以便能够重试,则捕获任何错误。

如果我使用 SftpOutboundGateway 如何设置远程目录路径? SftpOutboundGateway 网关 = new SftpOutboundGateway(sessionFactory(), "put", "payload");

【问题讨论】:

  • 当您编辑问题时,我们不会收到通知。每当您进行编辑时,您还应该评论您已这样做,以便我收到通知。

标签: spring-integration spring-integration-sftp


【解决方案1】:

查看文档:

put 操作产生的消息负载是一个字符串,其中包含传输后服务器上文件的完整路径。

因为你有一个 void return,它被丢弃了。

所以...

String upload(File file);

编辑

使用网关时,第三个构造函数参数是文件名的表达式;远程文件模板中提供了远程目录...

@Bean
public SftpRemoteFileTemplate template() {
    SftpRemoteFileTemplate template = new SftpRemoteFileTemplate(sessionFactory());
    template.setRemoteDirectoryExpression(new LiteralExpression("foo/test"));
    return template;
}

new SftpOutboundGateway(template(). "put", "headers['file_name']")

System.out.println(gate.upload("foo.txt", "foo".getBytes()));

foo/test/foo.txt

【讨论】:

  • 是的(但以后不要将代码放入 cmets - 它不会很好地呈现 - 最好编辑问题并在此处添加您已完成的评论)。
  • 我无法返回路径。我做错了什么?你有什么例子吗?
  • 显示网关下游的配置(一直到SFTP网关)。您需要使用带有 PUT 命令的 SFTP 出站网关来获得响应 - 如果您使用的是出站通道适配器 (SftpMessageHandler),那只能是单向的。
  • 查看我的答案以了解如何使用网关。
猜你喜欢
  • 2014-07-26
  • 1970-01-01
  • 1970-01-01
  • 2021-05-24
  • 1970-01-01
  • 2012-12-19
  • 2020-01-01
  • 1970-01-01
  • 2013-05-07
相关资源
最近更新 更多