ftp列出具体目录的所有目录,和目录按照文件类型列出
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列出具体目录的所有目录,和目录按照文件类型列出的更多相关文章
- FileZilla Server ftp 服务器下通过alias别名设置虚拟目录(多个分区)
最近检查服务器的时候发现磁盘空间不够用了,正好有两个硬盘正好,一个硬盘还空着,正好通过ftp服务器的别名功能实现添加空间了,这样就不用重新弄机器了 说明:FileZilla Server 的虚拟目录设 ...
- php 移动或重命名文件(图片)到另一目录下的方法有多种,这里只列出三种:
php 移动或重命名文件(图片)到另一目录下的方法有多种,这里只列出三种: 方法一:使用copy函数 格式:copy(source,destination) 将文件从 source ...
- Linux 目录详解 树状目录结构图
1.树状目录结构图 2./目录 目录 描述 / 第一层次结构的根.整个文件系统层次结构的根目录. /bin/ 需要在单用户模式可用的必要命令(可执行文件):面向所有用户,例如:cat.ls.cp,和/ ...
- 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 ...
- php 执行的目录到新的 directory 目录中
chdir : 改变目录. dir : 目录类别类. closedir : 关闭目录 handle. opendir : 打开目录 handle. readdir : 读取目录 handle. rew ...
- 本函数用来改变目前 php 执行的目录到新的 directory 目录中
chdir : 改变目录. dir : 目录类别类. closedir : 关闭目录 handle. opendir : 打开目录 handle. readdir : 读取目录 handle. rew ...
- CentOS(七)--Linux文件类型及目录配置
这篇随笔将会对Linux系统的文件类型以及Linux的目录结构进行详细补充(linux中目录管理和权限非常重要,特别是在linux安装数据库类软件). 一.Linux更改文件权限的两种方式 在之前的一 ...
- linux学习笔记之文件类型,及目录介绍
引用A:http://www.cnblogs.com/xiaoluo501395377/archive/2013/04/20/3033131.html 引用B:http://www.cnblogs.c ...
- 使用FreePic2Pdf导出书签至Word建立层级目录——快速初始化Word笔记本目录
使用FreePic2Pdf导出书签至Word建立层级目录 --快速初始化Word笔记本目录 文:安徽师范大学2014级计算机科学与技术 王昊 (Get Contact:441301158@qq.com ...
随机推荐
- tensorflow-解决3个问题
问题一:对特征归一化 Min-Max Scaling: X′=a+(X−Xmin)(b−a)/(Xmax−Xmin) # Problem 1 - Implement Min-Max scaling f ...
- List<int>转化为逗号链接的字符串
/// <summary> /// List<int>转化为逗号链接的字符串 /// </summary> /// <param name="lis ...
- MyEclipse基本配置及优化【MyEclipse_10.7】
MyEclipse基本配置 MyEclipse所有的配置都是基于工作空间的,当换了个工作空间,之前的所有配置信息就失效了.(建议每次换工作空间之前就配置好基本信息) 1.修改工作空间编码为UTF-8 ...
- 【vue lazyload】插件的使用步骤
VUE图片懒加载-vue lazyload插件的简单使用
- NLP/CL 顶会收录
全文转载自知乎@刘知远老师:初学者如何查阅自然语言处理学术资料(2016修订版). 1. 国际学术组织.学术会议与学术论文 自然语言处理(natural language processing,NLP ...
- mybatis配置加载源码概述
Mybatis框架里,有两种配置文件,一个是全局配置文件config.xml,另一个是对应每个表的mapper.xml配置文件.Mybatis框架启动时,先加载config.xml, 在加载每个map ...
- 部署jenkins+git
Jenkins简介 Jenkins是一个开源软件项目,是基于Java开发的一种持续集成工具,用于监控持续重复的工作,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能 安装并启动思路: 安装准备 ...
- Legendre公式和Kummer定理
Legendre公式 对于质数\(p\),函数\(v_p(n)\)为\(n\)标准分解后\(p\)的次数 显然有 \[v_p(n!) = \sum\limits_{i = 1}^{\infty} \l ...
- sysbench0.4.12测试query_cache_size和query_cache_type
建议: query_cache_size和query_cache_type生产环境中关闭. (1)软件包下载地址: https://dev.mysql.com/downloads/benchmarks ...
- 爱搞事情的webpack
webpack 是一个现代 JavaScript 应用程序的静态模块打包器(module bundler). 当 webpack 处理应用程序时,它会递归地构建一个依赖关系图(dependency g ...