public void downloadFtpFiles() throws ParseException {
String localDirs = PropertiesUtils.getProperties(Constants.LOCAL_DIR);
String ftplocalDir = PropertiesUtils.getProperties(Constants.ftplocal_dir);
FTPClient ftp=ftpConfig.connectFtpServer();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//注意月份是MM
String time= simpleDateFormat.format(new Date());
String dirName=DateUtil.getBeforeDayYMD(time);
try {
//获取ftp目录下的文件夹名
ftp.changeWorkingDirectory(ftp上路径);
FTPFile[] fs = ftp.listFiles();
for (FTPFile ff:fs) {
String fileName = ff.getName();
boolean flag = false;
// 切换目录,若当前是目录则返回true,否则返回false。
flag = ftp.changeWorkingDirectory(fileName);
if (flag) {
ftp.changeToParentDirectory();
}
if (flag) {
String localDir = 本地地址+ fileName;
logger.debug("开始下载" + fileName+dirName + "目录,本地目录为" + localDir);
File temp = new File(localDir);
if (!temp.exists()) {
temp.mkdirs();
}
//进入下载主目录。
ftp.changeWorkingDirectory(fileName);
// for (String dirName : Dirs) {
//遍历需要下载的文件夹 1、切换ftp远程目录,进入需要下载文件的目录.
boolean fl = ftp.changeWorkingDirectory(dirName);
if (fl) {
String localSubDirName = 本地地址+ File.separator + dirName;
File localSubDir = new File(localSubDirName);
if (!localSubDir.exists()) {
//当本地目录不存在时,创建本地目录.
localSubDir.mkdirs();
}
//获取该目录文件列表.
String[] fileNames = ftp.listNames();
if (null != fileNames && fileNames.length > 0) {
for (String fn : fileNames) {
File localFile = new File(localSubDirName + File.separator + fn);
if (!localFile.exists()) {
FileOutputStream fos = new FileOutputStream(localFile);
ftp.retrieveFile(fn, fos);
}
}
// updateNotifyDate(fileName, dirName);
ftp.changeToParentDirectory();
}
}
// }
ftp.changeToParentDirectory();
logger.info(fileName+dirName + "下载成功!");
}
// }
// }
// }
}
ftp.logout();
} catch (Exception e) {
e.printStackTrace();
logger.error("异常!"+e);
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
logger.error("io异常!"+ioe);
}
}
}
}
从FTP服务器下载指定文件夹中的所有最近更新的csv文件
/**
* Description: 从FTP服务器下载指定文件夹中的所有最近更新的csv文件
* @param url FTP服务器hostname
* @param port FTP服务器端口
* @param username FTP登录账号
* @param password FTP登录密码
* @param remotePath FTP服务器上的相对路径
* @param localPath 下载后保存到本地的路径
* @param dateStr 下载的是哪一天的数据
* @return 下载的文件数
*/
public static int downFileByPath(String url, int port, String username, String password, String remotePath, String localPath, String dateStr) {
int filesSize = 0;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url, port);
//如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login(username, password);//登录
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return filesSize;
}
boolean result = ftp.changeWorkingDirectory(remotePath);//转移到FTP服务器目录
if(!result){ //转移到指定目录失败
logger.error("找不到指定的ftp服务器目录:"+remotePath);
return filesSize;
}
FTPFile[] fs = ftp.listFiles();
String ffName = "";
for(FTPFile ff:fs){
ffName = ff.getName();
if(ffName.endsWith(".csv") && ffName.contains(dateStr)){
File localFile = new File(localPath+"/"+ffName);
OutputStream is = null;
File path = new File(localPath);
if(!((new File(localPath)).exists())){
path.mkdirs();
}
is = new FileOutputStream(localFile);
ftp.retrieveFile(ffName, is);
logger.debug(ffName+" 下载成功!");
filesSize++;
}
}
ftp.logout();
} catch (IOException e) {
//e.printStackTrace();
logger.error("异常!"+e);
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
logger.error("io异常!"+ioe);
}
}
}
return filesSize;
}