假设URI方案为“file”。同时假设路径以“.”开头。
一个示例路径是'./.bashrc'。完整的URI是什么样子?对我来说,'file://./.bashrc'看起来很奇怪。
一个示例路径是'./.bashrc'。完整的URI是什么样子?对我来说,'file://./.bashrc'看起来很奇怪。
file://localhost/absolute/path/to/file [ok]
file:///absolute/path/to/file [ok]
file://file_at_current_dir [no way]
也不是这个:
file://./file_at_current_dir [no way]
我刚刚确认了通过Python的urllib2.urlopen()方法
更多细节请参考http://en.wikipedia.org/wiki/File_URI_scheme:
"file:///foo.txt" is okay, while "file://foo.txt" is not,
although some interpreters manage to handle the latter
file_at_current_dir
的文件。我在此示例中使用Python来强调“在构建字符串时没有当前目录”的事实。 - amcgregor如果路径中包含'.'或'..',且没有根路径作为前缀,那么就无法使用完整的文件URI。无论你使用'file://./.bashrc'还是'file:///./.bashrc'这些路径都是没有意义的。如果你想使用相对链接,请不要使用协议/权限部分:
<a href="./.bashrc">link</a>
如果您想使用完整的URI,必须指定相对路径的根目录:
<a href="file:///home/kindrik/./.bashrc">link</a>
根据RFC 3986规定
The path segments "." and "..", also known as dot-segments, are
defined for relative reference within the path name hierarchy. They
are intended for use at the beginning of a relative-path reference
(Section 4.2) to indicate relative position within the hierarchical
tree of names. This is similar to their role within some operating
systems' file directory structures to indicate the current directory
and parent directory, respectively. However, unlike in a file
system, these dot-segments are only interpreted within the URI path
hierarchy and are removed as part of the resolution process (Section
5.2).
The complete path segments "." and ".." are intended only for use
within relative references (Section 4.1) and are removed as part of
the reference resolution process (Section 5.2). However, some
deployed implementations incorrectly assume that reference resolution
is not necessary when the reference is already a URI and thus fail to
remove dot-segments when they occur in non-relative paths. URI
normalizers should remove dot-segments by applying the
remove_dot_segments algorithm to the path, as described in Section 5.2.4.
The complete path segments "." and ".." are intended only for use
within relative references (Section 4.1) and are removed as part of
the reference resolution process (Section 5.2)
RFC 3986 描述了如何从 URI 中移除这些 "." 和 ".." 的算法。
$PWD
包含了像C:/Users/Joshua Pinter/
这样的空格,那么路径将无效。需要以某种方式进行转义。 - Joshua Pinterfile://${PWD// /\\ }/relative/path
。 - Steve Goossens+
。此外还要注意,URI允许UTF-8,因此许多Unicode字符(如重音字母、表情符号、§等符号)实际上根本不需要编码。另外需要注意的是:未编码的URI将被用户代理自动编码,因此作为字符串(包含空格)使用可能是可以的。(空格会导致shell扩展/进程参数列表构建问题。) - amcgregor//
)的§3.2 Authority部分。然后注意到该RFC中定义的 file:
方案的第 2 节(https://tools.ietf.org/html/rfc8089#section-2),该节仅允许 path-absolute
。即使某些系统通过约定允许它们,但相对 file:
URI 在技术上并不存在。 - amcgregor我不知道您的使用情况。
在我的Node.js代码中,我有类似的需求,因此当我需要一个相对于工作目录的文件url时,我会按照以下方式创建url...
const url = "file://" + process.cwd() + "/" + ".bashrc";
URI始终是绝对的(除非它们是相对URI,这是一种不带模式的不同实体)。这是因为URI是一种服务器-客户端技术,引用服务器的工作目录就没有意义了。反过来,引用文件系统在服务器-客户端上下文中也没有意义。尽管如此,《RFC 8089》仅允许使用绝对路径:
路径组件表示文件系统中文件的绝对路径。
但是,如果我要假设一种非标准扩展,则会选择以下语法:
file:file.txt
file:./file.txt
file://<主机的完全限定域名>/路径
和本地路径file:/路径
,file://localhost/路径
和file:///路径
。由于我们几乎肯定是在尝试指定一个本地相对路径(即可由"局部文件系统API"访问),且因.
既不是FQDN也不是hostname,所以最简单的file:
方案+方案特定部分URI语法最有意义。file:./file.txt
作为相对路径的修复方法适用于通用 URL! - Mohammad Kananfile://`pwd`/relative-path
file://`pwd`/.bashrc
有一个解决方法可能会有所帮助。
如果在开发时您只能指定文件的相对路径,但需要一个URL(需要知道绝对路径),请使用以下代码:
new File("relative/path/to/file").toURI().toURL();
这样,您将获得一个指向相对路径中文件的URL。
%20
表示空格);根据应用程序,您可能需要将转义字符替换为其实际表示。 - sleblancfile://
支持。https://bugs.chromium.org/p/chromium/issues/detail?id=1299624 - 9pfs