Node.js 如何使用nodejs从远程服务器复制带有空格和utf8的文件 问题

Node.js 如何使用nodejs从远程服务器复制带有空格和utf8的文件 问题,node.js,shell,escaping,Node.js,Shell,Escaping,我正在尝试构建一个节点应用程序,将本地目录与远程目录进行比较,然后将任何新文件从远程目录复制到本地目录。不幸的是,许多文件都是用户生成的内容,并且包含空格字符。我当前的实现似乎没有正确地转义文件名字符串。如何将这些文件复制到本地环境 目前的执行情况: 电流输出 对于此输入: copyRemoteFile('Screen Shot 2013-12-04 at 1_00_13 PM.png', 'server', '.'); 我得到这个输出: {[错误:命令失败:scp:/path/to/file

我正在尝试构建一个节点应用程序,将本地目录与远程目录进行比较,然后将任何新文件从远程目录复制到本地目录。不幸的是,许多文件都是用户生成的内容,并且包含空格字符。我当前的实现似乎没有正确地转义文件名字符串。如何将这些文件复制到本地环境

目前的执行情况: 电流输出 对于此输入:

copyRemoteFile('Screen Shot 2013-12-04 at 1_00_13 PM.png', 'server', '.');
我得到这个输出:

{[错误:命令失败:scp:/path/to/files/Screen:No>这样的文件或目录 cp:Shot \:没有这样的文件或目录 cp:2013-12-04 \:没有此类文件或目录 cp:at\:没有这样的文件或目录 cp:1\u 00\u 13 \:没有此类文件或目录 cp:PM.png:没有这样的文件或目录 ]killed:false,代码:1,信号:null}'scp:/path/to/files/Screen:没有这样的文件或 目录\ncp:Shot\:没有这样的文件或目录\ncp:2013-12-04\:没有这样的文件或目录\ncp: 在\:无此类文件或目录\ncp:1\u 00\u 13 \:无此类文件或目录\ncp:PM.png:无此类文件 或目录\n'

exec为您生成一个shell,并强制您关心如何逃逸。 请尝试执行文件:

不需要转义。

exec为您生成一个shell,并强制您关心转义。
npm install scp

scp.get({
  file: '~/file.txt', // remote file to grab
  user: 'username',   // username to authenticate as on remote system
  host: 'myServer',   // remote host to transfer from, set up in your ~/.ssh/config
  port: '22',         // remote port, optional, defaults to '22'
  path: '~'           // local path to save to (this would result in a ~/file.txt on the local machine)
});
请尝试执行文件:

不需要逃跑

npm install scp

scp.get({
  file: '~/file.txt', // remote file to grab
  user: 'username',   // username to authenticate as on remote system
  host: 'myServer',   // remote host to transfer from, set up in your ~/.ssh/config
  port: '22',         // remote port, optional, defaults to '22'
  path: '~'           // local path to save to (this would result in a ~/file.txt on the local machine)
});
文件如下:

源代码:

linux命令中的空格必须为引号:

copyRemoteFile('"Screen Shot 2013-12-04 at 1_00_13 PM.png"', 'server', '.');
若您通过web服务器调用exec linux命令,用户可能会使用特殊的文件名攻击您的服务器,您需要引用参数

文件如下:

源代码:

linux命令中的空格必须为引号:

copyRemoteFile('"Screen Shot 2013-12-04 at 1_00_13 PM.png"', 'server', '.');
若您通过web服务器调用exec linux命令,用户可能会使用特殊的文件名攻击您的服务器,您需要引用参数