Martin
Martin

Reputation: 3

failed to build valid URL when spaces are contained within

I ran into a very strange error in Java. It regards building URLs when they have spaces contained. For instance, this link: camping at clark

and this sample snippet of code, which reproduces the error:

String urlEncoded2 = "http%3A//www.sas.usace.army.mil/lakes/thurmond/images/camping+at+clark+2.jpg";

BufferedImage test = ImageIO.read(new URL(URLDecoder.decode(urlEncoded2, "UTF-8")));`

As you can see the url string passed, is UTF-8 encoded. However, no matter how I pass it, this code always fails. In my application I need to be able to read any image URL passed with no exclusions.

Thank you in advance for any help!

Upvotes: 0

Views: 579

Answers (1)

McDowell
McDowell

Reputation: 108859

"http%3A//www.sas.usace.army.mil/lakes/thurmond/images/camping+at+clark+2.jpg"

This is not a valid URL.

"http://www.sas.usace.army.mil/lakes/thurmond/images/camping at clark 2.jpg"

This is not a valid URL.

The problem is that the first value is just junk. Whatever encoded that value didn't do it correctly and this should be fixed at source.

The correct URL is:

"http://www.sas.usace.army.mil/lakes/thurmond/images/camping%20at%20clark%202.jpg"

Upvotes: 2

Related Questions