使用ftpClient下载ftp上所有文件解析

2025-05-29 0 58

需求:最新项目需要,写个小功能,需求就是实时下载ftp指定文件夹下的所有文件(包括子目录)到本地文件夹中,保留文件到目录路径不变。

分析:关键在于实时和下载并保持原目录。实时使用线程的定时调度完成,主要做后者,这显然要使用递归,但是ftp上的文件是不能直接得到相对路径的(恕我才疏学浅,并没有在FTPClient类中找到类似getPath()的方法),因此路径是要自己记录。总体思路有以下:

  1、得到所有路径以及子路径:递归遍历所有文件到路径。参数:ftp为FTPClient对象,path为当前的路径,pathArray保存当前的路径,并将此路径集合带到主函数中去   

?

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()){//如果是目录,则递归调用,查找里面所有文件

path+="/"+ftpFile.getName();

pathArray.add(path);

ftp.changeWorkingDirectory(path);//改变当前路径

getPath(ftp,path,pathArray);//递归调用

path=path.substring(0, path.lastIndexOf("/"));//避免对之后的同目录下的路径构造作出干扰,

}

}

}

  2、下载到指定的本地文件夹中,

    download(ftp, pathArray, "c:\\\\download");程序之前出了写错误,为了排查,我把下载分成两部分,第一部分先将所有目录创建完成,在第二个for循环中进行文件下载。参数: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;//构造本地路径

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();

}

}

}

}

测试的主函数,使用的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上所有文件,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!

原文链接:http://www.cnblogs.com/chenkaiwei/archive/2017/04/24/6754817.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 使用ftpClient下载ftp上所有文件解析 https://www.kuaiidc.com/116899.html

相关文章

发表评论
暂无评论