Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用spring集成从ftp列出文件_Java_Ftp_Spring Integration - Fatal编程技术网

Java 使用spring集成从ftp列出文件

Java 使用spring集成从ftp列出文件,java,ftp,spring-integration,Java,Ftp,Spring Integration,我想使用SpringIntegration列出FTP服务器上的所有文件,例如,在屏幕上打印它们。我做过这样的事情: 背景: <int:channel id="toSplitter"> <int:interceptors> <int:wire-tap channel="logger"/> </int:interceptors> </int:channel> <int:logging-channel-

我想使用SpringIntegration列出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>
输出:

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的结果发送到,GetFTPCchannel将逐个接收FileInfo对象

要将它们全部打印出来,您实际上应该有一个无限循环:

while (true) {
  variable("tt", channel.receive().toString());
  echo("${tt}");
}
要停止应用程序,你应该提供一些shoutDownHook或从控制台输入听一些东西

另一点是,用无限接收阻止你的应用是不好的。 有一个重载方法,它应用了超时参数。最后一项可能有助于确定循环的结束:

while (true) {
  Message<?> receive = channel.receive(10000);
  if (receive == null) {
      break;
  }
  variable("tt", receive.toString());
  echo("${tt}");
}
@配置 公共类FtpConfig{ @豆子 公共默认ftpSessionFactory ftpSessionFactory{ DefaultFtpSessionFactory ftpSessionFactory=新的DefaultFtpSessionFactory; ftpSessionFactory.setHostlocalhost; ftpSessionFactory.setPort21; ftpSessionFactory.setUsernameuser; ftpSessionFactory.setPasswordpass; 返回工厂; } } @豆子 公共应用程序运行程序RunnerDefaultFTPSSessionFactory sf{ 返回参数->{ FTPFile[]list=sf.getSession.list。; 对于FTPFile文件:列表{ System.out.printlnResult:+file.getName; } }; }
请解释一下这堵文字墙的作用。就外行而言,它可能会教OP喊“Fus Ro Dah”
while (true) {
  Message<?> receive = channel.receive(10000);
  if (receive == null) {
      break;
  }
  variable("tt", receive.toString());
  echo("${tt}");
}