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

发布于 2024-07-29 00:14:30 字数 795 浏览 7 评论 0原文

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

我这样做:

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

然后得到: file:/C:/test%20a.xml

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


编辑:

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

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

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

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

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

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


EDIT :

From Java source code : 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

指尖上的星空 2024-08-05 00:14:30

这是一个有趣的问题。

首先,我在 JRE6 上得到了相同的结果。 当我删除 toURL() 部分时我什至得到了这一点。

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

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

:<方案特定部分> 
  

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

通用 URI 语法由
组件的层次序列
称为计划、机构、
路径、查询和片段。

 URI = 方案 ":" hier-part [ "?"   查询] [“#”片段] 

    hier-part = "//" 权限路径-abempty 
                / 绝对路径 
                / 无根路径 
                / 路径为空 
  

方案和路径组件是
必需的,尽管路径可能为空
(无字符)。 当权威是
目前,路径必须为空
或以斜杠(“/”)字符开头。
当权威不存在时,
路径不能以两个斜杠开头
人物 (”//”)。 这些限制
导致五种不同的 ABNF 规则
对于路径(第 3.3 节),只有以下之一
它将匹配任何给定的 URI
参考。

那么,就这样吧。 由于文件 URI 没有权限段,因此禁止它们以 // 开头。

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

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.

RFC2396 does not actually require two slashes. According to section 3:

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

<scheme>:<scheme-specific-part>

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

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.

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

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.

丢了幸福的猪 2024-08-05 00:14:30

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

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

RFC3986 (3.2.2. Host) 指出:

“如果 URI 方案定义了主机的默认值,则当主机子组件未定义或注册名称为空(零长度)时,将应用该默认值。例如,定义“文件”URI 方案,以便不存在权限、空主机和“localhost”都表示最终用户的计算机,而“http”方案则认为缺少权限或空主机无效。”

因此,“文件”方案将 file:///path/file 转换为具有最终用户计算机的上下文,即使权限是空主机也是如此。

To answer why you can have both:

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

RFC3986 (3.2.2. Host) states:

"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."

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

数理化全能战士 2024-08-05 00:14:30

至于在浏览器中使用它,这并不重要。 我通常会看到 file:///... 但一个、两个或三个“/”都可以。 这让我认为(无需查看 java 文档)这将是正常行为。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文