Force URLEncoder to use %20 instead of + for spaces?

N

Neil

Hello:

When I encode the name of an image file to place into a URL, I
use this code:

URLEncoder.encode(imageName,"UTF-8")

This gives me image names with spaces replaced by + signs which
IE does not like.

If I change the spaces to %20, the URL works perfectly in IE.

For example, if my image name is Ping G2 Driver.gif, URLEncoder gives
me this URL:

http://dev.rentclubs.com/~maryanne/images/clubs/Driver/Ping+G2+Driver.gif

If I try to load this URL in IE, it gives me a 404 error.

If I use the exact same url with %20, it works just fine:

http://dev.rentclubs.com/~maryanne/images/clubs/Driver/Ping G2 Driver.gif

I thought + and %20 were both acceptable for spaces in a URL, but
apparently IE does not like the plus signs.

Is there a way to force URLEncoder to use %20 instead of + signs?

Thanks,
Neil
 
K

Knute Johnson

Neil said:
Hello:

When I encode the name of an image file to place into a URL, I
use this code:

URLEncoder.encode(imageName,"UTF-8")

This gives me image names with spaces replaced by + signs which
IE does not like.

If I change the spaces to %20, the URL works perfectly in IE.

For example, if my image name is Ping G2 Driver.gif, URLEncoder gives
me this URL:

http://dev.rentclubs.com/~maryanne/images/clubs/Driver/Ping+G2+Driver.gif

If I try to load this URL in IE, it gives me a 404 error.

If I use the exact same url with %20, it works just fine:

http://dev.rentclubs.com/~maryanne/images/clubs/Driver/Ping G2 Driver.gif

I thought + and %20 were both acceptable for spaces in a URL, but
apparently IE does not like the plus signs.

Is there a way to force URLEncoder to use %20 instead of + signs?

Thanks,
Neil

Doesn't look like it.

..replaceAll("\\+","%20");
 
R

Raymond DeCampo

Neil said:
Hello:

When I encode the name of an image file to place into a URL, I
use this code:

URLEncoder.encode(imageName,"UTF-8")

This gives me image names with spaces replaced by + signs which
IE does not like.

If I change the spaces to %20, the URL works perfectly in IE.

For example, if my image name is Ping G2 Driver.gif, URLEncoder gives
me this URL:

http://dev.rentclubs.com/~maryanne/images/clubs/Driver/Ping+G2+Driver.gif

If I try to load this URL in IE, it gives me a 404 error.

If I use the exact same url with %20, it works just fine:

http://dev.rentclubs.com/~maryanne/images/clubs/Driver/Ping G2 Driver.gif

I thought + and %20 were both acceptable for spaces in a URL, but
apparently IE does not like the plus signs.

Is there a way to force URLEncoder to use %20 instead of + signs?

Despite the name, URLEncoder is not for encoding URLs, but for encoding
parameter names and values for use in GET-style URLs or POST forms.
That is, for transforming plain text into the
application/x-www-form-urlencoded MIME format as described in the HTML
specification.

HTH,
Ray
 
R

Raymond DeCampo

Raymond said:
Despite the name, URLEncoder is not for encoding URLs, but for encoding
parameter names and values for use in GET-style URLs or POST forms. That
is, for transforming plain text into the
application/x-www-form-urlencoded MIME format as described in the HTML
specification.

HTH,
Ray

I meant to add that the java.net.URL class javadoc recommends using the
java.net.URI class for the type of encoding you are doing (RFC 2396).

HTH,
Ray
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,265
Messages
2,571,069
Members
48,771
Latest member
ElysaD

Latest Threads

Top