Is a (local) file path an URI?

风流意气都作罢 提交于 2020-01-31 13:34:26

问题


On some input we allow the following paths:

  • C:\Folder
  • \\server\Folder
  • http://example.com/...

I wonder if I can mark them all as "URI"s?

Thanks!


回答1:


C:/Folder and /server/Folder/ are file paths.

http://example.com/ is a URL, which is a URI sub-type, so you could mark it as a URI but not the other way around (like how squares are rectangles but not vice versa).

Of course, here you have posted a clear, simple example. When discussing the distinction between URI and URL, not only is the answer not clear cut, it is also disputed. I recommend taking a look at this thread and the answers posted in it for clarification. Generally though, it is mostly agreed that the main difference is that URLs provide an access method (such as http://).

So if we were to convert your first file path into a URL it would become the following (note the addition of the access method):

file:///c:/Folder/test.txt

If you modify all your file paths to include an access method like in my example, then it will be okay for you to mark them as URIs.




回答2:


No, unless you make sure it's an absolute path and add add "file://" to the beginning.

As per RFC 3986 Section 3:

URI         = scheme ":" hier-part [ "?" query ] [ "#" fragment ]

hier-part   = "//" authority path-abempty
            / path-absolute
            / path-rootless
            / path-empty

The scheme and the ":" are not in square brackets [], which means they are not optional.



来源:https://stackoverflow.com/questions/44692872/is-a-local-file-path-an-uri

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!