java查询ftp路径下所有文件名字

package com.company.project.utils;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

import java.io.IOException;
import java.util.ArrayList;

/**
 * 列出FTP服务器上指定目录下面的所有文件
 */
public class FTPListAllFiles {
    public FTPClient ftp;
    public ArrayList<String> arFiles;
    private static final String FAR_SERVER_URL = "10.*.*.4";
    private static final int SERVER_PORT = 21;
    private static final String SERVER_USER = "ftpuser";
    private static final String SERVER_PWD = "123456";
    private static final String path = "/data/ftp/";
    private static final String downloadPath = "";


    /**
     * 登陆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 {
        ftp = new FTPClient();
        //设置超时
        ftp.setConnectTimeout(60 * 60 * 1000);
        //设置编码
        ftp.setControlEncoding("UTF-8");
        this.ftp.connect(host, port);
        if (FTPReply.isPositiveCompletion(this.ftp.getReplyCode())) {
            if (this.ftp.login(username, password)) {
                this.ftp.setControlEncoding("GBK");
                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 String List(String pathName) throws IOException {

        StringBuffer filename = new StringBuffer();
        if (pathName.startsWith("/") && pathName.endsWith("/")) {
            String directory = pathName;
//更换目录到当前目录
            this.ftp.changeWorkingDirectory(directory);
            
            ftp.enterLocalPassiveMode(); //开启被动模式,跟端口是否开放有关,更多详情请百度
            FTPFile[] files = this.ftp.listFiles();
            if (files != null) {
                for (int i = 0; i < files.length; i++) {
                    if (files[i].isFile()) {
                        String n = new String(files[i].getName().getBytes("gbk"), "utf-8");
                        if (i == files.length - 1) {
                            filename.append(n );
                        } else {
                            filename.append(n + ",");
                        }

                    }
                }
            }
        }
        return filename.toString();
    }

    //获取指定文件夹内的文件名称
    public static String getFilenames() {
        String names = "";
        FTPListAllFiles f = new FTPListAllFiles();
        try {
            if (f.login(FAR_SERVER_URL, SERVER_PORT, SERVER_USER, SERVER_PWD)) {
                names = f.List(path);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            try {
                f.disConnection();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return names;
    }

    //测试
    public static void main(String[] args) throws IOException {
        System.out.println(getFilenames());
    }
}

加了个注释:

ftp.enterLocalPassiveMode(); //开启被动模式,跟端口是否开放有关,更多详情请百度
  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值