使用ftpClient下載ftp上所有文件解析
需求:最新項目需要,寫個小功能,需求就是實時下載ftp指定文件夾下的所有文件(包括子目錄)到本地文件夾中,保留文件到目錄路徑不變。
分析:關(guān)鍵在于實時和下載并保持原目錄。實時使用線程的定時調(diào)度完成,主要做后者,這顯然要使用遞歸,但是ftp上的文件是不能直接得到相對路徑的(恕我才疏學(xué)淺,并沒有在FTPClient類中找到類似getPath()的方法),因此路徑是要自己記錄。總體思路有以下:
1、得到所有路徑以及子路徑:遞歸遍歷所有文件到路徑。參數(shù):ftp為FTPClient對象,path為當(dāng)前的路徑,pathArray保存當(dāng)前的路徑,并將此路徑集合帶到主函數(shù)中去
getPath(ftp,path,pathArray);
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)中進行文件的下載。參數(shù):ftp為FTPClient,pathArray為1中帶出的路徑集合,后面一個String為本地路徑
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:
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ǎng)站的支持!
相關(guān)文章
elasticsearch集群cluster?discovery可配式模塊示例分析
這篇文章主要為大家介紹了elasticsearch集群cluster?discovery可配式模塊示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-04-04
Javabean轉(zhuǎn)換成json字符并首字母大寫代碼實例
這篇文章主要介紹了javabean轉(zhuǎn)成json字符并首字母大寫代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-02-02
Spring學(xué)習(xí)筆記之RedisTemplate的配置與使用教程
這篇文章主要給大家介紹了關(guān)于Spring學(xué)習(xí)筆記之RedisTemplate配置與使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用spring具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-06-06
Java中關(guān)于控制臺讀取數(shù)字或字符串的方法
下面小編就為大家?guī)硪黄狫ava中關(guān)于控制臺讀取數(shù)字或字符串的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10
使用java實現(xiàn)BBS論壇發(fā)送郵件過程詳解
這篇文章主要介紹了使用java發(fā)送郵件過程詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04

