Java判断文件是否为图片文件(GIF,PNG,JPG)
生活随笔
收集整理的這篇文章主要介紹了
Java判断文件是否为图片文件(GIF,PNG,JPG)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
查看源代碼:
1 /** 2 * 判斷文件是否為圖片文件(GIF,PNG,JPG) 3 * @param srcFileName 4 * @return 5 */ 6 public static boolean isImage(File srcFilePath) { 7 FileInputStream imgFile = null; 8 byte[] b = new byte[10]; 9 int l = -1; 10 try { 11 imgFile = new FileInputStream(srcFilePath); 12 l = imgFile.read(b); 13 imgFile.close(); 14 } catch (Exception e) { 15 return false; 16 } 17 18 if (l == 10) { 19 byte b0 = b[0]; 20 byte b1 = b[1]; 21 byte b2 = b[2]; 22 byte b3 = b[3]; 23 byte b6 = b[6]; 24 byte b7 = b[7]; 25 byte b8 = b[8]; 26 byte b9 = b[9]; 27 28 if (b0 == (byte) 'G' && b1 == (byte) 'I' && b2 == (byte) 'F') { 29 return true; 30 } else if (b1 == (byte) 'P' && b2 == (byte) 'N' && b3 == (byte) 'G') { 31 return true; 32 } else if (b6 == (byte) 'J' && b7 == (byte) 'F' && b8 == (byte) 'I'&& b9 == (byte) 'F') { 33 return true; 34 } else { 35 return false; 36 } 37 } else { 38 return false; 39 } 40 }?
轉(zhuǎn)載于:https://www.cnblogs.com/tovep/articles/2440082.html
總結(jié)
以上是生活随笔為你收集整理的Java判断文件是否为图片文件(GIF,PNG,JPG)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【编程之美】金刚坐飞机问题
- 下一篇: Javascript弹出对话框 确定取消