URL Encoding in Java: URLEncoder Complete Guide
Learn URL encoding in Java using URLEncoder and java.net.URI. Includes complete code examples for query parameters and full URLs.
URL encoding — also called percent-encoding — converts characters that are not allowed in a URL into a safe format by replacing them with a % followed by two hexadecimal digits. In Java, the java.net.URLEncoder and java.net.URI classes provide built-in support for this operation, making it straightforward to construct valid HTTP requests from user input or dynamic data.
How to Use the URL Encoder Tool
- Visit the URL Encoder on ToolSparkr.
- Paste the URL or string you need to encode into the input field.
- The encoded output appears instantly — spaces become
%20, special characters are percent-encoded. - Copy the result and use it directly in your Java HTTP request, database query, or configuration file.
URL Encoding in Java with URLEncoder
java.net.URLEncoder.encode() is the standard Java method for encoding form parameters and query string values:
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
public class UrlEncoderExample {
public static void main(String[] args) throws Exception {
String raw = \"Hello World! Price: $19.99 & discount=10%\";
String encoded = URLEncoder.encode(raw, StandardCharsets.UTF_8);
System.out.println(\"Encoded: \" + encoded);
// Encoded: Hello+World%21+Price%3A+%2419.99+%26+discount%3D10%25
}
}
Note: URLEncoder encodes spaces as + (application/x-www-form-urlencoded format). If your target API expects %20 for spaces, replace them after encoding:
String encoded = URLEncoder.encode(raw, StandardCharsets.UTF_8)
.replace(\"+\", \"%20\");
Encoding Full URLs with java.net.URI
For encoding a complete URL (not just a query value), use java.net.URI to avoid double-encoding the scheme and host:
import java.net.URI;
URI uri = new URI(\"https\", \"example.com\", \"/search\", \"q=hello world&lang=en\", null);
String safeUrl = uri.toASCIIString();
System.out.println(safeUrl);
// https://example.com/search?q=hello%20world&lang=en
Why Use a URL Encoder?
- HTTP compliance — Unencoded special characters in URLs cause malformed requests that servers may reject or misparse.
- Security — Proper encoding prevents injection attacks where malicious input manipulates URL structure.
- API correctness — REST APIs and OAuth flows require correctly percent-encoded parameters to authenticate and route requests.
- Internationalisation — Non-ASCII characters (Chinese, Arabic, emoji) must be UTF-8 encoded to be transmitted safely over HTTP.
Tips and Best Practices
- Always specify
StandardCharsets.UTF_8explicitly — never rely on the platform default charset, which can vary across operating systems. - Encode each query parameter value separately, not the entire URL string, to avoid double-encoding delimiters like
&and=. - Use
URIconstructors (notnew URI(rawString)) when building URLs programmatically — they handle encoding per component. - When using OkHttp or Apache HttpClient, use their built-in URL builder classes (
HttpUrl.Builder,URIBuilder) which handle encoding automatically.
For quick one-off encoding without writing any code, use the free URL Encoder on ToolSparkr.