Node.js使用ftp连接远程ftp服务器枚举和下载文件示例
示例代码:
var Ftp = require('ftp');
var fs = require('fs');
var path = require('path');
// 首先判断参数中是否包含{dateString}
var args = process.argv.splice(2);
if (args.length < 1) {
console.error('usage: node ftpFilesGetter.js [dateString]');
process.exit(1);
}
var dateString = args[0];
// 根据dateString获得nextDay
var year = parseInt(dateString.substring(0,4)),
month = parseInt(dateString.substring(4,6)),
day = parseInt(dateString.substring(6,8));
var lastday;
if (month == 2) {
if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0) lastday = 29;
else lastday = 28;
} else if ([1, 3, 5, 7, 8, 10, 12].indexOf(month) != -1) lastday = 31;
else lastday = 30;
if (day < lastday) day ++;
else {
month ++;
if (month > 12) {
month = 1;
year ++;
}
}
if (month < 10) month = '0' + month;
if (day < 10) day = '0' + day;
var nextDay = `${year}${month}${day}`;
// 判断下载文件所在的文件夹是否存在,如果不存在则创建
var rootDir = 'D:/test'; if (fs.existsSync(rootDir) == false) fs.mkdirSync(rootDir);
if (fs.existsSync(`${rootDir}/report_log`) == false) fs.mkdirSync(`${rootDir}/report_log`);
if (fs.existsSync(`${rootDir}/report`) == false) fs.mkdirSync(`${rootDir}/report`);
var logDir = `${rootDir}/report_log/${dateString}`; if (fs.existsSync(logDir) == false) fs.mkdirSync(logDir);
var nextDayLogDir = `${rootDir}/report_log/${nextDay}`; if (fs.existsSync(nextDayLogDir) == false) fs.mkdirSync(nextDayLogDir);
var dataDir = `${rootDir}/report/${dateString}`; if (fs.existsSync(dataDir) == false) fs.mkdirSync(dataDir);
// 登陆ftp
var connectionProperties = {
host: 'xxx.xxx.xxx.xxx',
user: 'stp_username',
password: 'ftp_password'
};
var ftp = new Ftp();
ftp.on('ready', ()=>{
console.log('connect to ftp ok!');
var needFileList = [];
var callback = function() {
console.log(`2 log files + many data files, total : ${needFileList.length} files!`);
var cnt = 0;
needFileList.forEach(file => {
var filename = file.substring(file.lastIndexOf('/') + 1);
var localFile;
if (file.substring(0,12) == `log/${dateString}`) { // 今天的log
localFile = `${logDir}/${filename}`;
}
else if (file.substring(0,12) == `log/${nextDay}`) { // 明天的log
localFile = `${nextDayLogDir}/${filename}`;
} else { // data
localFile = `${dataDir}/${filename}`;
}
ftp.get(file, (err, stream)=>{
stream.once('close', ()=>{
cnt ++;
console.log(`(${cnt}) download ${file} --> ${localFile} succeed!`);
if (cnt >= needFileList.length) {
console.log('process successfully end!');
process.exit(0);
}
});
stream.pipe(fs.createWriteStream(localFile));
});
});
}
ftp.list(`log/${dateString}`, (err, list)=>{
list.forEach(item => needFileList.push(`log/${dateString}/${item.name}`));
ftp.list(`log/${nextDay}`, (err, list)=>{
list.forEach(item => needFileList.push(`log/${nextDay}/${item.name}`));
ftp.list(`sdata/S-999000`, (err, list)=>{
var sysIdList = list.map(item => { return item.name });
var cnt1 = 0; // cnt1用于记录sysId的个数,当等于sysIdList.length的时候说明遍历完了
var tmpDirList = [];
sysIdList.forEach(sysId => {
ftp.list(`sdata/S-999000/${sysId}`, (err, list)=>{
cnt1 ++;
list.forEach(item => tmpDirList.push(`sdata/S-999000/${sysId}/${item.name}`));
if (cnt1 >= sysIdList.length) {
var cnt2 = 0;
tmpDirList.forEach(tmpDir =>{
ftp.list(`${tmpDir}/${dateString}`, (err, list)=>{
cnt2 ++;
list.forEach(item => needFileList.push(`${tmpDir}/${dateString}/${item.name}`));
if (cnt2 >= tmpDirList.length) {
callback();
}
});
});
}
});
});
});
});
});
});
ftp.connect(connectionProperties);