Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

When working with file uploads in Java, it’s crucial to ensure that the uploaded files are indeed images, especially when filenames and extensions can be misleading.

In this tutorial, we’ll explore two ways to determine whether a file is an image: Checking the file’s actual content and verifying based on the file’s extension.

2. Checking File Content

One of the most reliable ways to determine if a file is an image is by inspecting its content. Let’s explore two methods to do this: Using Apache Tika and then using the built-in Java ImageIO class.

2.1. Using Apache Tika

mimeApache Tika is a powerful library for detecting and extracting metadata from various file types.

Let’s add Apache Tika core to our project dependencies:

<dependency>
    <groupId>org.apache.tika</groupId>
    <artifactId>tika-core</artifactId>
    <version>2.9.2</version>
</dependency>

Then, we can implement a method to check if a file is an image using this library:

public static boolean isImageFileUsingTika(File file) throws IOException {
    Tika tika = new Tika();
    String mimeType = tika.detect(file);
    return mimeType.startsWith("image/");
}

Apache Tika does not read the whole file into memory, only the first few bytes to check. Therefore, we should use Tika with trusted sources because checking only the first few bytes also means that attackers may be able to smuggle in executables that aren’t images.

2.2. Using Java ImageIO Class

Java’s built-in ImageIO class can also determine if a file is an image by attempting to read the file as an image:

public static boolean isImageFileUsingImageIO(File file) throws IOException {
    BufferedImage image = ImageIO.read(file);
    return image != null;
}

The ImageIO.read() method reads the whole file into memory, so it is inefficient if we only want to test if this file is an image.

3. Checking File Extension

A simpler but less reliable method is to check the file extension. This method doesn’t guarantee that the file content matches the extension, but it’s faster and easier.

Java’s built-in Files.probeContentType() method can determine the MIME type of a file based on its extension. Here’s how we can use it:

public static boolean isImageFileUsingProbeContentType(File file) throws IOException {
    Path filePath = file.toPath();
    String mimeType = Files.probeContentType(filePath);
    return mimeType != null && mimeType.startsWith("image/");
}

Of course, we can always write a Java method ourselves to check if a file extension is in a predefined list.

4. Summary of Methods

Let’s compare the pros and cons of each method:

  • Checking file content:
    • Using Apache Tika: Reads the first few bytes of a file. It’s reliable and efficient but should be used with trusted sources.
    • Using Java ImageIO: Attempts to read the file as an image. It’s most reliable but inefficient.
  • Checking file extension: Determines based on the file’s extension, which is faster and easier, but also doesn’t guarantee that the file content is image.

5. Conclusion

In this tutorial, we explored different methods to check if a file is an image in Java. While checking the file content is more reliable, checking the file extension is faster and easier.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW