今天我不得不转义 URL (http://rfc3986.openrfc.org/ - 例如,替换 'space ' 字符与 %20) 令人惊讶的是,我在 Java 中找不到任何 API 可以将未转义的 URL 作为 1 个参数并返回转义的 URL。
我知道 URI 类能够执行转义操作,但是需要使用多参数构造函数(http://download.oracle.com/javase/1.4.2/docs/api/java/net/URI.html# URI(java.lang.String, java.lang.String, java.lang.String, int, java.lang.String, java.lang.String, java.lang.String)),简单地使用单参数构造函数不执行转义,但抛出异常。
幸运的是,我已经嵌入了 JRuby 库,所以我能够使用 URI.escape 方法 http://www.ruby-doc.org/stdlib/libdoc/uri/rdoc/classes/URI/Escape.html 但是我真的很惊讶没有简单的方法在Java中也能达到同样的效果。
我是否缺少一些 API?
Today I had to escape URL (http://rfc3986.openrfc.org/ - for instance, replace 'space' character with %20) and surprisingly I couldn't find any API in Java which would take unescaped URL as 1 parameter and return escaped URL.
I am aware of URI class which is capable of performing escaping operation, however it's required to use multiargument contructor (http://download.oracle.com/javase/1.4.2/docs/api/java/net/URI.html#URI(java.lang.String, java.lang.String, java.lang.String, int, java.lang.String, java.lang.String, java.lang.String)), using single-argument contructor simply doesn't perform escaping but throws an exception.
Luckily, I was already emedding JRuby library so I was able to use URI.escape method http://www.ruby-doc.org/stdlib/libdoc/uri/rdoc/classes/URI/Escape.html however I'm really surprised there is no easy way to achieve same effect in Java.
Am I missing some API?
发布评论