Apache Camel之FTP组件:简单地从Ftp服务器下载文件到本地文件夹

前期准备:在自己的电脑上搭建FTP服务器

  • 非Spring实现

    maven依赖:
    在一个项目里有关于camel的版本要保持一致!!!

<dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-core</artifactId>
    </dependency>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-jms</artifactId>
    </dependency>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-ftp</artifactId>
      <version>2.15.6</version>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
    </dependency>

具体实现:

public class RouteBuilderExample {

    public static void main(String args[]) throws Exception {
        CamelContext context = new DefaultCamelContext();

        context.addRoutes(new RouteBuilder() {
            @Override
            public void configure() {
                 from("ftp://[email protected]:21/FtpDownLoad?password=123456").
                 to("file:E:/FtpTestFile");
            }
        });
        context.start();
        Thread.sleep(10000);
        context.stop();
    }
}
  • Spring实现
    这里写图片描述

    camel-context.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"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://camel.apache.org/schema/spring 
       http://camel.apache.org/schema/spring/camel-spring.xsd
">

  <bean id="ftpToFile" class="camelinaction.FtpToFile"/>

  <!--装载 CamelContext -->

  <camelContext xmlns="http://camel.apache.org/schema/spring">  
    <!-- Java DSL 方式 -->
    <routeBuilder ref="ftpToFile"/>

    <!-- Spring DSL 方式 -->
   <route>
     <from uri="ftp://[email protected]:21/FtpDownLoad?password=123456"/>
     <to uri="file:E:/FtpTestFile"/>
   </route>
  </camelContext>
</beans>

FtpToFile类:

public class FtpToFile extends RouteBuilder{
    @Override
    public void configure() throws Exception { 
      from("ftp://[email protected]:21/FtpDownLoad?password=123456").
      to("file:E:/360");
    }
}

最后在win+R窗口进入项目所在目录运行maven我的是:
这里写图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章