Gunjan Shah
Gunjan Shah

Reputation: 5168

MimeType missing in Base64 encoded url

I have requirement to pass an image in json request and I am using javax.Base64 encoder.

I am able to encode the image to Base64 but I found that mime type "data:image/png;base64" is missing in generate encoded String.

So, my encoded String looks like below :

iVBORw0KGgoAAAANSUhEUgAAAPAAAABQCAAAAAACIqegAAABMEl**********

And, here is my simple code which I am trying with junit :

@Test
    public void getBiographicPanel() {
        byte[] image = bacodeGenerator.generateBarocdeImage("12345678");
        System.out.println(Base64.getEncoder().encodeToString(image));
        System.out.println(org.apache.commons.codec.binary.Base64.encodeBase64String(image));

        assertNotNull(image);
    }

How, is that any way or input parameter present in Base64 API which can we configure to generate Base64 image content with MimeType like below ?

data:image/png;base64,iVBORw0KGgoAAAANS

Upvotes: 2

Views: 5989

Answers (1)

LppEdd
LppEdd

Reputation: 21134

The MIME type is

a two-part identifier for file formats and format contents transmitted on the Internet.

That means if you don't need to exchange this file via internet protocols (e.g. HTTP), that type is absolutely irrelevant.


You're also confusing

data:image/png;base64

with a MIME type. That is not a media type.

data: is an actual URL format and is used to specify inline data inside the browser (see IETF).
In your case it would mean "hey browser! Look at this local resource encoded in Base64 to build-up the image!".

This is tipically used in <img /> tags, and must be set manually. That means you have to know the actual format of the data.

Upvotes: 3

Related Questions