Java:Windows文件上的File.toURI()。toURL() [英] Java : File.toURI().toURL() on Windows file

查看:1080
本文介绍了Java:Windows文件上的File.toURI()。toURL()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行的系统是Windows XP,JRE 1.6。

The system I'm running on is Windows XP, with 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 $之前没有两个斜杠c> C:?我期望 file:// C:... 。这是正常行为吗?

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

编辑:

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

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());
    }

似乎文件URL的权限部分为空或空,并且因此跳过双斜杠。那么URL的权限部分是什么?文件协议中是否真的没有?

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?

推荐答案

这是一个有趣的问题。

首先要做的事情是:我在JRE6上获得了相同的结果。当我丢掉toURL()部分时,我甚至得到了它。

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

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

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


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

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

<scheme>:<scheme-specific-part>


话虽如此,RFC2396已被 RFC3986 ,其中说明

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


通用URI语法由
层次结构的omponents
组成,称为scheme,authority,
path,query和fragment。

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

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

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没有权限段,因此禁止它们以//开头。

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

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

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.

这篇关于Java:Windows文件上的File.toURI()。toURL()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆