1、spring 中集成camel-ftp
近期項目中涉及到定期獲取讀取并解析ftp服務器上的文件,自己實現ftp-client的有些復雜,因此考慮集成camel-ftp的方式來解決ftp文件的下載問題。自己則專注于文件的解析工作.
demo: https://github.com/luckydl/ftp-camel-demo
1.1、pom引用
1
2
3
4
5
6
7
8
9
10
|
<dependency> <groupid>org.apache.camel</groupid> <artifactid>camel-spring-boot-starter</artifactid> <version> 2.22 . 1 </version> </dependency> <dependency> <groupid>org.apache.camel</groupid> <artifactid>camel-ftp</artifactid> <version> 2.22 . 1 </version> </dependency> |
注意:在選擇版本的時候,如果springboot版本是1.5.10.release的話,那么camel的版本最高只能使用2.21.2,使用2.22版本將會報錯。經測試的配套關系如下:
srpingboot | camel |
---|---|
1.5 | <=2.21.2 |
2.0 | >=2.22.x |
其他情況都會出現錯誤.
1.2、springboot application.yml配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
ftp: addr: 172.18 . 18.19 : 21 # ftp地址、端口 name: ftpuser password: ftp2018 options: password=${ftp.password}&readlock=rename&delay=10s&binary= true &filter=#zipfilefilter&noop= true &recursive= true url: ftp: //${ftp.name}@${ftp.addr}/?${ftp.options} # 本地下載目錄 local-dir: /var/data # 后臺運行進程 camel: springboot: main-run-controller: true management: endpoint: camelroutes: enabled: true read-only: true |
配置說明:
- delay:每次讀取時間間隔
- filter: 指定文件過濾器
- noop:讀取后對源文件不做任何處理
- recursive:遞歸掃描子目錄,需要在過濾器中允許掃描子目錄
- readlock:對正在寫入的文件的處理機制
更多參數配置見官方手冊
1.3、配置路由
要配置從遠端服務器下載文件到本地,格式如下,from內部為我們在上面配置的url,to為本地文件路徑。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
@component public class downloadroute extends routebuilder { /** logger */ private static final logger logger = loggerfactory.getlogger(downloadroute. class ); @value ( "${ftp.server.info}" ) private string sftpserver; @value ( "${ftp.local.dir}" ) private string downloadlocation; @autowired private dataprocessor dataprocessor; @override public void configure() throws exception{ from(sftpserver) .to(downloadlocation) .process(dataprocessor) .log(logginglevel.info, logger, "download file ${file:name} complete." ); } } |
說明:
若將from配置為本地地址,to配置為遠端地址,則可以實現向遠端服務器上傳文件
process是數據處理器,如果僅僅是下載文件到本地,那么就不需要該配置。
也可以配置多條路由也處理不同的業務:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@override public void configure() throws exception{ // route1 from(sftpserver) .to(downloadlocation) .process(dataprocessor) .log(logginglevel.info, logger, "download file ${file:name} complete." ); // route2 from(xxx).to(xxxx); // route3 from(xxxx).to(xxx).process(xxx); } |
1.4、配置文件過濾
如果ftp服務器上有很多文件,但是我們需要的只是其中的一種,全部下載下來,有業務層來實現過濾肯定不合適,我們可以使用camel-ftp的文件過濾器,通過url中的filter來指定,如“filter=#zipfilefilter”,
用戶需要實現genericfilefilter接口的accept方法。
例如我們只需要下載后綴名為.zip的壓縮包到本地,過濾器的編寫方法如下,因為我要遞歸掃描子目錄,因此類型為目錄的文件也需要允許通過。
1
2
3
4
5
6
7
8
9
10
11
|
/** * camel ftp zip文件過濾器 */ @component public class zipfilefilter implements genericfilefilter { @override public boolean accept(genericfile file) { return file.getfilename().endswith( ".zip" ) || file.isdirectory(); } } |
1.5、文件處理器
文件處理器就是我們對下載到本地的文件進行處理的操作,比如我們可能需要對下載的文件重新規劃目錄;或者解析文件并進行入庫操作等。這就需要通過實現processer的process方法。
本文中的demo就是通過processor來解析zip包中的文件內容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
@component public class dataprocessor implements processor { /** logger */ private static final logger logger = loggerfactory.getlogger(dataprocessor. class ); @value ( "${ftp.local-dir}" ) private string filedir; @override public void process(exchange exchange) throws exception { genericfilemessage<randomaccessfile> infilemessage = (genericfilemessage<randomaccessfile>) exchange.getin(); string filename = infilemessage.getgenericfile().getfilename(); string file_path = filedir + '/' + filename; readzip(file_path); } ... // 省略數據處理方法 } |
2、參考資料
關于camel ftp的各個參數配置,參見官方手冊:http://camel.apache.org/ftp2.html
此處需要注意的是,camel ftp手冊里面只寫了ftp獨有的一些配置項,camel-ftp組件繼承自camel-file,手冊里面有說明,就一句話,不注意就可能忽略了,筆者就是沒注意,被遞歸掃描子目錄的問題折騰了2天(閱讀要細心o(╥﹏╥)o)。。。因此有一些參數配置項可能在camel-ftp手冊里面找不到,請移步至:http://camel.apache.org/file2.html
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/donlin-zhang/p/9755403.html