2013-11-27 160 views
0

我必須將圖像URL轉換爲圖像。爲此,我嘗試了將base64轉換爲圖像的編碼。在調試代碼後,「Bufferedimage image」在ByteArrayInputStream bis=new ByteArrayInputStream(imagebyte)之後始終爲空。我能做什麼?如何將Base64(imageurl)轉換爲圖像

String imageStr = request.getParameter("imgURL"); 
    BufferedImage image = null; 

    try { 
     BASE64Decoder decoder = new BASE64Decoder(); 
    byte[] imageByte = decoder.decodeBuffer(imageStr); 
     ByteArrayInputStream bis = new ByteArrayInputStream(imageByte); 
     image = ImageIO.read(bis); 
     File outputfile = new File("E:\\saved.png"); 
     ImageIO.write(image, "png", outputfile); 
     bis.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
+0

您試圖將URL解碼爲圖像。該URL會告訴你圖像在哪裏! – kutschkem

+1

請參閱[此答案](http://stackoverflow.com/a/10132345/418556)。如果你不能從中獲益,請發佈[SSCCE](http://sscce.org/)。 –

回答

0

我必須像網址轉換爲圖像。

如果這是您的唯一要求,那麼這是否會做?

URL url = new URL("www.example.com/image.png"); 
BufferedImage image = ImageIO.read(url); 
File outputfile = new File("E:\\saved.png"); 
ImageIO.write(image, "png", outputfile);