简体   繁体   中英

Issue with File Polling from remote location, connection success but read from local directory with FTP Spring Integration

i have configured FTP with spring integration while using inbound side im unable to get files from remote path, it always read from the local system path

ftp-inbound-listen-dynamic.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-file="http://www.springframework.org/schema/integration/file"
xmlns:int-mail="http://www.springframework.org/schema/integration/mail"
xmlns:int-ftp="http://www.springframework.org/schema/integration/ftp"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
    http://www.springframework.org/schema/integration          http://www.springframework.org/schema/integration/spring-integration.xsd
    http://www.springframework.org/schema/integration/mail  http://www.springframework.org/schema/integration/mail/spring-integration-mail.xsd
    http://www.springframework.org/schema/integration/file  http://www.springframework.org/schema/integration/file/spring-integration-file.xsd
    http://www.springframework.org/schema/beans             http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/util              http://www.springframework.org/schema/util/spring-util.xsd
    http://www.springframework.org/schema/context           http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/integration/ftp http://www.springframework.org/schema/integration/ftp/spring-integration-ftp.xsd">

<context:property-placeholder />

<bean id="ftpSessionFactory"
    class="org.springframework.integration.ftp.session.DefaultFtpSessionFactory">
    <property name="host" value="${ftp.host}" />
    <property name="port" value="${ftp.port}" />
    <property name="username" value="${ftp.username}" />
    <property name="password" value="${ftp.password}" />
    <property name="clientMode" value="0" />
    <property name="fileType" value="2" />
    <property name="bufferSize" value="100000" />
</bean>

<int:channel id="inboundChannel" />

<int-ftp:inbound-channel-adapter id="ftpInbound"
    channel="inboundChannel" session-factory="ftpSessionFactory" charset="UTF-8"
    auto-create-local-directory="true" delete-remote-files="true"
    temporary-file-suffix=".writing" remote-directory="${ftp.remote-directory}"
    local-directory="${ftp.local-directory}" filter="compositeFilter"
    local-filter="localCompositeFilter">
    <int:poller fixed-rate="15000" />
</int-ftp:inbound-channel-adapter>


<bean id="compositeFilter"
    class="org.springframework.integration.file.filters.CompositeFileListFilter">
    <constructor-arg>
        <list>
            <bean
                class="org.springframework.integration.file.filters.AcceptAllFileListFilter" />
            <bean
                class="org.springframework.integration.ftp.filters.FtpRegexPatternFileListFilter">
                <constructor-arg name="pattern"
                    value=".+(\.(?i)(pdf|PDF|csv|CSV|xml|XML))$" />
            </bean>
        </list>
    </constructor-arg>
</bean>

<bean id="localCompositeFilter"
    class="org.springframework.integration.file.filters.CompositeFileListFilter">
    <constructor-arg>
        <list>
            <bean
                class="org.springframework.integration.file.filters.AcceptAllFileListFilter" />
        </list>
    </constructor-arg>
</bean>

<int:chain input-channel="inboundChannel">
    <int:header-enricher>
    </int:header-enricher>

    <int:service-activator ref="ftpListenerService"
        method="onDataReceived" />
</int:chain>

FtpInboundChannelResolver.java

@Inject
private ApplicationContext                                          applicationContext;

private final String                                                XML_PATH        = "/META-INF/integration/dynamic/ftp-inbound-listen-dynamic.xml";


@Value( "${local.ftp.storage.path}" )
private String                                                      localStoragePath ;

ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext(
                new String[] { XML_PATH }, false );

        ctx.setParent( applicationContext );

        StandardEnvironment env = new StandardEnvironment( );
        Properties props = new Properties( );


        props.setProperty( "ftp.host", "192.168.84.131" );
        props.setProperty( "ftp.port", "21" );
        props.setProperty( "ftp.username", "user" );
        props.setProperty( "ftp.password", "pass" );
        props.setProperty( "ftp.remote-directory", "/home/user/Public/files" );
        props.setProperty( "ftp.local-directory", localStoragePath );

        PropertiesPropertySource pps = new PropertiesPropertySource( "ftpprops", props );
        env.getPropertySources( ).addLast( pps );
        ctx.setEnvironment( env );
        ctx.refresh( );
        channel = ctx.getBean( "inboundChannel", MessageChannel.class );

FTPListnerService.java

@Service("ftpListenerService")
public class FTPListenerService implements IListenerService {

    private static Logger LOGGER = LoggerFactory.getLogger(FTPListenerService.class);

    @Inject
    private ApplicationContext applicationContext;

    public void onDataReceived( File ftpFile) {

        LOGGER.info("FTP: file received for processing " );
        try{
            // read and delete file
            String fileName = ftpFile.getName();
            byte[] fileContents = FileCopyUtils.copyToByteArray(ftpFile);
            ftpFile.delete();

            LOGGER.error("Data Received :", fileContents);


        }catch(Exception ex){
            LOGGER.error("ERROR: while reading ftp transmitted file", ex);
        }

    }
}

here is my complete FTP Configuration but im unable to read from FTP remote directory it read from local-direstory

Please Help

That's the way it works - files are copied to the local directory and the message payload is the File object in the local directory. If the file already exists locally it is not re-fetched.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM