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我的是:
這裏寫圖片描述

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