java - 使用 spring-integration 列出 ftp 中的文件

标签 java ftp spring-integration

我想使用 spring-integration 列出 FTP 服务器上的所有文件,例如,将它们打印在屏幕上。我做过这样的事情:

上下文:

<int:channel id="toSplitter">
    <int:interceptors>
        <int:wire-tap channel="logger"/>
    </int:interceptors>
</int:channel>

<int:logging-channel-adapter id="logger" log-full-message="true"/>
<int:splitter id="splitter" input-channel="toSplitter" output-channel="getFtpChannel"/>

<int-ftp:outbound-gateway id="gatewayLS"
                        session-factory="ftpClientFactory"
                        request-channel="inbound"
                        command="ls"
                        expression="payload"
                        reply-channel="toSplitter"/>
<int:channel id="getFtpChannel">
    <int:queue/>
</int:channel>
    <bean id="ftpClientFactory"
        class="org.springframework.integration.ftp.session.DefaultFtpSessionFactory">
    <property name="host" value="${host}"/>
    <property name="username" value="${user}"/>
    <property name="password" value="${password}"/>
    <property name="clientMode" value="0"/>
    <property name="fileType" value="2"/>
    <property name="bufferSize" value="10000000"/>                  
</bean>

Java代码:

ConfigurableApplicationContext context = 
            new FileSystemXmlApplicationContext("/src/citrus/resources/citrus-context.xml");
final FtpFlowGateway ftpFlow = context.getBean(FtpFlowGateway.class);
ftpFlow.lsFiles("/");
PollableChannel channel = context.getBean("getFtpChannel", PollableChannel.class);
variable("tt", channel.receive().toString());
echo("${tt}");

输出:

11:09:17,169 INFO  port.LoggingReporter| Test action <echo>
11:09:17,169 INFO    actions.EchoAction| [Payload=FileInfo [isDirectory=false,  isLink=false, Size=3607, ModifiedTime=Tue Jul 15 14:18:00 CEST 2014,       Filename=Smoke03_angart30_st40.exi, RemoteDirectory=/, Permiss
ions=-rw-r--r--]][Headers= {replyChannel=org.springframework.integration.core.MessagingTemplate$TemporaryReplyChannel@7829b776, sequenceNumber=1,  errorChannel=org.springframework.integration.core.MessagingTempla
te$TemporaryReplyChannel@7829b776, file_remoteDirectory=/, sequenceSize=1, correlationId=49b57f2d-4dbf-4a89-b5b8-0dfb15bca2be, id=0a58ad65-74b4-4aae-87be- aa6034a41776, timestamp=1405501757060}]
11:09:17,169 INFO  port.LoggingReporter| Test action <echo> done
11:09:17,169 INFO  port.LoggingReporter| TEST STEP 1/1 done

输出没问题,但是当我不知道 FTP 上存储了多少个文件时,我应该如何打印这些信息? (此代码仅打印一个文件)。我尝试检查 channel.receive() 是否为 null,但测试只是卡住了。

最佳答案

自从您发送LS的结果以来到<splitter> ,你的getFtpChannel将收到FileInfo<?>一个接一个的对象。

要打印所有内容,您确实应该有一个无限循环:

while (true) {
  variable("tt", channel.receive().toString());
  echo("${tt}");
}

要停止该应用程序,您应该提供一些 shoutDownHook或者从控制台输入中收听一些内容。

还有一点,用无限来阻止你的应用程序是不好的 receive() 。 有一个重载方法,它应用 timeout参数。最后一个可能有助于确定循环的结束:

while (true) {
  Message<?> receive = channel.receive(10000);
  if (receive == null) {
      break;
  }
  variable("tt", receive.toString());
  echo("${tt}");
}

关于java - 使用 spring-integration 列出 ftp 中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24776775/

相关文章:

exception-handling - Spring Integration - 在服务激活器组件中发生异常时写入错误队列

spring - Netflix hystrix 和 spring 集成

java - Struts2、JPA(支持 hibernate )、Spring ... Struts2 和 Spring 日志记录都通过 log4j 但不是 hibernate 工作

JavaDB双重安装

java - 在 JComboBox 中显示图像

java - Java 中的二进制到字符串转换?

ftp - apache VFS2 uriStyle - 根绝对路径以双斜杠结尾

php - 适用于 Windows 的备用 FTP 客户端

powershell - 在 PowerShell 中检查 FTP 服务器上的文件是否存在

spring-integration - Spring Mqtt集成: outbound topic issue