I am receiving image DataURL in my java servlet it looks like:
I need to save it as an image file, how can I do that?
The simplest way1 to do it is as follows:
String str = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAA...";
byte imagedata = DatatypeConverter.parseBase64Binary(str.substring(str.indexOf(",") + 1));
BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(imagedata));
ImageIO.write(bufferedImage, "png", new File("img.png"));
- In order to use the class
javax.xml.bind.DatatypeConverter, you need Java 6 o greater.