package com.haiyisoft.cAssistantWeb.util;

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.FTP;
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;
/**

*/
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("utf-8");
this.ftp.enterLocalPassiveMode();
this.ftp.setFileType(FTP.BINARY_FILE_TYPE);
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());
arFiles.add(file.getName());
}else if(file.isDirectory()){
// List(directory+file.getName()+"/");
List(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());
arFiles.add(file.getName());
}
}else if(file.isDirectory()){
//List(directory+file.getName()+"/",ext);
List(file.getName()+"/",ext);
}
}
}
}
public static void main(String[] args) throws IOException {
FTPListAllFiles f = new FTPListAllFiles(true);
if(f.login("172.20.188.157", 7021, "zgc", "123456")){
// f.List("/data/app/cassistant/apache-tomcat-7.0.85/webapps/generic/web/upload/");
f.List("/data/app/cassistant/apache-tomcat-7.0.85/webapps/generic/web/upload/","png");

}
f.disConnection();
Iterator<String> it = f.arFiles.iterator();
while(it.hasNext()){
System.out.println(it.next());
}

}
}

ftp列出具体目录的所有目录,和目录按照文件类型列出的更多相关文章

  1. FileZilla Server ftp 服务器下通过alias别名设置虚拟目录(多个分区)

    最近检查服务器的时候发现磁盘空间不够用了,正好有两个硬盘正好,一个硬盘还空着,正好通过ftp服务器的别名功能实现添加空间了,这样就不用重新弄机器了 说明:FileZilla Server 的虚拟目录设 ...

  2. php 移动或重命名文件(图片)到另一目录下的方法有多种,这里只列出三种:

    php 移动或重命名文件(图片)到另一目录下的方法有多种,这里只列出三种:       方法一:使用copy函数   格式:copy(source,destination)   将文件从 source ...

  3. Linux 目录详解 树状目录结构图

    1.树状目录结构图 2./目录 目录 描述 / 第一层次结构的根.整个文件系统层次结构的根目录. /bin/ 需要在单用户模式可用的必要命令(可执行文件):面向所有用户,例如:cat.ls.cp,和/ ...

  4. du 使用详解 linux查看目录大小 linux统计目录大小并排序 查看目录下所有一级子目录文件夹大小 du -h --max-depth=1 |grep [

    常用命令 du -h --max-depth=1 |grep [TG] |sort   #查找上G和T的目录并排序 du -sh    #统计当前目录的大小,以直观方式展现 du -h --max-d ...

  5. php 执行的目录到新的 directory 目录中

    chdir : 改变目录. dir : 目录类别类. closedir : 关闭目录 handle. opendir : 打开目录 handle. readdir : 读取目录 handle. rew ...

  6. 本函数用来改变目前 php 执行的目录到新的 directory 目录中

    chdir : 改变目录. dir : 目录类别类. closedir : 关闭目录 handle. opendir : 打开目录 handle. readdir : 读取目录 handle. rew ...

  7. CentOS(七)--Linux文件类型及目录配置

    这篇随笔将会对Linux系统的文件类型以及Linux的目录结构进行详细补充(linux中目录管理和权限非常重要,特别是在linux安装数据库类软件). 一.Linux更改文件权限的两种方式 在之前的一 ...

  8. linux学习笔记之文件类型,及目录介绍

    引用A:http://www.cnblogs.com/xiaoluo501395377/archive/2013/04/20/3033131.html 引用B:http://www.cnblogs.c ...

  9. 使用FreePic2Pdf导出书签至Word建立层级目录——快速初始化Word笔记本目录

    使用FreePic2Pdf导出书签至Word建立层级目录 --快速初始化Word笔记本目录 文:安徽师范大学2014级计算机科学与技术 王昊 (Get Contact:441301158@qq.com ...

随机推荐

  1. windows下修改vagrant虚拟机中的html ,通过nginx访问后不生效

    更改本地html文件,通过nginx配置的虚拟域名访问, 无论怎么修改内容,都不变化,即使我把内容删除一半,它显示的也是以前内容的一半,除非删除文件,才会从新生成. 解决办法 修改nginx.conf ...

  2. Matplotlib 随机漫步图

    import matplotlib.pyplot as plt from random import choice class Randomwalk(): def __init__(self,num_ ...

  3. 【4】Zookeeper数据模型

    一.Znode节点是什么 1.1.概念   Znode节点是Zookeeper中数据模型中最小的数据单元.Zookeeper的数据模型是一颗树,由"/"进行分割路径.每个znode ...

  4. javascript 元编程之 method_missing

    javascript 元编程之 method_missing 引言 要说元编程 ruby 中技巧太多了,今天来写的这个技术也来自于 ruby 中的灵感. method_missing 这个在 ruby ...

  5. win10 修改文件夹右击默认打开程序

    1.注册表打开 cmd  regedit 2.打开如下位置 3.编辑图中2个Anycode.command的值 为打开 保存即可

  6. 【wifi移植 3】开发板wifi自动获取IP

    内核版本:3.4.61 1. 配置内核,支持DHCP ~/kernel$ make menuconfig [*] Networking support  ---> Networking opti ...

  7. manjaro 常用软件安装

    1.搜狗输入法 sudo pacman -S fcitx-sogoupinyin fcitx-configtool fcitx-im yay -Sa fcitx-qt4 sudo vim /etc/e ...

  8. python基础编程: 函数示例、装饰器、模块、内置函数

    目录: 函数示例 装饰器 模块 内置函数 一.函数示例: 1.为什么使用函数之模块化程序设计: 不使用模块程序设计的缺点: 1.体系结构不清晰,可主读性差: 2.可扩展性差: 3.程序冗长: 2.定义 ...

  9. 内核模式构造-Semaphore构造(WaitLock)

    internal sealed class SimpleWaitLock : IDisposable { //(信号量)允许多个线程并发访问一个资源 //如果所有线程以只读方式访问资源则是安全的 pr ...

  10. springboot中解决servlet乱码问题,使用配置类注册过滤器解决

    8.1 使用传统的Spring提供的字符编码过滤器 在03-springboot-web中的ServletConfig中配置文件类,注册字符编码过滤器 @Bean public FilterRegis ...