Aliuk
Aliuk

Reputation: 1359

Java - Get image file extension given its source and its mime type

Is there a way or a library in Java to get the extension of an image file given its source as a byte array and its Mime type?

I am asking this since the same mime type can be used for different extensions.

Upvotes: 2

Views: 2221

Answers (1)

xuso
xuso

Reputation: 715

As other said, it's not 100% reliable, but I know Apache Tika can do that.

You don't need the content if you already have the mime type:

MimeType type = MimeTypes.getDefaultMimeTypes().forName("image/png");
String extension = type.getExtension(); //.png

Upvotes: 3

Related Questions