2013-11-27 164 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);