DEV Community

Ajit Singh
Ajit Singh

Posted on

file extension from byte array

0

Below is the code snippet to get the file extension

public static Map<String, String> getImageType(byte[] imageContent) throws Exception {
        Map<String, String> result = new HashMap<String, String>()
        TikaConfig config = TikaConfig.getDefaultConfig();
        MediaType mediaType = config.getMimeRepository().detect(new ByteArrayInputStream(imageContent), new Metadata());
        MimeType mimeType = config.getMimeRepository().forName(mediaType.toString())
        result.put("extn", mimeType.getExtension().replaceFirst(".", ""));
        return result;
    }

tika-core dependency used…

Top comments (0)