2015-10-17 56 views
1

我正在尋找保存圖像從一個網站的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 

有誰知道一個解決方案嗎?謝謝!

回答

1

java.net.MalformedURLException:無協議:i.imgur.com/fq4ZpIEb.jpg

您需要輸入 「http://」

即: 「http://i.imgur.com/fq4ZpIEb.jpg」 將網址。

+0

感謝您給我們! :d –

1

你必須指定協議說httphttpsftp圖像URL

相關問題