Test if a file is an image file

后端 未结 8 1249
情深已故
情深已故 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");
    }
    

提交回复
热议问题