hsp8712 2012-08-14 00:03
浏览 412
已采纳

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;
}

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

  • 写回答

3条回答 默认 最新

  • qgm168 2012-08-20 16:45
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键