Java Spring-FTP - 集成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20777744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:16:00  来源:igfitidea点击:

Spring-FTP - Integration

javaspringftp

提问by shailesh repalle

What I am trying to do :

我正在尝试做的事情:

Iam trying to transfer file from C:\maven to C:\shaileshftp using spring ftp . FTP server used - FileZillaServer.Host is localhost. We have a single machine to test the whole functionality.

我正在尝试使用 spring ftp 将文件从 C:\maven 传输到 C:\shaileshftp 。使用的 FTP 服务器 - FileZillaServer.Host 是本地主机。我们有一台机器来测试整个功能。

Issue and Error log

问题和错误日志

SEVERE: All attempts to deliver Message to MessageHandlers failed. Multiple causes: error occurred in message handler [org.springframework.integration.ftp.gateway.FtpOutboundGateway#0] Error handling message for file [c:\shaileshftp\b.txt -> b.txt]

严重:向 MessageHandlers 传递消息的所有尝试均失败。多种原因:消息处理程序中发生错误 [org.springframework.integration.ftp.gateway.FtpOutboundGateway#0] 文件 [c:\shaileshftp\b.txt -> b.txt] 的错误处理消息

cause.org.springframework.integration.MessageHandlingException: error occurred in message handler [org.springframework.integration.ftp.gateway.FtpOutboundGateway

cause.org.springframework.integration.MessageHandlingException:消息处理程序中发生错误 [org.springframework.integration.ftp.gateway.FtpOutboundGateway

Caused by: java.lang.ClassCastException: java.io.File cannot be cast to java.lang.String

引起:java.lang.ClassCastException:java.io.File 不能转换为 java.lang.String

Spring configuration code:
-------------------------


    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-stream="http://www.springframework.org/schema/integration/stream"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:int-file="http://www.springframework.org/schema/integration/file"
xmlns:int-ftp="http://www.springframework.org/schema/integration/ftp"
xsi:schemaLocation="http://www.springframework.org/schema/integration/ftp http://www.springframework.org/schema/integration/ftp/spring-integration-ftp-2.2.xsd
            http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
            http://www.springframework.org/schema/integration/file http://www.springframework.org/schema/integration/file/spring-integration-file-2.2.xsd
            http://www.springframework.org/schema/integration/stream http://www.springframework.org/schema/integration/stream/spring-integration-stream-2.2.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<!-- Step1 :Adding FTP Namespace -->

<!-- Step2 : FPT session Factory configuration -->
<bean id="ftpSessionFactory"
    class="org.springframework.integration.ftp.session.DefaultFtpSessionFactory">
    <property name="host" value="localhost" />
    <property name="port" value="21" />
    <property name="username" value="shailesh" />
    <property name="password" value="Passw0rd" />
    <property name="clientMode" value="0" />
    <property name="fileType" value="2" />
    <property name="bufferSize" value="100000" />
</bean>

<int:channel id="ftpChannel" />

<int-ftp:inbound-channel-adapter id="ftpInbound"
    cache-sessions="false" channel="ftpChannel" session-factory="ftpSessionFactory"
    charset="UTF-8" auto-create-local-directory="true"
    delete-remote-files="true" filename-pattern="*.*" remote-directory="C:/maven"
    remote-file-separator="/" local-directory="c:/shaileshftp">
    <int:poller fixed-rate="1000" />
</int-ftp:inbound-channel-adapter>

<int-ftp:outbound-gateway id="gateway1"
    session-factory="ftpSessionFactory" request-channel="ftpChannel"
    command="ls" command-options="-1" expression="payload" reply-channel="ftpChannel" />


<int-ftp:outbound-channel-adapter id="ftpOutbound"
    channel="ftpChannel" session-factory="ftpSessionFactory" charset="UTF-8"
    remote-file-separator="/" auto-create-directory="true"
    remote-directory-expression="headers['remote_dir']"
    temporary-remote-directory-expression="headers['temp_remote_dir']" />

Java Code

Java代码

package com.mq.springintegration;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class TestMqSpringIntegration {
    public static void main(String[] args) {
        try {
            ApplicationContext ctx = new FileSystemXmlApplicationContext(
                "src/res/applicationcontext.xml");
        } catch (Exception e) {
                e.printStackTrace();
        }
    }
}

回答by Paulo Miguel Almeida

I couldn't truly get your code so I decided to make a simple example based on garyrussell 's repository

我无法真正获得您的代码,因此我决定基于 garyrussell 的存储库制作一个简单的示例

Here are the following sources.

以下是以下来源。

pom.xml

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>ftpexample</groupId>
    <artifactId>ftpexample</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <configuration.source>1.6</configuration.source>
        <configuration.target>1.6</configuration.target>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <org.springframework.version>3.1.1.RELEASE</org.springframework.version>
    </properties>


<dependencies>
    <!-- Spring IoC Dependencies -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.integration</groupId>
        <artifactId>spring-integration-ftp</artifactId>
        <version>2.1.6.RELEASE</version>
    </dependency>


</dependencies>

</project>

applicationContext.xml - Notice you must fill in your connection details

applicationContext.xml - 注意你必须填写你的连接细节

<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-ftp="http://www.springframework.org/schema/integration/ftp"
       xsi:schemaLocation="http://www.springframework.org/schema/integration/ftp http://www.springframework.org/schema/integration/ftp/spring-integration-ftp.xsd
                http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id="ftpClientFactory"
          class="org.springframework.integration.ftp.session.DefaultFtpSessionFactory">
        <property name="host" value="localhost"/>
        <property name="port" value="21"/>
        <property name="username" value="<yourUser>"/>
        <property name="password" value="<yourPassword>"/>
        <property name="clientMode" value="0"/>
        <property name="fileType" value="2"/>
        <property name="bufferSize" value="100000"/>
    </bean>

    <int:channel id="ftpChannel" />

    <int-ftp:outbound-channel-adapter id="ftpOutbound"
                                      channel="ftpChannel"
                                      remote-directory="/Users/paulomiguelalmeida"
                                      session-factory="ftpClientFactory"/>

</beans>

And a sample java class which sends a message to spring asking to upload a test file.

还有一个示例 java 类,它向 spring 发送消息,要求上传测试文件。

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.support.MessageBuilder;

import java.io.File;

public class SendFileSpringFTP {

    public static void main(String[] args) throws InterruptedException {
        ConfigurableApplicationContext ctx =
                new ClassPathXmlApplicationContext("/applicationContext.xml");

        MessageChannel ftpChannel = ctx.getBean("ftpChannel", MessageChannel.class);

        File file = new File("/Users/paulomiguelalmeida/Desktop/workspace/idea/ftpexample/src/main/resources/test.txt");
        final Message<File> messageFile = MessageBuilder.withPayload(file).build();
        ftpChannel.send(messageFile);

        Thread.sleep(2000);
    }

}

enter image description here

在此处输入图片说明

Hope it works for you

希望这对你有用