Test if a file is an image file

后端 未结 8 1235
情深已故
情深已故 2020-12-01 07:55

I am using some file IO and want to know if there is a method to check if a file is an image?

相关标签:
8条回答
  • 2020-12-01 07:59

    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");
    }
    
    0 讨论(0)
  • 2020-12-01 08:04
    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?

    0 讨论(0)
  • 2020-12-01 08:05

    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.

    0 讨论(0)
  • 2020-12-01 08:07

    There are a variety of ways to do this; see other answers and the links to related questions. (The Java 7 approach seems the most attractive to me, because it uses platform specific conventions by default, and you can supply your own scheme for file type determination.)

    However, I'd just like to point out that no mechanism is entirely infallible:

    • Methods that rely on the file suffix will be tricked if the suffix is non-standard or wrong.

    • Methods that rely on file attributes (e.g. in the file system) will be tricked if the file has an incorrect content type attribute or none at all.

    • Methods that rely on looking at the file signature can be tricked by binary files which just happen to have the same signature bytes.

    • Even simply attempting to read the file as an image can be tricked if you are unlucky ... depending on the image format(s) that you try.

    0 讨论(0)
  • 2020-12-01 08:13

    Here's my code based on the answer using tika.

    private static final Tika TIKA = new Tika();
    public boolean isImageMimeType(File src) {
        try (FileInputStream fis = new FileInputStream(src)) {
            String mime = TIKA.detect(fis, src.getName());
            return mime.contains("/") 
                    && mime.split("/")[0].equalsIgnoreCase("image");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    
    0 讨论(0)
  • 2020-12-01 08:16

    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");
        }
    }
    
    0 讨论(0)
提交回复
热议问题