Java URLEncode question?

I'm writing a Java program that creates a URI. Unfortunately it looks like I have to use a URLEncoder to convert all the spaces into compatible URL syntax. Normally that's either %20 or the plus + sign. However, when I try to put a URL like "http://server/a+file.mp3" into Firefox the server comes back with a 404 error. If I replace the plus sign with %20 it works. Anyone know why?

2010-02-20T08:31:59Z

URLs cannot contain spaces. URL encoding normally replaces a space with a + sign.

http://www.w3schools.com/TAGS/ref_urlencode.asp

2010-02-20T16:47:54Z

The quote is directly from that link. Likewise, both the URLEncoder in Java and .Net replace spaces with + signs in URLs.

vincentgl2010-02-23T19:58:24Z

Favorite Answer

Yes, the w3schools.com page does say that. I remember using "+" for <space> in URLs a long time ago. I think %20 became the more commonly used replacement, perhaps related to the introduction of URIs. The earlier RFC 2396 specified %20, as does the current authoritative document RFC3986.

http://tools.ietf.org/html/rfc3986#section-2.1

Or maybe because Javascript uses only %20 when encoding. Anyway, either should work. The problem probably doesn't involve Firefox at all. The problem almost certainly resides in the server. I'm guessing the server-side code is decoding the HTTP request, and when it parses the requested resource from the GET request line, it isn't properly replacing the "+" with a blank when it searches for the "a file.mp3" resource.

Silent2010-02-20T15:56:46Z

I think you're a little confused. The encoded version of a space is %20, not a plus sign. "a file.mp3" and "a+file.mp3" are two different files.

Edit:
You're correct that URLs shouldn't contain spaces, but URL encoding replaces a space with %20, not a plus sign. I'm not sure where you're getting the plus sign thing from; the page you linked to does not mention it.

Relex2010-02-20T15:46:23Z

That's because it's uhmm.. Hex I think, and %20 is just instead of a space, while a + has the value of %21 I think..
So if you type in server.com/my index/index.html the browser (Modern once at least) automatically converts the space into %20, so it becomes readable, same with the plus, the browser converts it into %21 (Or whatever the code is) so that it's readable..

Hope this helped!