首页 > 解决方案 > 用于 Mput 操作的 Spring-Integration SFTP 网关

问题描述

我正在尝试使用 SFTP 出站网关上传多个文件。我的 Java 代码是这样的:

final DirectChannel reqWriteChannel = (DirectChannel) context.getBean("toWriteChannel");
final PollableChannel repWriteChannel = (PollableChannel) context.getBean("fromWriteChannel");

reqWriteChannel.send(MessageBuilder.withPayload(listOfFiles).build());
Message<?> input = repReadChannel.receive(1000);

System.out.println(input);
System.out.println(input.getPayload().toString());

这是 XML 配置:

<int:channel id="fromWriteChannel"><int:queue /></int:channel>
<int:channel id="toWriteChannel" />

<int-sftp:outbound-gateway
      id="sftpWriteOnly"
      session-factory="sftpSessionFactory"
      request-channel="toWriteChannel"
      reply-channel="fromWriteChannel"
      command="mput"
      expression="payload"
      remote-directory="/test/mytest/"
      remote-file-separator="X"
      auto-create-directory="true"
      order="1" mput-regex=".*">
</int-sftp:outbound-gateway>

<int:poller default="true" fixed-delay="500"/>

当我传递一个文件时,上面的代码有效,但是当我传递文件列表时,我得到以下异常:

Caused by: java.lang.IllegalArgumentException: Only File or String payloads allowed for 'mput'
at org.springframework.integration.file.remote.gateway.AbstractRemoteFileOutboundGateway.doMput(AbstractRemoteFileOutboundGateway.java:816)
at org.springframework.integration.file.remote.gateway.AbstractRemoteFileOutboundGateway.handleRequestMessage(AbstractRemoteFileOutboundGateway.java:598)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:109)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:158)
... 7 more

知道如何解决此问题并上传多个文件吗?请分享任何完整的例子。谢谢

标签: javaspringspring-integrationspring-integration-dslspring-integration-sftp

解决方案


MPUT 发送目录中的所有文件;不是一个列表File

文档...

mput 向服务器发送多个文件并支持以下选项:

-R - Recursive- 发送目录和子目录中的所有文件(可能已过滤)

消息负载必须是代表本地目录的 java.io.File。

...

(我强调,它也可以是一个引用目录的字符串)。

如果您不想发送所有文件,可以添加过滤器。

随意打开“改进” JIRA 问题,我们可以添加对File.


推荐阅读