Nodejs 谁给一个ftp demo nodeftp jsftp都行 但是得包括上传下载哦 列出文件列表
Nodejs 谁给一个ftp demo nodeftp jsftp都行 但是得包括上传下载哦 列出文件列表
当然可以。下面是一个使用 jsftp
库的 Node.js FTP 示例,其中包括上传、下载文件以及列出目录的功能。
首先,你需要安装 jsftp
库。可以通过 npm 安装:
npm install jsftp
接下来,创建一个名为 ftpDemo.js
的文件,并添加以下代码:
const JsFtp = require('jsftp');
// 创建一个 JsFtp 实例
const Ftp = new JsFtp({
host: "your-ftp-host", // FTP 主机地址
port: 21, // FTP 端口,默认为 21
user: "your-username", // 用户名
pass: "your-password" // 密码
});
// 列出指定目录中的文件
function listFiles(directory) {
Ftp.list(directory, (err, res) => {
if (err) throw err;
console.log("文件列表:");
res.forEach(file => {
console.log(file.name);
});
});
}
// 下载文件
function downloadFile(remotePath, localPath) {
const fileStream = Fs.createWriteStream(localPath);
Ftp.get(remotePath, (stream) => {
stream.pipe(fileStream);
}).on('close', () => {
console.log(`文件已成功下载到 ${localPath}`);
});
}
// 上传文件
function uploadFile(localPath, remotePath) {
const fileStream = Fs.createReadStream(localPath);
Ftp.put(fileStream, remotePath, (err) => {
if (err) throw err;
console.log(`文件已成功上传到 ${remotePath}`);
});
}
// 使用示例
listFiles("/"); // 列出根目录下的文件
downloadFile("/remote/file.txt", "./local/file.txt"); // 下载远程文件到本地
uploadFile("./local/newfile.txt", "/remote/newfile.txt"); // 上传本地文件到远程
代码解释
- 引入库:首先通过
require('jsftp')
引入jsftp
库。 - 初始化 FTP 实例:使用你的 FTP 服务器信息(主机地址、端口、用户名和密码)创建一个
JsFtp
实例。 - 列出文件:定义一个
listFiles
函数,用于列出指定目录中的文件。调用Ftp.list
方法并传入目录路径作为参数。 - 下载文件:定义一个
downloadFile
函数,用于从 FTP 服务器下载文件到本地。使用Fs.createWriteStream
创建一个可写流,将 FTP 文件流的数据写入到该流中。 - 上传文件:定义一个
uploadFile
函数,用于将本地文件上传到 FTP 服务器。使用Fs.createReadStream
创建一个读取流,并将其数据通过Ftp.put
方法上传到 FTP 服务器。 - 测试代码:最后,通过调用这些函数来测试功能是否正常工作。
希望这段代码能满足您的需求!
这种事应该让专业的来,下面的是遍历一个目录获得完整文件列表的例子,下载我就不说了 require(‘child_process’).exec(‘wget -r -np --spider -R robots.txt ftp://user:pswd@ftpsite.com/folder/’, function(err, stdout, stderr){});
能详细点么? 看不懂 !~
当然可以。这里提供一个使用 jsftp
库的 Node.js FTP 客户端示例代码,包括上传、下载文件以及列出文件列表的功能。
首先,你需要安装 jsftp
库:
npm install jsftp
然后,你可以使用以下代码实现所需功能:
const JsFtp = require('jsftp');
const fs = require('fs');
// 创建一个JsFtp实例
const Ftp = new JsFtp({
host: "ftp.example.com",
port: 21,
user: "username",
pass: "password"
});
// 列出指定目录中的文件
Ftp.list("/path/to/directory", (err, res) => {
if (err) throw err;
console.log("Files in the directory:", res);
});
// 上传文件到FTP服务器
const filePath = '/local/path/to/file.txt';
const remotePath = '/remote/path/to/file.txt';
fs.readFile(filePath, (err, data) => {
if (err) throw err;
Ftp.put(data, remotePath, (err) => {
if (err) throw err;
console.log("File uploaded successfully.");
});
});
// 下载文件从FTP服务器
const downloadFilePath = '/remote/path/to/downloadedfile.txt';
const localDownloadPath = '/local/path/to/downloadedfile.txt';
Ftp.get(downloadFilePath, fs.createWriteStream(localDownloadPath), (err) => {
if (err) throw err;
console.log("File downloaded successfully.");
});
解释
-
创建JsFtp实例:通过传递必要的连接参数(主机名、端口、用户名和密码)创建一个
JsFtp
实例。 -
列出文件:使用
list
方法列出远程目录中的文件,并将结果打印出来。 -
上传文件:使用
fs
模块读取本地文件,然后使用put
方法将文件数据上传到FTP服务器上的指定路径。 -
下载文件:使用
get
方法从FTP服务器下载文件,并将其保存到本地指定路径。
确保替换 host
, port
, user
, pass
, /path/to/directory
, /local/path/to/file.txt
, /remote/path/to/file.txt
, /remote/path/to/downloadedfile.txt
和 /local/path/to/downloadedfile.txt
这些值以匹配你的实际需求。