Java:Windows 文件上的 File.toURI().toURL()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1131273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:06:05  来源:igfitidea点击:

Java : File.toURI().toURL() on Windows file

javaurlurifilepathfile-uri

提问by glmxndr

The system I'm running on is Windows XP, with JRE 1.6.

我运行的系统是带有 JRE 1.6 的 Windows XP。

I do this :

我这样做:

public static void main(String[] args) {
    try {
        System.out.println(new File("C:\test a.xml").toURI().toURL());
    } catch (Exception e) {
        e.printStackTrace();
    }       
}

and I get this : file:/C:/test%20a.xml

我明白了: file:/C:/test%20a.xml

How come the given URL doesn't have two slashes before the C:? I expected file://C:.... Is it normal behaviour?

为什么给定的 URL 在C:?之前没有两个斜杠?我预料到了file://C:...。这是正常行为吗?



EDIT :

编辑 :

From Java source code : java.net.URLStreamHandler.toExternalForm(URL)

来自 Java 源代码:java.net.URLStreamHandler.toExternalForm(URL)

    result.append(":");
    if (u.getAuthority() != null && u.getAuthority().length() > 0) {
        result.append("//");
        result.append(u.getAuthority());
    }

It seems that the Authority part of a file URL is null or empty, and thus the double slash is skipped. So what is the authority part of a URL and is it really absent from the file protocol?

文件 URL 的 Authority 部分似乎为 null 或空,因此跳过双斜杠。那么 URL 的权威部分是什么,它真的不存在于文件协议中吗?

采纳答案by Powerlord

That's an interesting question.

这是一个有趣的问题。

First things first: I get the same results on JRE6. I even get that when I lop off the toURL() part.

第一件事:我在 JRE6 上得到相同的结果。当我去掉 toURL() 部分时,我什至明白了这一点。

RFC2396does not actually require two slashes. According to section 3:

RFC2396实际上并不需要两个斜线。根据第 3 节:

The URI syntax is dependent upon the scheme. In general, absolute URI are written as follows:

<scheme>:<scheme-specific-part>

URI 语法取决于方案。一般来说,绝对URI的写法如下:

<scheme>:<scheme-specific-part>

Having said that, RFC2396 has been superseded by RFC3986, which states

话虽如此, RFC2396 已被RFC3986取代,其中指出

The generic URI syntax consists of a hierarchical sequence of omponents referred to as the scheme, authority, path, query, and fragment.

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

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

The scheme and path components are required, though the path may be empty (no characters). When authority is present, the path must either be empty or begin with a slash ("/") character. When authority is not present, the path cannot begin with two slash characters ("//"). These restrictions result in five different ABNF rules for a path (Section 3.3), only one of which will match any given URI reference.

通用 URI 语法由组件的分层序列组成,这些组件称为方案、权限、路径、查询和片段。

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

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

方案和路径组件是必需的,但路径可能为空(无字符)。当存在权限时,路径必须为空或以斜杠 ("/") 字符开头。当权限不存在时,路径不能以两个斜杠字符(“//”)开头。这些限制导致路径有五种不同的 ABNF 规则(第 3.3 节),其中只有一种会匹配任何给定的 URI 引用。

So, there you go. Since file URIs have no authority segment, they're forbidden from starting with //.

所以,你去了。由于文件 URI 没有权限段,因此禁止以 // 开头。

However, that RFC didn't come around until 2005, and Java references RFC2396, so I don't know why it's following this convention, as file URLs before the new RFC have always had two slashes.

然而,那个 RFC 直到 2005 年才出现,Java 引用了 RFC2396,所以我不知道它为什么遵循这个约定,因为新 RFC 之前的文件 URL 总是有两个斜杠。

回答by Jeremy Cron

As far as using it in a browser is concerned, it doesn't matter. I have typically seen file:///...but one, two or three '/' will all work. This makes me think (without looking at the java documentation) that it would be normal behavior.

就在浏览器中使用它而言,这无关紧要。我通常看到过file:///...一个、两个或三个“/”都可以工作。这让我认为(不查看 java 文档)这将是正常行为。

回答by rbeede

To answer why you can have both:

要回答为什么您可以同时拥有两者:

file:/path/file
file:///path/file
file://localhost/path/file

RFC3986 (3.2.2. Host)states:

RFC3986(3.2.2. 主机)指出:

"If the URI scheme defines a default for host, then that default applies when the host subcomponent is undefined or when the registered name is empty (zero length). For example, the "file" URI scheme is defined so that no authority, an empty host, and "localhost" all mean the end-user's machine, whereas the "http" scheme considers a missing authority or empty host invalid."

“如果 URI 方案为主机定义了默认值,那么当主机子组件未定义或注册名称为空(零长度)时,该默认值适用。例如,定义了“文件”URI 方案,因此没有权限,空主机和“本地主机”都表示最终用户的机器,而“http”方案认为缺少权限或空主机无效。”

So the "file" scheme translates file:///path/fileto have a context of the end-user's machine even though the authority is an empty host.

因此,file:///path/file即使授权是空主机,“文件”方案也可以转换为具有最终用户机器的上下文。