作者专注于Java、架构、Linux、小程序、爬虫、自动化等技术。 工作期间含泪整理出一些资料回复 【java】【黑客】【爬虫】【小程序】【面试】等关键字免费获取资料。


前言

连接ftp服务器方式:​​通过Java代码连接ftp服务器​​ 

实现

下载文件实现方式:

public void downloadFile(String localPath, String ftpPath, String fileName) {
if (!localPath.endsWith("/")) {
localPath = localPath + "/";
}
FTPClient client = ftpClientManager.getClient();
File localFile = new File(localPath + fileName);
try {
client.changeWorkingDirectory(ftpPath);
OutputStream is = new FileOutputStream(localFile);
client.retrieveFile(fileName, is);
is.close();
logger.info("success to download file: " + localFile.getAbsolutePath());
} catch (Exception e) {
logger.error("faild to download file " + localFile.getAbsolutePath() + " because " + e.getMessage());
System.exit(0);
}
}