简体   繁体   English

如何将Base64(imageurl)转换为图像

[英]How to convert Base64(imageurl) to an image

I have to convert image URL into an image. 我必须将图像URL转换为图像。 For that I tried following coding for convert base64 to an image. 为此,我尝试了将base64转换为图像的编码。 While on debugging the code "Bufferedimage image" is always null after ByteArrayInputStream bis=new ByteArrayInputStream(imagebyte) . 在调试时,在ByteArrayInputStream bis=new ByteArrayInputStream(imagebyte)之后,代码“ Bufferedimage image”始终为null。 What can I do? 我能做什么?

   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();
    }

I have to convert image URL into an image. 我必须将图像URL转换为图像。

If that is your only requirement, then will this do? 如果这是您的唯一要求,那么会这样做吗?

 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM