开发者

Encode (and redirect) to a URL that has special characters in Java

I have a URL in a String object like this:

http://bhorowitz.com/2011/03/24/bubble-trouble-i-don't-think-so/

the URL may or may not contain unicode characters that need to be encoded. For example, the link above should be transformed to:

http://bhorowitz.com/2011/03/24/bubble-trouble-i-don%e2%80%99t-think-so/

before I redirect to it.

How do I properly escape all special characters (such as unicode) while keeping the rest of the URL structure intact? Is there something out there already that will do this or do I need to 开发者_运维知识库roll my own?

Edit: the tricky part is that I need to escape only invalid characters while leaving the rest of the URL untouched (e.g. http:// should remain http:// and should not be escaped). URLEncoder, as far as I can tell, does not allow me to do this.


I think this is what you were actually looking for:

new URL(yourURLString).toURI().toASCIIString();

It will only encode the required characters while leaving everything else untouched.


http://download.oracle.com/javase/6/docs/api/java/net/URLEncoder.html


JDK ships with enough tools to handle what you want. Please reffer to documentation: http://download.oracle.com/javase/6/docs/api/java/net/URLEncoder.html and http://download.oracle.com/javase/6/docs/api/java/net/URLDecoder.html

Usage is pretty straightforward.

String decoded = URLDecoder.decode("url%20to%20decode", "UTF-8");
String encoded = URLEncoder.encode("url to decode", "UTF-8");

Please notice, that proper character encoding should be provided. Both classes have single parameter versions of those methods, but they are considered deprecated.


I believe this does what you want. It'll encode anything not a / in the path though. It's not perhaps the most elegant solution, yet it should be safe to use.

    // make sure url is valid before parsing it
    try {
        new URL(url);
    } catch (MalformedURLException e) {
        return;
    }

    StringBuilder sb = new StringBuilder();
    Scanner scanner = new Scanner(url).useDelimiter("/");

    // append the protocol part, e.g. http://
    sb.append(scanner.next());
    sb.append('/');

    // append the hostname part
    sb.append(scanner.next());
    sb.append('/');

    // encode each part of path
    while (scanner.hasNext()) {
        String part = scanner.next();
        sb.append(URLEncoder.encode(part, "UTF-8"));
        sb.append('/');
    }

    // remove trailing slash if original doesn't have one
    if (!url.endsWith("/")) {
        sb.deleteCharAt(sb.length() - 1);
    }

    String encoded = sb.toString();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜