I have been trying to figure out how to fetch the MIME type from byte array in Java 6, but unfortunately have not been able fetch the MIME type yet.
Can someone help me get out of this?
For detecting MIME-types, use the aptly named "mimetype" command. It has a number of options for formatting the output, it even has an option for backward compatibility to "file". But most of all, it accepts input not only as file, but also via stdin/pipe, so you can avoid temporary files when processing streams.
Using getFileNameMap() The method returns the table of MIME types used by all instances of URLConnection. This table is then used to resolve the input file type.
The mediaType of this bytearray can be of any type. The code used now for fetching byte is below. HttpHeaders headers = new HttpHeaders(); headers.
A Multipurpose Internet Mail Extension (MIME) type, as defined in RFC 2045 and 2046.
You can use the MimetypesFileTypeMap
provided class from Java 6. This class is exclusively used to fetch the MIME type.
Use it to fetch the MIME type as shown below:
byte[] content = ;
InputStream is = new BufferedInputStream(new ByteArrayInputStream(content));
String mimeType = URLConnection.guessContentTypeFromStream(is);
For fetching from File you can use below code:
MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
String mime = mimeTypesMap.getContentType(file);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With