我正在尋找保存圖像從一個網站的URL以.jpg擴展名結尾。以下是我正在使用的代碼:Java java.net.MalformedURLException:無協議從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();
}
}
示例URL是:i.imgur.com/fq4ZpIEb.jpg。
但是,每次我運行此代碼返回此錯誤消息:
java.net.MalformedURLException: no protocol: i.imgur.com/fq4ZpIEb.jpg
有誰知道一個解決方案嗎?謝謝!
感謝您給我們! :d –