java 遍历ftp文件夹_java遍历ftp文件夹下所有文件(或指定文件下的文件)

该博客介绍了如何使用Java的Apache Commons Net库遍历FTP服务器上的文件夹,包括递归列出所有文件及指定扩展名的文件。通过FTPClient对象连接服务器,设置编码避免乱码,然后使用changeWorkingDirectory、listFiles和递归方法遍历目录。示例代码展示了登录、遍历、获取指定扩展名文件和断开连接的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

importjava.io.IOException;importjava.io.PrintWriter;importjava.util.ArrayList;importorg.apache.commons.net.PrintCommandListener;importorg.apache.commons.net.ftp.FTPClient;importorg.apache.commons.net.ftp.FTPFile;importorg.apache.commons.net.ftp.FTPReply;importorg.apache.log4j.Logger;/*** 列出FTP服务器上指定目录下面的所有文件*/

public classFTPListAllFiles {private static Logger logger = Logger.getLogger(FTPListAllFiles.class);publicFTPClient ftp;public ArrayListarFiles;/*** 重载构造函数

*

*@paramisPrintCommmand 是否打印与FTPServer的交互命令*/

public FTPListAllFiles(booleanisPrintCommmand) {

ftp= newFTPClient();

arFiles= new ArrayList();if(isPrintCommmand) {

ftp.addProtocolCommandListener(new PrintCommandListener(newPrintWriter(System.out)));

}

}/*** 登陆FTP服务器

*

*@paramhost FTPServer IP地址

*@paramport FTPServer 端口

*@paramusername FTPServer 登陆用户名

*@parampassword FTPServer 登陆密码

*@return是否登录成功

*@throwsIOException*/

public boolean login(String host, int port, String username, String password) throwsIOException {this.ftp.connect(host, port);if (FTPReply.isPositiveCompletion(this.ftp.getReplyCode())) {if (this.ftp.login(username, password)) {/**需要注意这句代码,如果调用List()方法出现,文件的无线递归,与真实目录结构不一致的时候,可能就是因为转码后,读出来的文件夹与正式文件夹字符编码不一致所导致。

则需去掉转码,尽管递归是出现乱码,但读出的文件就是真实的文件,不会死掉。等读完之后再根据情况进行转码。

如果ftp部署在windows下,则:

for (String arFile : f.arFiles) {

arFile = new String(arFile.getBytes("iso-8859-1"), "GBK");

logger.info(arFile);

}*/

this.ftp.setControlEncoding("GBK");return true;

}

}if (this.ftp.isConnected()) {this.ftp.disconnect();

}return false;

}/*** 关闭数据链接

*

*@throwsIOException*/

public void disConnection() throwsIOException {if (this.ftp.isConnected()) {this.ftp.disconnect();

}

}/*** 递归遍历出目录下面所有文件

*

*@parampathName 需要遍历的目录,必须以"/"开始和结束

*@throwsIOException*/

public void List(String pathName) throwsIOException {if (pathName.startsWith("/") && pathName.endsWith("/")) {//更换目录到当前目录

this.ftp.changeWorkingDirectory(pathName);

FTPFile[] files= this.ftp.listFiles();for(FTPFile file : files) {if(file.isFile()) {

arFiles.add(pathName+file.getName());

}else if(file.isDirectory()) {//需要加此判断。否则,ftp默认将‘项目文件所在目录之下的目录(./)’与‘项目文件所在目录向上一级目录下的目录(../)’都纳入递归,这样下去就陷入一个死循环了。需将其过滤掉。

if (!".".equals(file.getName()) && !"..".equals(file.getName())) {

List(pathName+ file.getName() + "/");

}

}

}

}

}/*** 递归遍历目录下面指定的文件名

*

*@parampathName 需要遍历的目录,必须以"/"开始和结束

*@paramext 文件的扩展名

*@throwsIOException*/

public void List(String pathName, String ext) throwsIOException {if (pathName.startsWith("/") && pathName.endsWith("/")) {//更换目录到当前目录

this.ftp.changeWorkingDirectory(pathName);

FTPFile[] files= this.ftp.listFiles();for(FTPFile file : files) {if(file.isFile()) {if(file.getName().endsWith(ext)) {

arFiles.add(pathName+file.getName());

}

}else if(file.isDirectory()) {if (!".".equals(file.getName()) && !"..".equals(file.getName())) {

List(pathName+ file.getName() + "/", ext);

}

}

}

}

}public static void main(String[] args) throwsIOException {

FTPListAllFiles f= new FTPListAllFiles(true);if (f.login("IP", 7000, "userName", "password")) {

f.List("/", "tar");

}

f.disConnection();for(String arFile : f.arFiles) {

logger.info(arFile);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值