开发者

failed to build valid URL when spaces are contained within

开发者 https://www.devze.com 2023-03-03 16:22 出处:网络
I ran into a very strange error in Java. It regards building URLs when they have spaces contained. For instance, this link:

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 pa开发者_StackOverflowssed with no exclusions.

Thank you in advance for any help!


"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"
0

精彩评论

暂无评论...
验证码 换一张
取 消