common- net3.6.jar ftpclient 的listFiles()返回0

今天有需要做一个FTP 的客户端,下载服务器的文件夹内的所有文件,一个一个的用get 下载显然不现实、在网上收到了common- net 。于是利用这个jar 包,提供的ftp 实现来下载。但是仿造网上别人的Demo 来写了一个。就是在ftpclient.listFiles() 这个方法执行时就是没有返回数据( 长度始终为0 )。尽管文件夹内有文件。服务器 : solaris客户端 : linuxcommons-net : commons-net-3.6.jarps: 尽管网上说由于时间中包含中文的问题可能引起这样的问题,或者在listFiles 时要先进入被动模式。但都是针对的是Linux 服务器来说的。上面两种都尝试了, listFiles() 返回数组长度还是0以下是实现代码,在linux服务器测试ftpclient.listFiles()可以正常执行返回。图片说明

可以考虑升级common- net3.6.jar的版本


 package downfile;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

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

public class Downloader {

    private FTPClient ftp = null; 
    private String server;  
    private String uname;  
    private String password;  
    private int port = 21;  

    public Downloader(String server, int port, String uname, String password) {
        this.server = server;  
        if (this.port > 0){  
            this.port = port;  
        }  
        this.uname = uname;  
        this.password = password;  
        //初始化  
        ftp = new FTPClient();
    }

    /** 
     * 连接FTP服务器 
     *  
     * @param server 
     * @param uname 
     * @param password 
     * @return 
     * @throws Exception 
     */  
    public FTPClient connectFTPServer() throws Exception {  
        try {  
            ftp.configure(getFTPClientConfig());  
            ftp.connect(this.server, this.port);  
            if (!ftp.login(this.uname, this.password)) {  
                ftp.logout();  
                ftp = null;  
                return ftp;  
            }  

            // 文件类型,默认是ASCII  
            ftp.setFileType(FTPClient.BINARY_FILE_TYPE);  
            ftp.setControlEncoding("GBK");  
            // 设置被动模式  
            ftp.enterLocalPassiveMode();  
            ftp.setConnectTimeout(2000);  
            ftp.setBufferSize(1024);  
            // 响应信息  
            int replyCode = ftp.getReplyCode();  
            if ((!FTPReply.isPositiveCompletion(replyCode))) {  
                // 关闭Ftp连接  
                closeFTPClient();  
                // 释放空间  
                ftp = null;  
                throw new Exception("登录FTP服务器失败,请检查![Server:" + server + "、"  
                        + "User:" + uname + "、" + "Password:" + password);  
            } else {  
                return ftp;  
            }  
        } catch (Exception e) {  
            ftp.disconnect();  
            ftp = null;  
            throw e;  
        }  
    }  

    /** 
     * 配置FTP连接参数 
     *  
     * @return 
     * @throws Exception 
     */  
    public FTPClientConfig getFTPClientConfig() throws Exception {  
        String systemKey = FTPClientConfig.SYST_NT;  
        String serverLanguageCode = "zh";  
        FTPClientConfig conf = new FTPClientConfig(systemKey);  
        conf.setServerLanguageCode(serverLanguageCode);  
        conf.setDefaultDateFormatStr("yyyy-MM-dd");  
        return conf;  
    }  

    /** 
     * 从FTP服务器下载文件 
     *  
     * @param remotePath 
     *            FTP路径(不包含文件名) 
     * @param fileName 
     *            下载文件名 
     * @param localPath 
     *            本地路径 
     */  
    public Boolean downloadFile(String remotePath, String fileName,  
            String localPath) throws Exception {  

        BufferedOutputStream output = null;  
        boolean success = false;  
        try {  
            // 检查本地路径  
            this.checkFileExist(localPath);  
            // 改变工作路径  
            if (!this.changeDirectory(remotePath)) {  
                System.out.println("服务器路径不存在");  
                return false;  
            }  
            // 列出当前工作路径下的文件列表  
            List<FTPFile> fileList = this.getFileList();  
            if (fileList == null || fileList.size() == 0) {  
                System.out.println("服务器当前路径下不存在文件!");  
                return success;  
            }  
            for (FTPFile ftpfile : fileList) {  
                System.out.println(ftpfile.getName());
            }  
        } catch (Exception e) {  
            throw e;  
        } finally {  
            if (output != null) {  
                output.close();  
            }  
        }  
        return success;  
    } 

    /** 
     * 从FTP服务器获取文件流 
     *  
     * @param remoteFilePath 
     * @return 
     * @throws Exception 
     */  
    public InputStream downloadFile(String remoteFilePath) throws Exception {  

        return ftp.retrieveFileStream(remoteFilePath);  
    }  

    /** 
     * 检查本地路径是否存在 
     *  
     * @param filePath 
     * @return 
     * @throws Exception 
     */  
    public boolean checkFileExist(String filePath) throws Exception {  
        boolean flag = false;  
        File file = new File(filePath);  
        if (!file.exists()) {  
            throw new Exception("本地路径不存在,请检查!");  
        } else {  
            flag = true;  
        }  
        return flag;  
    } 

    /** 
     * 获取FTP服务器[当前工作路径]下的文件列表 
     *  
     * @param path 
     * @return 
     * @throws Exception 
     */  
    public List<FTPFile> getFileList() throws Exception {  

        List<FTPFile> ftpfiles = Arrays.asList(ftp.listFiles());  

        return ftpfiles;  
    }  
    /** 
     * 改变FTP服务器工作路径  
     *  
     * @param remoteFoldPath 
     */  
    public Boolean changeDirectory(String remoteFoldPath) throws Exception {  

        return ftp.changeWorkingDirectory(remoteFoldPath);  
    }

    /** 
     * 关闭FTP连接 
     *  
     * @param ftp 
     * @throws Exception 
     */  
    public void closeFTPClient(FTPClient ftp) throws Exception {  

        try {  
            if (ftp.isConnected())  
                ftp.logout();  
                ftp.disconnect();  
        } catch (Exception e) {  
            throw new Exception("关闭FTP服务出错!");  
        }  
    }  

    /** 
     * 关闭FTP连接 
     *  
     * @throws Exception 
     */  
    public void closeFTPClient() throws Exception {  

        this.closeFTPClient(this.ftp);  
    }  
}


图片说明

检测一下目标 目录文件的 权限