简体   繁体   English

Java URL 构造函数忽略路径段

[英]Java URL constructor ignores path segment

I am using the Java URL constructor "URL(URL context, String spec)" found here but the constructed URL is not what I expect - it is leaving out a path segment provided in the context parameter.我正在使用此处找到的 Java URL 构造函数“URL(URL 上下文,字符串规范)”,但构造的 URL 不是我期望的上下文参数。

As an example, this code例如,这段代码

new URL(new URL("http://asdf.com/z"), "a/b/c");

produces a URL with value产生一个 URL 值

http://asdf.com/a/b/c http://asdf.com/a/b/c

So it has left out of "z" path segment.所以它已经离开了“z”路径段。

I have two questions:我有两个问题:

  1. What is the meaning of "context" the first parameter here in the java doc? java doc中的“上下文”第一个参数是什么意思? I could not find mention of it in the URL specification nor did I really find it in java doc.我在 URL 规范中找不到提及它,我也没有在 java 文档中找到它。
  2. Is leaving out the "z" expected behavior?是否遗漏了“z”预期行为?

Thanks!谢谢!

What is the meaning of "context" the first parameter here in the java doc? java doc中的“上下文”第一个参数是什么意思?

It's like the "base URL" of the spec parameter.这就像spec参数的“基本 URL”。 If context is https://example.com , and spec is /foo , the constructor would create https://example.com/foo .如果contexthttps://example.com ,并且spec/foo ,则构造函数将创建https://example.com/foo It's similar to (but not exactly the same as, as we'll see later) asking "I am currently on https://example.com , and I want to go to /foo , what would my final URL be?" It's similar to (but not exactly the same as, as we'll see later) asking "I am currently on https://example.com , and I want to go to /foo , what would my final URL be?"

Is leaving out the "z" expected behavior?是否遗漏了“z”预期行为?

Yes.是的。 If you follow through the rules of resolving a relative URL against an base URL in RFC 2396 with regards to this case, you will reach this step:如果您按照 RFC 2396 中针对此案例的基本 URL 解决相对 URL 的规则,您将到达此步骤:

(6) If this step is reached, then we are resolving a relative-path reference. (6) 如果达到此步骤,那么我们正在解析相对路径引用。 The relative path needs to be merged with the base URI's path.相对路径需要与基本 URI 的路径合并。 Although there are many ways to do this, we will describe a simple method using a separate string buffer.虽然有很多方法可以做到这一点,但我们将描述一个使用单独的字符串缓冲区的简单方法。

(a) All but the last segment of the base URI's path component is copied to the buffer. (a) 除了基 URI 路径组件的最后一段之外的所有部分都被复制到缓冲区。 In other words, any characters after the last (right-most) slash character, if any, are excluded.换句话说,最后一个(最右边的)斜线字符之后的任何字符(如果有)都将被排除。

(b) The reference's path component is appended to the buffer string. (b) 引用的路径组件附加到缓冲区字符串。

The "last segment" here, refers to z , and that is not added to the buffer.这里的“最后一段”指的是z ,它没有添加到缓冲区中。 Right after that, the path a/b/c "is appended to the buffer".在那之后,路径a/b/c “被附加到缓冲区”。 Steps (c) onwards deals with removing .步骤 (c) 以后处理删除. and .. , which is irrelevant here... ,这在这里无关紧要。

Note that RFC 2386 doesn't say you MUST implement the algorithm in this way, but that whatever your implementation is, your output must match the output of that algorithm:请注意,RFC 2386 并没有说您必须以这种方式实现算法,但无论您的实现是什么,您的 output 必须与该算法的 output 匹配:

The above algorithm is intended to provide an example by which the output of implementations can be tested -- implementation of the algorithm itself is not required.上述算法旨在提供一个示例,通过该示例可以测试 output 的实现——不需要算法本身的实现。

So yeah, this is expected.所以,是的,这是意料之中的。 To keep the /z , you should add another / after the z :要保留/z ,您应该在z之后添加另一个/

new URL(new URL("http://asdf.com/z/"), "a/b/c")

This way the "last segment" becomes the empty string.这样,“最后一段”就变成了空字符串。

You can treat the context like the current directory in file system.您可以将上下文视为文件系统中的当前目录。
With context "http://asdf.com/z", the current directory is "http://asdf.com/", and use "a/b/c" as the spec will result a full path "http://asdf.com/a/b/c".使用上下文“http://asdf.com/z”,当前目录为“http://asdf.com/”,使用“a/b/c”作为规范将产生完整路径“http:// /asdf.com/a/b/c”。

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

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