需求:最新項目需要,寫個小功能,需求就是實時下載ftp指定文件夾下的所有文件(包括子目錄)到本地文件夾中,保留文件到目錄路徑不變。
分析:關(guān)鍵在于實時和下載并保持原目錄。實時使用線程的定時調(diào)度完成,主要做后者,這顯然要使用遞歸,但是ftp上的文件是不能直接得到相對路徑的(恕我才疏學(xué)淺,并沒有在FTPClient類中找到類似getPath()的方法),因此路徑是要自己記錄。總體思路有以下:
1、得到所有路徑以及子路徑:遞歸遍歷所有文件到路徑。參數(shù):ftp為FTPClient對象,path為當(dāng)前的路徑,pathArray保存當(dāng)前的路徑,并將此路徑集合帶到主函數(shù)中去
1
|
getPath(ftp,path,pathArray); |
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public static void getPath(FTPClient ftp,String path,ArrayList<String> pathArray) throws IOException{ FTPFile[] files = ftp.listFiles(); for (FTPFile ftpFile : files) { if (ftpFile.getName().equals( "." )||ftpFile.getName().equals( ".." )) continue ; if (ftpFile.isDirectory()){ //如果是目錄,則遞歸調(diào)用,查找里面所有文件 path+= "/" +ftpFile.getName(); pathArray.add(path); ftp.changeWorkingDirectory(path); //改變當(dāng)前路徑 getPath(ftp,path,pathArray); //遞歸調(diào)用 path=path.substring( 0 , path.lastIndexOf( "/" )); //避免對之后的同目錄下的路徑構(gòu)造作出干擾, } } } |
2、下載到指定的本地文件夾中,
download(ftp, pathArray, "c:\\download");程序之前出了寫錯誤,為了排查,我把下載分成兩部分,第一部分先將所有目錄創(chuàng)建完成,在第二個for循環(huán)中進(jìn)行文件的下載。參數(shù):ftp為FTPClient,pathArray為1中帶出的路徑集合,后面一個String為本地路徑
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
public static void download(FTPClient ftp,ArrayList<String> pathArray,String localRootPath) throws IOException{ for (String string : pathArray) { String localPath=localRootPath+string; File localFile = new File(localPath); if (!localFile.exists()) { localFile.mkdirs(); } } for (String string : pathArray) { String localPath=localRootPath+string; //構(gòu)造本地路徑 ftp.changeWorkingDirectory(string); FTPFile[] file=ftp.listFiles(); for (FTPFile ftpFile : file) { if (ftpFile.getName().equals( "." )||ftpFile.getName().equals( ".." )) continue ; File localFile = new File(localPath); if (!ftpFile.isDirectory()){ OutputStream is = new FileOutputStream(localFile+ "/" +ftpFile.getName()); ftp.retrieveFile(ftpFile.getName(), is); is.close(); } } } } |
測試的主函數(shù),使用的ftpClient為org.apache.commons.net.ftp.FTPClient:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
public static void main(String[] args) throws SocketException, IOException { FTPClient ftp = new FTPClient(); ftp.connect( "127.0.0.1" ); ftp.login( "test" , "test" ); int reply; reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); System.err.println( "FTP server refused connection." ); System.exit( 1 ); } ftp.setBufferSize( 1024 ); ftp.setFileType(FTP.BINARY_FILE_TYPE); ftp.enterLocalPassiveMode(); String path= "" ; ArrayList<String> pathArray= new ArrayList<String>(); getPath(ftp,path,pathArray); System.out.println(pathArray); download(ftp, pathArray, "c:\\download" ); ftp.logout(); ftp.disconnect(); } |
以上所述是小編給大家介紹的使用ftpClient下載ftp上所有文件,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對服務(wù)器之家網(wǎng)站的支持!
原文鏈接:http://www.cnblogs.com/chenkaiwei/archive/2017/04/24/6754817.html