前期准备:在自己的电脑上搭建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://ftpTest@172.16.1.13: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://ftpTest@172.16.1.13: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://ftpTest@172.16.1.13:21/FtpDownLoad?password=123456").
to("file:E:/360");
}
}
最后在win+R窗口进入项目所在目录运行maven我的是: