Explain MIME Type
A MIME (Multipurpose Internet Mail Extensions) type is a standard way to indicate the nature and format of a document or file. It is used on the internet to categorize and interpret different types of data, ensuring that browsers and other applications handle them correctly.
- Purpose: MIME types specify the type of data contained in a file, allowing browsers and servers to handle files appropriately.
- Format: A MIME-type consists of a type and a subtype, separated by a slash (e.g., text/html, image/jpeg, application/json).
ExamplesÂ
- text/html: HTML document
- image/jpeg: JPEG image file
- application/json: JSON data

Table of Contents
Java Example
In Java, MIME types are often used when handling HTTP requests and responses, especially when dealing with file uploads or downloads.
Example: Handling File Upload with MIME-Type
Let’s demonstrate how to handle file uploads and retrieve the MIME of the uploaded file using Servlets in Java.
Example
1. Servlet to Handle File Upload:
java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
@WebServlet("/uploadFile")
@MultipartConfig
public class FileUploadServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
// Retrieves <input type="file" name="file">
Part filePart = request.getPart("file");
String fileName = filePart.getSubmittedFileName();
String contentType = filePart.getContentType();
response.getWriter().write("File " + fileName + " uploaded successfully.<br>");
response.getWriter().write("MIME Type: " + contentType);
}
}
Explanation of the MIME
- Servlet Configuration: The @MultipartConfig annotation enables the servlet to handle file uploads.
- File Upload Handling:
- The doPost method handles POST requests containing a file upload.
- It retrieves the uploaded file using request.getPart(“file”).
- filePart.getSubmittedFileName() retrieves the name of the uploaded file.
- filePart.getContentType() retrieves the MIME type of the uploaded file.