How to get the mime type of a file in Java

To get the mime type of a file in Java you can use the following snippet.

Sample Java

String fileName = "/path/to/file.zip";
MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
String mimeType = mimeTypesMap.getContentType(fileName);
File file = new File(fileName);
mimeType = mimeTypesMap.getContentType(file);
System.out.println(mimeType);

Please Share us:
Please rate this snippet

One thought on “How to get the mime type of a file in Java

Leave a Reply