apache commons-net ftpClient listFiles方法诡异问题

问题:使用ftpClient listFiles方法来判断文件的存在性,代码如上,自己的测试机上是ok的,但现网出现不稳定现象,2000个路径验证,循环验证,第一次出现十几个 验证不存在(其实文件存在,权限也没问题),第二次再验证,不存在的个数会增加,以此类推,但过几分钟,再执行验证,不存在的个数又会降下来,感觉很诡异。

 

public static boolean fileExist(String filepath)
{
    FTPClient ftpClient = ftpContext.get();
    String path = "";

    if(filepath.startsWith("/"))
    {
        path = filepath.substring(1,filepath.length());
    }
    else
    {
        path = filepath;
    }

    logger.warn("this full file path is ["+FtpUtil.currentWorkspace(false)+"/"+path+"] ");

    try {
        path = new String(path.getBytes(ENCODE_TYPE),"ISO-8859-1") ;
    } catch (UnsupportedEncodingException e1) {
        logger.error(e1.getMessage());
    }

    // filepath该路径对应的文件数组
    FTPFile[] files = null;

    try {
        files = ftpClient.listFiles(path,
            new FTPFileFilter() {
                public boolean accept(FTPFile ftpFile) {
                    return ftpFile.isFile();
                }
            }
        );
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    }

    // 文件数组为空或长度小于等于0,代表文件不存在
    if(files == null || files.length <= 0)
    {
        return false;
    }

    return true;
}

 麻烦各位给点建议,谢谢啦^_^!

在login()后加上这句
ftpClient.enterLocalPassiveMode();//important

是不是文件名有中文?

我之前也写过类似的,最后尝试直接使用listFile的入参为全路径测试,貌似是可以的,不妨试试,代码如下:
[code="java"]
public static boolean exists(String path) {
FTPClient client = ftpContext.get() ;
if(client == null) {
return false ;
}
try {
FTPFile[] files = client.listFiles(ROOT_PATH.get() + new String(path.getBytes(ENCODE_TYPE),"ISO-8859-1")) ;
return files.length > 0 ;
} catch (IOException e) {
logger.error("Exception occured when check the FTP File["+path+"] is exist : " + e.getMessage()) ;
}
return false ;
}
[/code]