SFTP 路径格式与本地路径格式

SFTP path format versus local path format

我正在编写一些 Java 代码(使用 JSch 库)到 SFTP 到远程 Windows 机器并将文件复制到我的本地 Windows 文件夹.

在远程机器上指定文件路径时,我不得不以 /C/temp/myfile.txt 格式指定路径,而不是 C:\temp\myfile.txt.

问题:

  1. 你能告诉我这是什么格式吗?有名字吗?

  2. 你能告诉我路径是否可以使用任何其他格式吗?我希望能够将路径指定为 C:\temp\myfile.txt,因此用户根本不知道文件是否是从远程计算机 SFTP 生成的。我猜我必须在路径中包含 parse/split/replace 个字符,因为 FTP get 命令无法理解此路径格式。

谢谢。

SFTP 协议规定 / 必须用作路径分隔符。

参见SFTP specification

This protocol represents file names as strings. File names are assumed to use the slash ('/') character as a directory separator.

File names starting with a slash are "absolute", and are relative to the root of the file system. Names starting with any other character are relative to the user's default directory (home directory). Note that identifying the user is assumed to take place outside of this protocol.


SFTP 服务器将服务器文件系统上的真实 OS 特定路径映射到符合 SFTP 规范的语法。

您必须使用服务器作者选择使用的语法。

所以在所有情况下,您都必须将 \ 映射到 /

您可能需要删除 :。尽管这实际上是可配置的。许多(特别是 Windows)SFTP 服务器都有一个配置,允许您将虚拟 SFTP 路径映射到真实路径。在这种情况下,您可以将 /C:/ 映射到 C:\.

无论如何,你的问题实际上不是关于JSch,更不用说Java,而是关于你的SFTP服务器。

另请参阅 my WinSCP SFTP 客户端的常见问题解答:How do I change drive on the remote panel?