简体   繁体   中英

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 (eg 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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