DevToolsForYou

URL Encoder and Decoder in Java — Code Examples

URL Encoder and Decoder in JavaUse the online tool →

URL encoding (percent-encoding) replaces characters that are not allowed in URLs with a percent sign followed by their hexadecimal value. Here is how to encode and decode URL components in each language.

Use java.net.URLEncoder for encoding form parameters and java.net.URI for encoding full URLs.

Java
import java.net.URLEncoder;
import java.net.URLDecoder;
import java.net.URI;
import java.nio.charset.StandardCharsets;

public class UrlEncodingExample {
    public static void main(String[] args) throws Exception {
        String input = "hello world & foo=bar";

        // Encode a query parameter value
        String encoded = URLEncoder.encode(input, StandardCharsets.UTF_8);
        System.out.println(encoded); // hello+world+%26+foo%3Dbar

        // Decode
        String decoded = URLDecoder.decode(encoded, StandardCharsets.UTF_8);
        System.out.println(decoded); // hello world & foo=bar

        // Build a full URL safely using URI
        URI uri = new URI("https", "example.com", "/search", "q=hello world", null);
        System.out.println(uri.toString());
        // https://example.com/search?q=hello%20world
    }
}
Notes & gotchas
  • URLEncoder.encode() follows the application/x-www-form-urlencoded spec — spaces become +, not %20.
  • Always pass StandardCharsets.UTF_8 explicitly; the charset parameter is required in Java 10+.
  • Use URI constructor for encoding complete URLs — it handles each component separately.
Try it in your browser

Need to url encode/decode without writing code? The URL Encoder and Decoder runs entirely in your browser — paste your input and get the result instantly. No signup, no install, no data sent to a server.

Open URL Encode/Decode
URL Encoder and Decoder in other languages