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