java - check if file is jpg -


I would like to check whether I am reading from a directory the file is a JPG but I just do not want to check the extension. I'm thinking of an option to read the header. I have done some research and I want to use

  imageIO.read   

I have seen an example

  String directory = "/ directory"; Buffed image img = faucet; Try {img = ImageIO.read (new file (directory)); } Hold (IOException e) {// it is not a JPG file}   

I'm not sure where to go from here, it takes it in the complete directory ... but I Does the jpg file in the directory require that someone can tell me what is wrong in my code or what additional needs to be made?

Thank you!

You can read the first bytes stored in the buffer image. This will give you the exact file type

  For example GIF GIF87a or GIF89a JPEG image files begin with FF D8 and end with FF D9   

Try it

  Boolean status = isJPEG (new file ("C: \\ user \\ public \\ pictures \\ Sample image \\ crissamium.jpg ")); System.out.println ("Status:" + Status); Personal static boolean isJPEG (file filename) removes the exception {DataInputStream ins = New DataInputStream (New BufferedInputStream (New FileInputStream (filename))); Try {if (ins.readInt () == 0xffd8ffe0) {return true; } Other {return false; }} Finally {IN.close (); }}    

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -