【问题标题】:failed to build valid URL when spaces are contained within当其中包含空格时无法构建有效的 URL
【发布时间】:2011-08-19 05:05:05
【问题描述】:

我在 Java 中遇到了一个非常奇怪的错误。当 URL 包含空格时,它会考虑构建 URL。例如,这个链接: camping at clark

以及重现错误的代码示例 sn-p:

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")));`

如您所见,传递的 url 字符串是 UTF-8 编码的。但是,无论我如何通过它,这段代码总是失败。在我的应用程序中,我需要能够读取通过的任何图像 URL,没有排除。

提前感谢您的帮助!

【问题讨论】:

    标签: java url spaces


    【解决方案1】:
    "http%3A//www.sas.usace.army.mil/lakes/thurmond/images/camping+at+clark+2.jpg"
    

    这不是一个有效的 URL。

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

    这不是一个有效的 URL。

    问题是第一个值只是垃圾。无论对该值进行何种编码,该值都没有正确执行,这应该在源代码中修复。

    正确的网址是:

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

    【讨论】:

    • 感谢您的回复!但是,是JS的转义函数,将它转换成这样,即第一个URL。
    猜你喜欢
    • 2015-02-04
    • 1970-01-01
    • 2014-03-22
    • 2021-07-02
    • 1970-01-01
    • 1970-01-01
    • 2020-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多