简体   繁体   English

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

[英]Java : File.toURI().toURL() on Windows file

The system I'm running on is Windows XP, with JRE 1.6. 我正在运行的系统是Windows XP,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 我得到了这个: file:/C:/test%20a.xml

How come the given URL doesn't have two slashes before the C: ? 为什么给定的URL在C:之前没有两个斜杠? I expected file://C:... . 我期待file://C:... Is it normal behaviour? 这是正常的行为吗?


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. 似乎文件URL的Authority部分为null或为空,因此跳过双斜杠。 So what is the authority part of a URL and is it really absent from the file protocol? 那么URL的权限部分是什么?它是否真的不存在于文件协议中?

That's an interesting question. 这是一个有趣的问题。

First things first: I get the same results on JRE6. 首先要做的是:我在JRE6上获得了相同的结果。 I even get that when I lop off the toURL() part. 当我丢掉toURL()部分时,我甚至得到了它。

RFC2396 does not actually require two slashes. RFC2396实际上不需要两个斜杠。 According to section 3: 根据第3节:

The URI syntax is dependent upon the scheme. URI语法取决于方案。 In general, absolute URI are written as follows: 通常,绝对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语法由一系列的omponents组成,称为方案,权限,路径,查询和片段。

  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. 这些限制导致路径的五个不同的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总是有两个斜杠。

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方案,以便没有权限,空主机,“localhost”都表示最终用户的机器,而“http”方案认为缺少权限或空主机无效。“

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. 因此,“文件”方案将file:///path/file为具有最终用户机器的上下文,即使权限是空主机也是如此。

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. 我通常看到file:///...但是一个,两个或三个'/'都可以工作。 This makes me think (without looking at the java documentation) that it would be normal behavior. 这让我觉得(不看java文档)这将是正常的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM