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);

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

Leave a Reply