zoukankan      html  css  js  c++  java
  • 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());
    }

    }
    }

  • 相关阅读:
    CSUSTOJ-伊井野弥子是风纪委员(简单BFS)
    CSUSTOJ-石上优不想留级(一维坐标三分及思维解法)
    CSUSTOJ-哈希的纸团(mid思维)
    CSUSTOJ-辉夜大小姐想被猜中(简单暴力)
    CSUSTOJ-藤原书记想要探病(简单矩阵快速幂)
    CSUSTOJ-石上优想要逃离(STL+思维暴力)
    CSUSTOJ-白银御行想展示(思维题)
    CSUSTOJ-藤原书记的佩斯(简单数学)
    CSUSTOJ-白银探病篇(简单思维)
    Odoo发邮件被服务器退回
  • 原文地址:https://www.cnblogs.com/zhangzhiqin/p/10268542.html
Copyright © 2011-2022 走看看