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);
Node.js使用ftp连接远程ftp服务器枚举和下载文件示例的更多相关文章
- Linux下不借助工具实现远程linux服务器上传下载文件
# Linux下不借助工具实现远程linux服务器上传下载文件 ## 简介 - Linux下自带ssh工具,可以实现远程Linux服务器的功能- Linux下自带scp工具,可以实现文件传输功能 ## ...
- Centos下ftp协议连接远程ftp server主机
环境说明 [root@Check3 ~]# cat /etc/redhat-release CentOS release 6.9 (Final) [root@Check3 ~]# uname -a L ...
- Notepad++ 连接远程 FTP 进行文件编辑
一.下载安装 Notepad++ 1.下载 Notepad++ : https://pan.baidu.com/s/1o7VrS4y 密码 : ck8a 2.安装 Notepad++ 2.1.勾选所有 ...
- paip.最好的脚本语言node js 环境搭建连接mysql
paip.最好的脚本语言node js 环境搭建连接mysql #====下载node...走十一个exe..容易的.. 1 #0----Hello world .js 2 #---------模 ...
- [转]用Node.js创建自签名的HTTPS服务器
用Node.js创建自签名的HTTPS服务器 创建自己的CA机构 创建服务器端证书 创建客户端证书 将证书打包 创建自己的CA机构 为CA生成私钥 openssl genrsa -out ca-key ...
- 前端使用node.js的http-server开启一个本地服务器
前端使用node.js的http-server开启一个本地服务器 在写前端页面中,经常会在浏览器运行HTML页面,从本地文件夹中直接打开的一般都是file协议,当代码中存在http或https的链接时 ...
- 如何配置pl/sql 连接远程oracle服务器
在下边的两种情况下,如何配置pl/sql 连接远程oracle服务器 1)在客户端不装oracle 客户端也不装服务器,能否配置pl/sql 连接远程oracle服务器,如何配置,请给出详细的文档说明 ...
- Node.js创建自签名的HTTPS服务器
https://cnodejs.org/topic/54745ac22804a0997d38b32d 用Node.js创建自签名的HTTPS服务器 发布于 4 年前 作者 eeandrew 6 ...
- 搭建linux远程服务器和传输下载文件
其实,将ubuntu系统设置为服务器很简单,只需要开启ssh服务就可以了.开启了ssh服务以后,其它电脑就可以通过ssh登录你的这台ubuntu服务器.SSH分客户端openssh-client和op ...
随机推荐
- python 查询每周最后一个工作日
背景: 做定时任务时,一般都是写死每周五XXXXX,但有时遇到节假日的情况,周五可能不是本周最后一个工作日 代码如下: import urllib2,datetime,json nowTime = d ...
- 六、用DataContractSerialize类序列化XML
一.层次结构 基类:XmlObjectSerializer 派生类: DataContractSerializer NetDataContractSerializer DataContractJson ...
- Mysql 安装,及其闪退的问题解决
1.下载 Mysql 64下载链接(华军软件) http://soft.onlinedown.net/soft/3573.htm 2.安装 2.1 傻瓜式安装 不再赘述,略过. 2.2安装完成打开闪退 ...
- learning scala view collection
The view method will create a non-strict version of the collection which means that the elements of ...
- loj #2316
最短路 + 记忆化 记忆化搜索更容易实现 #include <iostream> #include <cstdio> #include <algorithm> #i ...
- Bzoj 1280: Emmy卖猪pigs
1280: Emmy卖猪pigs Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 279 Solved: 182[Submit][Status][Dis ...
- 搭建自己的博客(二十二):通过ajax提交评论信息,并增加公式编辑功能
编辑功能使用到了ckeditor的MathJax组件.ajax提交评论可以不用刷新浏览器. 1.变化的部分
- 学习C/C++的简单方法
如何学习C++呢.C和C++是很多专业的必修课,尤其对计算机专业来说,更是重中之重.C++语言是早期发展的高级语言,具备执行速度快,语法优美等特点.是底层高效率系统的首选开发语言.今天就和大家分享一下 ...
- nasm汇编讲解
一.什么是nasm汇编 nasm使用在windows.linux等系统下的汇编. 二.语法介绍 2.1 nasm 是区分大小写 例如:符号 foo 与 FOO 是两个不同的标识符. 2.2 内存操作数 ...
- 函数第二部分:为什么说动态参数是没有计划好的参数-Python基础前传(11)
动态参数1-一个星号变元组 动态参数存在的意义? 函数的作者有时候也不知道这个函数到底需要多少个参数,这时候动态参数就有存在的意义了 动态参数创建-加* 底层原理是:把数值型或其他数据类型变成了元组类 ...