【发布时间】:2016-01-15 21:24:57
【问题描述】:
我希望保存以 .jpg 扩展名结尾的网站 URL 中的图像。这是我正在使用的代码:
private void saveImage(String imageURL){
Image img = null;
imageURL = imageURL.substring(2);
try {
URL url = new URL(imageURL);
img = ImageIO.read(url);
} catch (IOException e) {
e.printStackTrace();
}
BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
// Draw the image on to the buffered image
Graphics2D bGr = bimage.createGraphics();
bGr.drawImage(img, 0, 0, null);
bGr.dispose();
try {
ImageIO.write(bimage, "jpg", new File(System.getProperty("user.home") + "/Desktop/image.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
}
示例网址如下:i.imgur.com/fq4ZpIEb.jpg。
但是,每次我运行此代码时,它都会返回此错误消息:
java.net.MalformedURLException: no protocol: i.imgur.com/fq4ZpIEb.jpg
有人知道解决办法吗?谢谢!
【问题讨论】: