JDK 1.4 与 JDK 1.5 中的 java URI 变化
import java.net.*;
public class TestURI {
public static void main(String args[]) throws URISyntaxException
{
String first = new String("foo");
String second = new String("bar");
String third = new String("[space or another space]");
URI temp = new URI(first, second, third);
System.out.println(temp.getFragment());
}
}
当我在 JDK 1.4 中运行上述代码时,我得到
[space or another space]
当我在 JDK 1.5/1.6 中运行相同的代码时,我得到以下内容:
[space%20or%20another%20space]
有人可以告诉我发生了什么变化吗?
谢谢, Raj
编辑:
如果我执行以下操作,它会起作用:
import java.net.*;
public class TestURI {
public static void main(String args[]) throws URISyntaxException
{
String first = new String("foo");
String second = new String("bar");
String third = new String("[space or another space]").replaceAll("\\[", "leftSB").replaceAll("\\]", "rightSB");
URI temp = new URI(first, second, third);
System.out.println(temp.getFragment().replaceAll("leftSB", "\\[").replaceAll("rightSB", "\\]"));
}
}
import java.net.*;
public class TestURI {
public static void main(String args[]) throws URISyntaxException
{
String first = new String("foo");
String second = new String("bar");
String third = new String("[space or another space]");
URI temp = new URI(first, second, third);
System.out.println(temp.getFragment());
}
}
When I run the above code in JDK 1.4, I get
[space or another space]
When I run the same code in JDK 1.5/1.6, I get the following:
[space%20or%20another%20space]
Could somebody tell me what changed?
Thanks,
Raj
Edit:
If I do something like the following, it works:
import java.net.*;
public class TestURI {
public static void main(String args[]) throws URISyntaxException
{
String first = new String("foo");
String second = new String("bar");
String third = new String("[space or another space]").replaceAll("\\[", "leftSB").replaceAll("\\]", "rightSB");
URI temp = new URI(first, second, third);
System.out.println(temp.getFragment().replaceAll("leftSB", "\\[").replaceAll("rightSB", "\\]"));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来空格已被 URI 编码。
%20
是 ASCII 空格字符的十六进制格式。我认为片段标识符中的空格是非法的,Java 1.4 中的实现并不知道这一点。
从 类文档,我强调:
之后您将使用三参数构造函数和
getFragment
方法。看起来应该再次解码空格,但事实并非如此。这可能是一个错误,但 Sun Bug 数据库现在似乎已离线,所以我无法真正检查这一点。It looks like the spaces got URI-encoded.
%20
is the hexadecimal formatting of the ASCII space character.I suppose spaces are illegal in the fragment identifier, which the implementation in Java 1.4 did not know.
From the class documentation, emphasis by me:
You are using the three-argument constructor and the
getFragment
method afterwards. It looks like it should decode the spaces again, but it does not. This could be a bug, but the Sun Bug database seems to be offline now, so I can't really check this.