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 ...
随机推荐
- hadoop-hive的内表和外表
--创建内表create table if not exists employee(id int comment 'empoyeeid',dateincompany string comment 'd ...
- 如何解决Win10电脑网速慢的问题?
很多Win10系统用户反映自己的电脑配置并不算低,却比有些旧电脑上网速度还要慢,不知道是什么原因. 其实,除了运营商网速的原因外,我们的电脑设置也会对网速有所影响.下面好系统U盘启动就来介绍一个Win ...
- Intellij IDEA显示调用时序图插件SequenceDiagram
1 推荐一个插件SequenceDiagram可以自动生成项目调用的时序图 安装好之后重启idea即可 2 使用,找到你要查询的方法
- DNS负载均衡与NGINX负载均衡策略
负载均衡是指的是把请求均匀的分摊到多个服务器上处理.一般常见的负载均衡有两种:①客户端与反向代理服务器之间的DNS负载均衡②反向代理服务器与应用服务器之间的负载均衡(这种负载均衡有很多,可以是webl ...
- Centos 安装 kubectl kubelet kubeadm
cat <<EOF > /etc/yum.repos.d/kubernetes.repo [kubernetes] name=Kubernetes baseurl=https://m ...
- Some notes of The Peter Principle
管理学读书笔记之彼得原理 chapter0 绪论为什么有这么多不称职现象?姑息错误或纵容懒惰.偶然事故都不是问题根源,关键是我们的社会为不称职现象提供土壤,并对其有所鼓励. chapter1 彼得原理 ...
- mysql8.0出现的2059 - authentication plugin 'caching_sha2_password' -navicat连接异常问题解决
转载自:https://blog.csdn.net/qq_24664619/article/details/80263546 刚装了mysql8.0,用navicat登陆不了,会出现2059错误,只能 ...
- C语言常用库函数
一.数学函数 调用数学函数时,要求在源文件中包下以下命令行: #include <math.h> 函数原型说明 功能 返回值 说明 int abs( int x) 求整数x的绝对值 计算结 ...
- POI导出Excel不弹出保存提示_通过ajax异步请求(post)到后台通过POI导出Excel
实现导出excel的思路是:前端通过ajax的post请求,到后台处理数据,然后把流文件响应到客户端,供客户端下载 文件下载方法如下: public static boolean downloadLo ...
- MQTT 入门介绍——菜鸟教程
一.简述 MQTT(Message Queuing Telemetry Transport,消息队列遥测传输协议),是一种基于发布/订阅(publish/subscribe)模式的"轻量级& ...