Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3340996
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:40:19+00:00 2026-05-18T00:40:19+00:00

In my web application I have an image uploading module. I want to check

  • 0

In my web application I have an image uploading module. I want to check the uploaded file whether it’s an image file or any other file. I am using Java in server side.

The image is read as BufferedImage in java and then I am writing it to disk with ImageIO.write()

How shall I check the BufferedImage, whether it’s really an image or something else?

Any suggestions or links would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T00:40:20+00:00Added an answer on May 18, 2026 at 12:40 am

    I’m assuming that you’re running this in a servlet context. If it’s affordable to check the content type based on just the file extension, then use ServletContext#getMimeType() to get the mime type (content type). Just check if it starts with image/.

    String fileName = uploadedFile.getFileName();
    String mimeType = getServletContext().getMimeType(fileName);
    if (mimeType.startsWith("image/")) {
        // It's an image.
    } else {
        // It's not an image.
    }
    

    The default mime types are definied in the web.xml of the servletcontainer in question. In for example Tomcat, it’s located in /conf/web.xml. You can extend/override it in the /WEB-INF/web.xml of your webapp as follows:

    <mime-mapping>
        <extension>svg</extension>
        <mime-type>image/svg+xml</mime-type>
    </mime-mapping>
    

    But this doesn’t prevent you from users who are fooling you by changing the file extension. If you’d like to cover this as well, then you can also determine the mime type based on the actual file content. If it’s affordable to check for only BMP, GIF, JPEG, PNG, TIFF or WBMP types (but not PSD, SVG, etc), then you can just feed it directly to ImageIO#read() and check if it doesn’t throw an exception.

    try (InputStream input = uploadedFile.getInputStream()) {
        try {
            ImageIO.read(input).toString();
            // It's an image (only BMP, GIF, JPEG, PNG, TIFF and WBMP are recognized).
        } catch (Exception e) {
            // It's not an image.
        }
    }
    

    But if you’d like to cover more image types as well, then consider using a 3rd party library which does all the work by sniffing the file signatures. For example Apache Tika which recognizes on top of ImageIO formats also PSD, BPG, WEBP, ICNS and SVG as well:

    Tika tika = new Tika();
    try (InputStream input = uploadedFile.getInputStream()) {
        String mimeType = tika.detect(input);
        if (mimeType.startsWith("image/")) {
            // It's an image.
        } else {
            // It's not an image.
        }
    }
    

    You could if necessary use combinations and outweigh the one and other.

    That said, you don’t necessarily need ImageIO#write() to save the uploaded image to disk. Just writing the obtained InputStream directly to a Path or any OutputStream like FileOutputStream the usual Java IO way is more than sufficient (see also Recommended way to save uploaded files in a servlet application):

    try (InputStream input = uploadedFile.getInputStream()) {
        Files.copy(input, new File(uploadFolder, fileName).toPath());
    }
    

    Unless you’d like to gather some image information like its dimensions and/or want to manipulate it (crop/resize/rotate/convert/etc) of course.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my program, I have a web application that creates a PNG image. This
I want to create a java application that allows users to import a web
I have a web application in a servlet container. How can I get the
I have a folder inside my ASP.NET web application. I put all css files
I am using web service in my application and somewhere need to display images
I am trying to animate an image using following code: [UIView beginAnimations:nil context:NULL]; [UIView
I'm trying to POST some data as if I was using FORM on HTML
I'm diving into something without sufficient background, but I feel like there may be
i'm really freaking out cause i don't know how to find example or documentation
Hii, I am creating a WPF aplication. My home page will look like the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.