从远程位置进行文件轮询,连接成功,但使用FTP Spring Integration从本地目录读取

时间:2016-05-14 18:29:16

标签: java spring ftp spring-integration spring-integration-sftp

我已经使用spring集成配置了FTP,而使用入站端我无法从远程路径获取文件,它始终从本地系统路径读取

FTP-入站监听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);
        }

    }
}

这是我完整的FTP配置,但我无法从FTP远程目录中读取它从local-direstory中读取

请帮助

1 个答案:

答案 0 :(得分:0)

这就是它的工作方式 - 文件被复制到本地目录,消息有效负载是本地目录中的File对象。如果文件已存在于本地,则不会重新获取该文件。