I have an urgent ugly problem. I have a php file on a server that reads the name of the files of a directory from the server and POST them to my java application. When I try to read it, i get

Server returned HTTP response code: 400 for URL: http://www.xxxxxxxxxxxxx

. This does not happens on files that do not contain white spaces. Only at URLs like: "http://www.abc.com/Hello world.gif" but not at "http://www.abc.com/HelloWorld.gif".
I tried to encode the URL... I could not solve it. Please can you help me? I need it fast...

8 Years
Discussion Span
Last Post by PatrickSharp

Ah, if only a nickle I had for each urgent problem needing to be solved fast...

/** imports
import java.net.URLDecoder;
import java.net.URLEncoder;

// ...

String addr = "http://www.blah.blah/image with spaces.gif";
String junk = URLEncoder.encode(addr,"UTF-8"); 
junk = URLDecoder.decode(addr,"UTF-8");

Your output is:

[url]http://www.blah.blah/image[/url] with spaces.gif

Of course, that's quite simple, thanks, but it useless as long as it cannot be parsed and throws malformed URL exception. I had a midi file loaded from URL. I vizited Sun forums and found out some topics and they could not solve this... white spaces in URL are forbidden. The encoding doesn't help as I said and as they posted there, too.
If you encode the URL it becomes unparsable. Sad but true. (same problem on that forum).
However, thanks.

Edited by Clawsy: n/a


How about trying just replacing the spaces with :

URLString.replaceAll(" ","%20");

I'm personally OK with spaces in filenames, except if they live on a webserver for this very same reason :)

IF you are requesting the filename with a Get or FTP request, then replace the spaces with %20 .

Edited by PatrickSharp: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.