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 ...
随机推荐
- Python学习计划-首篇
概述 Python语言这几年的热度一直持续上升,有多火也不用我再介绍了,光从近几年编程语言排行榜的上升热度就能看出,因此我也计划学习一下python语言,并且将每次的学习收获记录下来.一方面是记录下来 ...
- fastadmin 金额 字段类型及html验证
金额 字段类型 整数 小数 decimal 10 2 float 10 2 html验证 <div class="form-group&q ...
- thinkphp5.0 field和with连用的问题
field在with后面时field会不起作用,会查询数据库中全部的字段: field在with前面时会无法筛选联表的字段 解决办法把with换成join,field和join顺序无所谓 * @par ...
- python中的负数取模问题(一个大坑)
先来看一段代码 这是什么情况?为什么会出现这种结果.我们再来看看其它语言的执行结果 我们用golang.js.c分别算了一下,结果得到的结果都是一致的,但是python为啥不一样呢? 其实之所以这么做 ...
- Galera Cluster 实现mysql的高可用 (Percona XtraDB Cluster)
Galera Cluster 实现mysql的高可用 (Percona XtraDB Cluster) # 基础搭建 # 设备:三台主机 192.168.47.101 192.168.47.102 1 ...
- 简单粗暴 每个servlet之前都插入一段代码解决 乱码问题
response.setHeader("content-type", "text/html;charset=UTF-8"); response.setChara ...
- 自己实现strcat函数
问题:自己实现一个strcat_s函数,要和C语言库函数的strcat函数完成同样的功能. (1) 函数原型 char *strcat(char *dest, const char *src); (2 ...
- [Nginx]子目录反向代理kibana并添加basic认证
背景 服务器ip:192.168.1.2 安装软件 nginx kibana(默认端口5601) 实现方案:访问http://192.168.1.2/kibana 即可访问到kibana后端,同时需要 ...
- string::compare
1. compare string (1) 4int compare (const string& str) const noexcept; substrings (2) int compar ...
- 点双联通分量(BCC)的正确姿势
Tarjan求点双连通分量 - 李昊哲