英文:
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("https://i.stack.imgur.com/1LRx6.jpg");
can you try hard coding above url in your sample and see if that works!
</details>
专注分享java语言的经验与见解,让所有开发者获益!
评论