MIME Type in Java

MIME Type in Java | The MIME represents Multipurpose Internet Mail Extension and is also called a content type. A MIME type is a label used to identify a type of data. It is used so the software can know how to handle the data. It serves the same purpose on the Internet that file extensions do on Microsoft Windows.

In servlet component,

res.setContentType("<mime-type>");

The servlet component gives instruction to the browser along with the response (output) through the response object, web container, webserver to display the received output/response in the MIME type format.

Based on this content type instruction, the browser software becomes ready to display the web page in different formats on the browser. Example:-

res.setContentType("text/html");

In this example, the servlet component gives instructions to the browser to treat the received output as HTML tags-based text content. So, the web page will be displayed on the browser by recognizing the HTML tags of the output text. 

In most of the browsers “text/html” will be taken as the default MIME type. Therefore if we don’t set any MIME type explicitly in our servlet component then it will be displayed in HTML format.

MIME Types

Some of the popular MIME type in Java while working with web applications are,

MIME Type Display the web page as
text/htmlHTML tags based text content.
text/plainPlain content without formatting.
text/xmlXML tags based text content.
application/mswordMSWord application document.
application/mswordMS Excel application.

Some other popular MIME types are given below. 

  • application/vnd.ms-excel
  • application/jar
  • application/pdf
  • application/octet-stream
  • application/x-zip
  • images/jpeg
  • images/png
  • images/gif
  • audio/mp3
  • video/mp4
  • video/quicktime and e.t.c.

IANA is the official registry of MIME media types and maintains a list of all the official MIME types, see the list here.

How to Find MIME type in Windows

In the Windows operating system, we can find all available MIME types by using regedit (Registry Editor) tool. There we can get MIME types of different file types.

We can get to through:- Open run (WinKey + R) => Search “regedit”. It will open the registry editor. Inside the registry editor, in the “HKEY_CLASSES_ROOT” folder, all extensions are available. Most of those extensions don’t have any MIME type.

Servlet Application Example

Develop a web application having multiple servlet components generating the response in different formats. 

See the code at GitHub. In this example, some results (Excel and Word content type) can’t be displayed directly to the browser. After giving a request to those components, the result will be downloaded to our computer.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Reply