文件 Uri 方案和相关文件
假设 uri 的方案是“文件”。还假设路径以 .
开头,
示例路径为 ./.bashrc
。完整的 uri 看起来如何? file://./.bashrc
对我来说似乎很奇怪。
Assume that the scheme for a uri is "file". Also assume that the path starts with .
An example path is ./.bashrc
. How would the full uri look? file://./.bashrc
appears odd to me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
简而言之,文件 URL 的形式为:
或者您可以省略主机(但不能省略斜杠):
但不是这个:
也不是这个:
我刚刚通过 Python 的 urllib2.urlopen() 确认了
更多详细信息 http://en.wikipedia.org/wiki/File_URI_scheme:
In short, a file URL takes the form of:
or you can omit the host (but not the slash):
but not this:
nor this:
I just confirmed that via Python's urllib2.urlopen()
More detail from http://en.wikipedia.org/wiki/File_URI_scheme:
不可能使用带有“.”的完整文件:URI或路径中没有根部分的“..”段。无论您使用“file://./.bashrc”还是“file:///./.bashrc”,这些路径都没有意义。如果您想使用相对链接,请在没有协议/权限部分的情况下使用它:
如果您想使用完整的 URI,您必须告诉根相对于您的相对路径是:
根据 RFC 3986
RFC 3986 甚至描述了删除这些“.”的算法。和来自 URI 的“..”。
It's impossible to use full file: URI with '.' or '..' segments in path without root part of that path. Whether you use 'file://./.bashrc' or 'file:///./.bashrc' these paths will have no sense. If you want to use a relative link, use it without protocol/authority part:
If you want to use full URI, you must tell a root relative to which your relative path is:
According to RFC 3986
RFC 3986 describes even an algorithm of removing these "." and ".." from URI.
在终端中,您可以输入“file://$PWD/.bashrc”,使用“$PWD”来引用当前目录。
In a terminal you could type "file://$PWD/.bashrc" using "$PWD" to refer to the current directory.
您不应在
file:
后添加双斜杠。正确的形式是参见 RFC 3986,
path-rootless定义
You should not put double slash after
file:
. Correct form isSee RFC 3986,
path-rootless
definition我不知道你的用例。
我的节点代码中有类似的需求,因此当我需要相对于我的工作目录的文件 url 时,我会创建一个像这样的 url ...
I don't know your use case.
I have a similar need in my node code, so when I need a file url relative to my working directory I create a url like so ...
URI 始终是绝对的(除非它们是相对 URI,这是一种没有模式的不同野兽)。这是因为它们是一种服务器-客户端技术,引用服务器的工作目录是没有意义的。话又说回来,引用文件系统在服务器-客户端上下文中也没有意义
URIs are always absolute (unless they're relative URIs, which is a different beast without a schema). That comes from them being a server-client technology where referencing the server's working directory doesn't make sense. Then again, referencing the file system doesn't make sense in a server-client context either ????. Nevertheless, RFC 8089 permits only absolute paths:
However, if I were to postulate a non-standard extension, I would choose the following syntax:
The explanation is that RFC 8089 specifies non-local paths
file://<FQDN of host>/path
and local pathsfile:/path
,file://localhost/path
, andfile:///path
. Since we're almost certainly trying to specify a local relative path (ie, accessible by "local file system APIs"), and because a.
is not a FQDN or even a hostname, the simplefile:
scheme + scheme-sepecific-part URI syntax makes the most sense.在 unix shell 脚本中,我设法做到这一点:
在您的特定情况下:
In a unix shell script I managed to go with this:
In your particular case:
有一个解决方法可能会有所帮助。
如果在开发时您只能指定文件的相对路径,但需要 URL(需要知道绝对路径),请使用如下代码 (Java):
这样您就可以获得仍然指向相对路径中的文件的 URL 。它仍然显示在 URL 中,但可以打开。如果您想删除像“..”这样的组件,请使用中间的规范路径。这是一个对我有用的例子:
There is a workaround that might help.
If at development time you can only specify a relative path to the file but need a URL (that requires to know the absolute path) use code like this (Java):
With that you get a URL still pointing to the file in a relative path. It still shows in the URL but it can be opened. If you want to get rid of components like ".." use the canoncial path inbetween. Here is an example that works for me: