Snippet

  1. import java.awt.image.BufferedImage;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.File;
  4.  
  5. import javax.imageio.ImageIO;
  6.  
  7. import sun.misc.BASE64Decoder;
  8.  
  9. public class Base64ImageTest
  10. {
  11.     public static void main(String args[]);
  12.      {
  13.         // this is not the real stream just for example
  14.         String base64String
  15. = "BORw0KGgoAAAANSUhEUgAAAUAAAAHgCAYAAADUjLREAAAgAElEQVR4AexdB4B
  16. U1dX+ZmZ7ZWGX3pHeu6goitgQDCZGjdHYu4nGqL81mmaJvdd";
  17.  
  18.         BASE64Decoder decoder = new BASE64Decoder();
  19.         byte[] decodedBytes = decoder.decodeBuffer(base64String);
  20.         log.debug("Decoded upload data : " + decodedBytes.length);
  21.  
  22.            
  23.            
  24.          String uploadFile = "/tmp/test.png";
  25.          log.debug("File save path : " + uploadFile);
  26.  
  27.          BufferedImage image = ImageIO.read(new ByteArrayInputStream(decodedBytes));
  28.          if (image == null) {
  29.               log.error("Buffered Image is null");
  30.           }
  31.          File f = new File(uploadFile);
  32.  
  33.          // write the image
  34.           ImageIO.write(image, "png", f);
  35.       }
  36. }

<!>