Search code examples
javaftpspring-integrationoutbound

Downloading files from FTP server recursively from various sub directories


I have configured beans in an XML file with the inbound channel adapter with an OUTBOUND GATEWAY. I am using @serviceactivator in java class to invoke the channel.But m getting files which are in the root folder only am not able to get the files from the sub directories.

My XML file:

<int:inbound-channel-adapter channel="inbound1" expression="'/'">
 <int:poller fixed-delay="3000"/>
</int:inbound-channel-adapter>

<int-ftp:outbound-gateway id="gatewayGet"
        session-factory="ftpClientFactory"
        request-channel="inbound1"
        local-directory="C:/Users/pprakash/Desktop/DataFiles/actual"
        auto-startup="true"
        command="mget"
        command-options="-R"
        filename-pattern="*.csv"
        expression="'actual/*/*'"
        reply-channel="outbound"/>

<int:channel id="outbound">
<int:interceptors>
    <int:wire-tap channel="logger"/>
</int:interceptors>
</int:channel>
<int:logging-channel-adapter id="logger" log-full-message="true" />

'<bean id="ftpClientFactory"
                  class="org.springframework.integration.ftp.session.DefaultFtpSessionFactory">
            <property name="host" value="192.168.79.1"/>
            <property name="port" value="21"/>
            <property name="username" value="*****"/>
            <property name="password" value="*****"/>
            <property name="bufferSize" value="100000"/>
</bean>'

My java code is like :-

 static int index=0;
@ServiceActivator(inputChannel = "inbound1")
public void foo1(File file) throws InterruptedException {
     logger.debug("Inbound msg to  gateway :[ "+(index++)+"]" +    file.getAbsolutePath());
}

@ServiceActivator(inputChannel = "outbound")
public void foo2(List<File> file) throws InterruptedException {
    System.out.println("outbound gateway");
   logger.debug("File Received  :[ "+(index++)+"]" + file.size());

}

Solution

  • filename-pattern="*.csv"

    Unless your subdirectories also match that pattern they won't be scanned.

    See the documentation

    For example, filename-regex="(subDir|.*1.txt)" will retrieve all files ending with 1.txt in the remote directory and the subdirectory subDir. If a subdirectory is filtered, no additional traversal of that subdirectory is performed.

    (My emphasis).

    So, your pattern needs to be such that subdirectories in the tree will be traversed.