Hi,
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...
Thanks!

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"); 
System.out.println(junk);
junk = URLDecoder.decode(addr,"UTF-8");
System.out.println(junk);

Your output is:

http%3A%2F%2Fwww.blah.blah%2Fimage+with+spaces.gif
[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.

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 .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.