Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fetch the MIME type from byte array in Java 6?

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?

like image 938
Dinesh Padmanabhan Avatar asked Nov 30 '15 12:11

Dinesh Padmanabhan


People also ask

How do I find MIME type?

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.

Which method returns the MIME type for currently set for response?

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.

What is the media type for byte array?

The mediaType of this bytearray can be of any type. The code used now for fetching byte is below. HttpHeaders headers = new HttpHeaders(); headers.

What is MIME type Java?

A Multipurpose Internet Mail Extension (MIME) type, as defined in RFC 2045 and 2046.


1 Answers

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);
like image 112
mayank agrawal Avatar answered Sep 18 '22 16:09

mayank agrawal