I have the below code for saving an image from a byte array.
I am able to save the image successfully using the below code.
Currently I am saving image with
There are many solutions. Very simple for example:
String contentType = URLConnection.guessContentTypeFromStream(new ByteArrayInputStream(imageBytes));
Or you can use third-party libraries. Like Apache Tika:
String contentType = new Tika().detect(imageBytes);
Would you use the prefix header data:image/png;base64
of the base64 image data?
From the RFC, the "data" url definition:
### Form : `data:[<mediatype>][;base64],<data>`
### Syntax:
`dataurl := "data:" [ mediatype ] [ ";base64" ] "," data
mediatype:= [ type "/" subtype ] *( ";" parameter )
data := *urlchar
parameter := attribute "=" value`
Samples:
data:image/gif;base64,R0lGODdhMAAwAPAAAAAAAP///ywAAAAAMAAw......
Then you can get the extension with gif.