java开发FTP客户端列出指定目录下面所有文件
来源:互联网 发布:淘宝俪人购是正品吗 编辑:程序博客网 时间:2024/06/07 00:12
http://zhouzaibao.iteye.com/blog/362866
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.ArrayList;
- import java.util.Iterator;
- import org.apache.commons.net.PrintCommandListener;
- import org.apache.commons.net.ftp.FTPClient;
- import org.apache.commons.net.ftp.FTPFile;
- import org.apache.commons.net.ftp.FTPReply;
- import org.apache.log4j.Logger;
- /**
- * 列出FTP服务器上指定目录下面的所有文件
- * @author BenZhou mailto:zhouzb@qq.com
- * 原文地址:http://zhouzaibao.iteye.com/blog/362866
- */
- public class FTPListAllFiles {
- private static Logger logger = Logger.getLogger(FTPListAllFiles.class);
- public FTPClient ftp;
- public ArrayList<String> arFiles;
- /**
- * 重载构造函数
- * @param isPrintCommmand 是否打印与FTPServer的交互命令
- */
- public FTPListAllFiles(boolean isPrintCommmand){
- ftp = new FTPClient();
- arFiles = new ArrayList<String>();
- if(isPrintCommmand){
- ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
- }
- }
- /**
- * 登陆FTP服务器
- * @param host FTPServer IP地址
- * @param port FTPServer 端口
- * @param username FTPServer 登陆用户名
- * @param password FTPServer 登陆密码
- * @return 是否登录成功
- * @throws IOException
- */
- public boolean login(String host,int port,String username,String password) throws IOException{
- this.ftp.connect(host,port);
- if(FTPReply.isPositiveCompletion(this.ftp.getReplyCode())){
- if(this.ftp.login(username, password)){
- this.ftp.setControlEncoding("GBK");
- return true;
- }
- }
- if(this.ftp.isConnected()){
- this.ftp.disconnect();
- }
- return false;
- }
- /**
- * 关闭数据链接
- * @throws IOException
- */
- public void disConnection() throws IOException{
- if(this.ftp.isConnected()){
- this.ftp.disconnect();
- }
- }
- /**
- * 递归遍历出目录下面所有文件
- * @param pathName 需要遍历的目录,必须以"/"开始和结束
- * @throws IOException
- */
- public void List(String pathName) throws IOException{
- if(pathName.startsWith("/")&&pathName.endsWith("/")){
- String directory = pathName;
- //更换目录到当前目录
- this.ftp.changeWorkingDirectory(directory);
- FTPFile[] files = this.ftp.listFiles();
- for(FTPFile file:files){
- if(file.isFile()){
- arFiles.add(directory+file.getName());
- }else if(file.isDirectory()){
- List(directory+file.getName()+"/");
- }
- }
- }
- }
- /**
- * 递归遍历目录下面指定的文件名
- * @param pathName 需要遍历的目录,必须以"/"开始和结束
- * @param ext 文件的扩展名
- * @throws IOException
- */
- public void List(String pathName,String ext) throws IOException{
- if(pathName.startsWith("/")&&pathName.endsWith("/")){
- String directory = pathName;
- //更换目录到当前目录
- this.ftp.changeWorkingDirectory(directory);
- FTPFile[] files = this.ftp.listFiles();
- for(FTPFile file:files){
- if(file.isFile()){
- if(file.getName().endsWith(ext)){
- arFiles.add(directory+file.getName());
- }
- }else if(file.isDirectory()){
- List(directory+file.getName()+"/",ext);
- }
- }
- }
- }
- public static void main(String[] args) throws IOException {
- FTPListAllFiles f = new FTPListAllFiles(true);
- if(f.login("192.168.0.3", 21, "test", "test")){
- f.List("/","mp4");
- }
- f.disConnection();
- Iterator<String> it = f.arFiles.iterator();
- while(it.hasNext()){
- logger.info(it.next());
- }
- }
- }
1 2
- java开发FTP客户端列出指定目录下面所有文件
- 算法:递归列出一个目录下面的所有文件
- java列出指定目录下的文件
- Java图形界面开发—列出指定目录
- java 列出指定目录下的所有文件及文件扩展名
- java列出目录下的所有文件
- java列出目录下的所有文件
- [Linux C]列出指定目录下的所有文件(夹)
- javaFile循环列出指定目录下的所有文件(源代码)
- 列出指定目录下的所有文件的绝对路径
- 递归列出指定目录下的所有文件的绝对路径
- 递归列出指定目录下所有的符号链接文件
- 递归列出指定目录下所有的普通文件
- 列出指定目录下属于给定用户的所有文件
- 列出指定目录下后缀是.mp3的所有文件
- 列出指定目录所有文件并修改文件名的源代码
- 列出指定目录下的所有文件的路径
- dos批处理:列出指定目录下所有文件
- 重新编译Hadoop 2.7.2 native以支持snappy
- Java windows项目迁移到linux上,遇到一些跟路径相关的问题
- cocos2d-C++ 学习UI控件(五)之 PageView
- Session服务器详解
- 点云超像素分割实验进展
- java开发FTP客户端列出指定目录下面所有文件
- Web API文档生成工具apidoc
- iOS中CoreAnimation的一些小问题 animationDidStop确认哪个动画结束 和 连续animation之间的动画不连续闪现
- [转载]Windows Hook 易核心编程<2>远程线程注入 下
- 与独角兽公司技术高管切磋交流,问啊W-Time上海站活动全程免费!
- CocoaPods详解之----使用篇
- 解决Eclipse导入别人工程时的中文乱码问题
- my heart
- MySQL命令行登录的例子