收到 URL 格式错误异常,异常信息显示没有协议,但实际上已经有协议。

huangapple 未分类评论47阅读模式
英文:

Getting a malformed url exception saying there is no protocol when there is one

问题

我正在尝试从一个 URL 创建图像,但我一直收到这个异常 java.net.MalformedURLException: no protocol: error。然而,我正在使用的 URL 确实有一个协议 https://i.stack.imgur.com/zB9WN.jpg 无法弄清楚为什么它不起作用。谢谢

try {
            URL url=new URL(parser.getImage());
            BufferedImage c= ImageIO.read(url);
            ImageIcon icon=new ImageIcon(c);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
英文:

I'm trying to create an image from a url but I keep getting this exception java.net.MalformedURLException: no protocol: error. However the url I'm using does have a protocol https://i.stack.imgur.com/zB9WN.jpg Can't figure out why it won't work. Cheers

try {
            URL url=new URL(parser.getImage());
            BufferedImage c= ImageIO.read(url);
            ImageIcon icon=new ImageIcon(c);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

</details>


# 答案1
**得分**: 0

如果您想使用图像,则URL应该是以下形式的图像URL:

URL url = new URL("https://i.stack.imgur.com/1LRx6.jpg");

您可以尝试在示例代码中将上述URL进行硬编码,看看是否起作用!

<details>
<summary>英文:</summary>

if you want to use image then URL should be of Image like below:

URL url=new URL(&quot;https://i.stack.imgur.com/1LRx6.jpg&quot;);

can you try hard coding above url in your sample and see if that works!

</details>



huangapple
  • 本文由 发表于 2020年4月6日 09:33:31
  • 转载请务必保留本文链接:https://java.coder-hub.com/61051730.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定