Test if a file is an image file
Solution 1
This works pretty well for me. Hope I could help
import javax.activation.MimetypesFileTypeMap;
import java.io.File;
class Untitled {
public static void main(String[] args) {
String filepath = "/the/file/path/image.jpg";
File f = new File(filepath);
String mimetype= new MimetypesFileTypeMap().getContentType(f);
String type = mimetype.split("/")[0];
if(type.equals("image"))
System.out.println("It's an image");
else
System.out.println("It's NOT an image");
}
}
Solution 2
if( ImageIO.read(*here your input stream*) == null)
*IS NOT IMAGE*
And also there is an answer: How to check a uploaded file whether it is a image or other file?
Solution 3
In Java 7, there is the java.nio.file.Files.probeContentType() method. On Windows, this uses the file extension and the registry (it does not probe the file content). You can then check the second part of the MIME type and check whether it is in the form <X>/image
.
Solution 4
You may try something like this:
String pathname="abc\xyz.png"
File file=new File(pathname);
String mimetype = Files.probeContentType(file.toPath());
//mimetype should be something like "image/png"
if (mimetype != null && mimetype.split("/")[0].equals("image")) {
System.out.println("it is an image");
}
Solution 5
You may try something like this:
import javax.activation.MimetypesFileTypeMap;
File myFile;
String mimeType = new MimetypesFileTypeMap().getContentType( myFile ));
// mimeType should now be something like "image/png"
if(mimeType.substring(0,5).equalsIgnoreCase("image")){
// its an image
}
this should work, although it doesn't seem to be the most elegant version.
lancegerday
I am a college student Majoring in Computer Engineering. I know Java, C++, C#, Python and have used SQL and asp.NET
Updated on July 18, 2022Comments
-
lancegerday almost 2 years
I am using some file
IO
and want to know if there is a method to check if a file is an image? -
GameDroids about 12 yearssorry, when I wrote my answer yours wasn't there... maybe it took me too long to write it. But you are right, its completely the same :)
-
Ismael about 12 yearseheh, no problem. I think it happens to everyone all the time in here. :)
-
Stephen C about 12 years@ismaelga - Indeed, it happens so often that most people wouldn't even bother to comment on it.
-
Ismael about 12 years@StephenC - I am a bad person, I know. I just commented because of the gap of something like 30 minutes, but i know it's still a normal thing. :) and i'm not being sarcastic
-
Ismael over 10 yearshi @dermoritz This seems to be a problem on your system. Check out this SO post stackoverflow.com/questions/4855627/…
-
WowBow about 9 yearsHow about if it is from URL? Before saving the URL to a file? I got "application/octet-stream" instead of image. Any idea ?
-
wangqi060934 almost 9 years@dermoritz,me too."new MimetypesFileTypeMap().getContentType(path)" returns "application/octet-stream",but "URLConnection.guessContentTypeFromName(path)" returns the right mime type.
-
gstackoverflow almost 9 yearsDoes it checks file content or just extension?
-
Alex Fedulov over 8 yearsThis only checks based on the extension of the file. Non-valid images (other binaries without image content and proper file headers) cannot be detected using this method.
-
Ismael over 8 yearsYou are right @Alex Fedulov. Try prunge s answer if this is not enough to you
-
Biagio Cannistraro about 8 yearsThis is a bad way to test if the file is an image. a better solution is: Files.probeContentType(path);
-
Ismael about 8 years@BiagioCannistraro You might want to add that as another answer
-
yngwietiger almost 8 yearsOn Linux, it seems to only look at the extension on my machine. Changed a .jpeg file to .txt and it returned "text/plain". I like Krystian's answer below.
-
zafar142003 over 7 yearsThis is the only way I could find if a file is an image file (on an Ubuntu system with Java 8).