NutzCN Logo
问答 请教一个关于FTP下载文件的问题,ftpClient.listFiles() 一直返回空值
发布于 2164天前 作者 qq_6ba0af6a 7528 次浏览 复制 上一个帖子 下一个帖子
标签:

连接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(); 也没用

7 回复

把slf4j-jdk14删掉

删了这个jar 不就报错了吗
Exception in thread "main" java.lang.Error: Unresolved compilation problem:

at com.efraiser.common.util.FTPUtil.main(FTPUtil.java:153)

还是输出这个

111111
3333

话说, 你怎么传个本地路径C:\DCWORK\DC4DW\ReportFile\2018\Q2

服务器怎么可能暴露个本地路径给你访问??? 用filezilla看路径啦

嗯嗯,受教了,就是这个问题,谢谢

添加回复
请先登陆
回到顶部