Explain Codes LogoExplain Codes Logo

How to do URL decoding in Java?

java
url-decoding
java-encoding
character-encoding
Alex KataevbyAlex Kataev·Nov 7, 2024
TLDR

URLDecoder.decode is your go-to in Java for URL decoding. Use UTF-8 as the character set and settle the puzzle:

String decodedUrl = URLDecoder.decode("http%3A%2F%2Fexample.com%3Fquery%3Djava+url+decode", "UTF-8"); System.out.println(decodedUrl); // You're reading this, right? Great, it worked!

Run into a spooky UnsupportedEncodingException? Just catch it. Nobody likes uninvited guests.

For the folks on Java 10 and later, handling decoding turned easy-as-peasy with the direct scope for Charset in the API:

String decodedUrl = URLDecoder.decode("http%3A%2F%2Fexample.com%3Fquery%3Djava+url+decode", StandardCharsets.UTF_8); System.out.println(decodedUrl); // Seriously, what could be better than this? Breakfast maybe.

Unravel the mystery of decoding

Encrypted, percent-encoded characters. Secret messages. Sound like a plot from a spy movie? It's not. It's just UTF-8 encoded URLs in real life.

Why the secrecy?

Percent-encoding conceals characters to protect data integrity. For instance, %20 is a safe house for a simple space character.

Beware the obstacles

Encoding isn't always a smooth ride. Wrong character maps can mess up decoding, so understanding characters-to-bytes is crucial. Also, select your character encoding wisely, else lose yourself in a forest of misinterpreted data.

Going beyond what meets the eye: URI

When URLDecoder seems a little off for your project (because let's face it, we all have our eccentric requirements), take the road less traveled. Class java.net.URI opens a new world of methods that work by the RFC2396 rules, which is the latest internet standard for URLs.

URI uri = new URI("http://example.com?query=java%20url%20decode"); String decodedQuery = uri.getQuery(); System.out.println(decodedQuery); // I solemnly swear the query is now up to no good.

Ready for the big league?

Apache's URLCodec.decode

Apache declares a fantastic codec kingdom where URLCodec.decode is the crown jewel. For projects as complex as a labyrinth, this might be the best bet.

URLCodec codec = new URLCodec(); String decodedUrl = codec.decode("http%3A%2F%2Fexample.com%3Fquery%3Djava+url+decode"); System.out.println(decodedUrl); // Unlocked another achievement.

Character encoding: A tale of forgotten errors

Are you falling into the trap of conflating URL encoding for HTML forms and RFC2396? Scrap the confusion; understand they're two different beasts!

Decoding that fits like a glove

What are you decoding today? Depending on it, context-aware decoding is crucial. Choose URLDecoder for form data or URI for URIs.

Extra: Demystifying special characters

Don't be afraid of characters like : or /, encoded as %3A and %2F. After decoding, they revert to their original selves. Change of heart, anyone?

Web data: A decoding love story

Web data handling and form submissions thrive on accurate URL encoding/decoding. It's like a shield, repelling the attacks of data misinterpretation and ensuring the messages shared are kept intact.

Learning beyond borders

If character encoding has sparked a newfound interest, do consider diving into Joel Spolsky's delightful article diving deep into all things Unicode.