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

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

  • 非Spring实现

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

<dependency>
      <groupId>org.apache.camelgroupId>
      <artifactId>camel-coreartifactId>
    dependency>
    <dependency>
      <groupId>org.apache.camelgroupId>
      <artifactId>camel-jmsartifactId>
    dependency>
    <dependency>
      <groupId>org.apache.camelgroupId>
      <artifactId>camel-ftpartifactId>
      <version>2.15.6version>
    dependency>
    <dependency>
      <groupId>log4jgroupId>
      <artifactId>log4jartifactId>
    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实现
    Apache Camel之FTP组件:简单地从Ftp服务器下载文件到本地文件夹_第1张图片

    camel-context.xml:



<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 xmlns="http://camel.apache.org/schema/spring">  
    
    <routeBuilder ref="ftpToFile"/>

    
   <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我的是:
这里写图片描述

你可能感兴趣的:(Camel)