ftp java listfiles_请教一个关于FTP下载文件的问题,ftpClient.listFiles() 一直返回空值...

连接FTP 代码

public static boolean connectAndLogin(String host, int port, String ftpUser, String ftpPwd) {

if (ftpClient == null) {

ftpClient = new FTPClient();

}

try {

ftpClient.connect(host, port);

if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {

if (ftpClient.login(ftpUser, ftpPwd)) {

ftpClient.setBufferSize(102400);

return true;

} else {

logger.error("登录ftp失败,请核对用户名密码");

}

} else {

logger.error("ftp连接失败!!");

}

} catch (Exception e) {

e.printStackTrace();

logger.error("连接ftp异常:" + e.getLocalizedMessage());

}

return false;

}

检查是否包含文件

public static boolean checkFTPFileExists(String filePath, final String fileName) {

try {

FTPFile[] fs = ftpClient.listFiles(filePath, new FTPFileFilter() {

@Override

public boolean accept(FTPFile ftpFile) {

if (ftpFile.getName().equals(fileName)) {

return true;

}

return false;

}

});

if (fs.length > 0) {

return true;

}

} catch (IOException e) {

e.printStackTrace();

logger.error("检查FTP文件[" + fileName + "]是否存在是异常:" + e.getLocalizedMessage());

}

return false;

}

main函数

public static void main(String[] args) {

if(FTPUtil.connectAndLogin("192.168.83.130", 21, "samm", "samm")) {

System.err.println("111111");

if(FTPUtil.checkFTPFileExists("C:\\DCWORK\\DC4DW\\ReportFile\\2018\\Q2", "4f0905fdbd154f2681ac54df2ed1579a_1.xls")) {

System.err.println("222222222");

}else {

System.err.println("3333");

}

}else {

System.err.println("4444");

}

}

返回结果

SLF4J: Class path contains multiple SLF4J bindings.

SLF4J: Found binding in [jar:file:/E:/github/com.efraiser.sam/project/WebContent/WEB-INF/lib/slf4j-jdk14-1.7.9.jar!/org/slf4j/impl/StaticLoggerBinder.class]

SLF4J: Found binding in [jar:file:/E:/github/com.efraiser.sam/project/WebContent/WEB-INF/lib/logback-classic-1.0.6.jar!/org/slf4j/impl/StaticLoggerBinder.class]

SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.

SLF4J: Actual binding is of type [org.slf4j.impl.JDK14LoggerFactory]

111111

3333

为什么一直得不到值,设置了ftpClient.enterLocalPassiveMode(); 也没用

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值