How to Accurately Detect File MIME Types in Java: 6 Practical Methods

This article explains why validating file types is essential for security and demonstrates six Java-based techniques—including Java 7 API, URLConnection, MimeTypesFileTypeMap, jMimeMagic, Apache Tika, and Spring's MediaTypeFactory—to reliably determine a file's MIME type under various conditions.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
How to Accurately Detect File MIME Types in Java: 6 Practical Methods

1. Introduction

Validating file types helps prevent malicious uploads that could contain viruses, trojans, or other harmful code. By checking the MIME type you reduce the risk of executing unsafe content on the server.

This article introduces several strategies for obtaining a file's MIME type in Java and explains when each approach is preferable.

2. Practical Cases

2.1 Using Java 7 API

Java 7 provides Files.probeContentType(Path) which relies on installed FileTypeDetector implementations.

Path path = new File("d:/images/1.png").toPath();
String mimeType = Files.probeContentType(path);

2.2 Using URLConnection

Way 1 : Retrieve the MIME type via URLConnection.getContentType().

File file = new File("d:/images/1.png");
URLConnection connection = file.toURI().toURL().openConnection();
String mimeType = connection.getContentType();

Way 2 : Guess the type from the file name.

File file = new File("d:/images/1.png");
String mimeType = URLConnection.guessContentTypeFromName(file.getName());

Way 3 : Use FileNameMap to obtain the content type.

File file = new File("d:/images/1.png");
FileNameMap fileNameMap = URLConnection.getFileNameMap();
String mimeType = fileNameMap.getContentTypeFor(file.getName());

2.3 Using MimeTypesFileTypeMap

This class resolves MIME types based on file extensions and searches a series of mime.types files.

File file = new File("d:/images/1.png");
MimetypesFileTypeMap fileTypeMap = new MimetypesFileTypeMap();
String mimeType = fileTypeMap.getContentType(file.getName());

If no match is found, application/octet-stream is returned.

2.4 Using jMimeMagic

jMimeMagic is a licensed library that can detect MIME types from streams, so the file does not need to exist on the filesystem.

<dependency>
  <groupId>net.sf.jmimemagic</groupId>
  <artifactId>jmimemagic</artifactId>
  <version>0.1.5</version>
</dependency>
File file = new File("d:/images/1.png");
MagicMatch match = Magic.getMagicMatch(file, false);
System.out.println(match.getMimeType());

2.5 Using Apache Tika

Tika detects MIME types by inspecting magic bytes, making it reliable even when file extensions are altered.

<dependency>
  <groupId>org.apache.tika</groupId>
  <artifactId>tika-core</artifactId>
  <version>2.9.2</version>
</dependency>
File file = new File("d:/images/1.png");
Tika tika = new Tika();
String mimeType = tika.detect(file);
System.out.println(mimeType);

2.6 Using Spring’s MediaTypeFactory

Part of Spring Web, MediaTypeFactory determines the media type from a filename.

File file = new File("d:/images/1.png");
Optional<MediaType> mimeTypeOptional = MediaTypeFactory.getMediaType(file.getName());
System.out.println(mimeTypeOptional.isPresent() ? mimeTypeOptional.get() : "未知");

Note: This method relies solely on the file extension, so changing the extension can produce an incorrect type.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Spring BootMIME typeFile detection
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.