Take the 2-minute tour ×

How can I encode dynamic String values in order to create URL instances? I need to replace spaces with %20, accents, non-ASCII characters... ? I tried to use URLEncoder but it also encodes '/' character and if I give a string encoded with URLEncoder to the URL constructor I get a MalformedURLException (no protocol).

share|improve this question
Please give some example String and you're code. –  Lukas Knuth Jun 1 '11 at 9:27
different encoding rules will apply to different parts of the URI. As Lukas suggests, please provide examples of what information you start with and what you want to end up with. –  McDowell Jun 1 '11 at 10:45

2 Answers 2

URLEncoder has a very misleading name. It is according to the Javadocs used encode form parameters using MIME type application/x-www-form-urlencoded.

With this said it can be used to encode e.g., query parameters. For instance if a parameter looks like &/?# its encoded equivalent can be used as:

String url = "http://host.com/?key=" + URLEncoder.encode("&/?#");

Unless you have those special needs the URL javadocs suggests using new URI(..).toURL which performs URI encoding according to RFC2396.


The following sample

new URI("http", "host.com", "/path/", "key=| ?/#ä", "fragment").toURL();

produces the result http://host.com/path/?key=%7C%20?/%23ä#fragment. Note how characters such as ?&/ are not encoded.

For further information, see the posts HTTP URL Address Encoding in Java or how to encode URL to avoid special characters in java.


Since your input is a string url, using one of the parameterized constructor of URI will not help you. Neither can you use new URI(strUrl) directly since it doesn't quote url parameters.

So at this stage we must use a trick to get what you want:

public URL parseUrl(String s) throws Exception {
     URL u = new URL(s);
     return new URI(

Before you can use this routine you have to sanitize your string to ensure it represents an absolute url. I see two approaches to this:

  1. Guessing. Prepend http:// to the string unless it's already present.

  2. Construct the url from a context using new URL(URL context, String spec)

share|improve this answer
URI uri = new URI("www.google.com"); uri.toURL(); -> exception : "URI is not absolute" –  Arutha Jun 1 '11 at 9:53
That's not a valid URI. Please use the correct scheme, e.g., http. –  Johan Sjöberg Jun 1 '11 at 9:56
I receive string from users... –  Arutha Jun 1 '11 at 12:19
@Arutha, that doesn't make it any more right. It means that you have to sanitize the url yourself. –  Johan Sjöberg Jun 1 '11 at 13:02
Nice trick! I'm using it to encode URL's in my Wordnik library now. Thanks! –  Jeremy Brooks Jul 26 '12 at 1:23

So what you're saying is that you want to encode part of your URL but not the whole thing. Sounds to me like you'll have to break it up into parts, pass the ones that you want encoded through the encoder, and re-assemble it to get your whole URL.

share|improve this answer

Your Answer


