說明:如果根據后綴名來判斷文件的類型的話是不靠譜的,因為后綴名一旦被手動重命名之后,是無法判斷的,所以需要根據文件的編碼方式或者文件頭信息來判斷,因為這些是無法手動改變的。
方法一 根據編碼方式獲取圖片的類型:
1 package com.test01; 2 3 import javax.imageio.ImageIO; 4 import javax.imageio.ImageReader; 5 import javax.imageio.stream.ImageInputStream; 6 import java.io.File; 7 import java.io.FileInputStream; 8 import java.io.IOException; 9 import java.util.ArrayList; 10 import java.util.Iterator; 11 import java.util.List; 12 13 /** 14 * @Description: Test02 15 * @Author: luzhiming 16 * @CreateDate: 2021/9/13 10:28 17 * @Version: 1.0 18 */ 19 public class GetImageTypeTest { 20 21 public static void main(String[] args) throws IOException { 22 String rootPath = System.getProperty("user.dir"); 23 List<String> list = new ArrayList<>(); 24 list.add(rootPath + "/image-demo/file/input/1.jpg"); 25 // 修改過后綴名 jiang webp 修改為 jpg 26 list.add(rootPath + "/image-demo/file/input/2.jpg"); 27 list.add(rootPath + "/image-demo/file/input/1111.png"); 28 list.add(rootPath + "/image-demo/file/input/100.webp"); 29 list.forEach(filename -> { 30 try { 31 getImageType(filename); 32 } catch (IOException e) { 33 e.printStackTrace(); 34 } 35 }); 36 } 37 38 public static void getImageType(String filename) throws IOException { 39 File file = new File(filename); 40 ImageInputStream image = ImageIO.createImageInputStream(new FileInputStream(file)); 41 Iterator<ImageReader> readers = ImageIO.getImageReaders(image); 42 String formatName = readers.next().getFormatName(); 43 System.out.println(formatName); 44 } 45 }
控制台輸出:
JPEG WebP JPEG WebP
方法二 將圖片的信息讀成流轉16進制,根據文件流判斷文件類型(推薦使用)
1 package com.test01; 2 3 import java.io.*; 4 import java.util.ArrayList; 5 import java.util.List; 6 7 public class ImageTypeUtils { 8 /** 9 * 常用文件的文件頭如下:(以前六位為准) 10 * JPEG (jpg),文件頭:FFD8FF 11 * PNG (png),文件頭:89504E47 12 * GIF (gif),文件頭:47494638 13 * TIFF (tif),文件頭:49492A00 14 * Windows Bitmap (bmp),文件頭:424D 15 * CAD (dwg),文件頭:41433130 16 * Adobe Photoshop (psd),文件頭:38425053 17 * Rich Text Format (rtf),文件頭:7B5C727466 18 * XML (xml),文件頭:3C3F786D6C 19 * HTML (html),文件頭:68746D6C3E 20 * Email [thorough only] (eml),文件頭:44656C69766572792D646174653A 21 * Outlook Express (dbx),文件頭:CFAD12FEC5FD746F 22 * Outlook (pst),文件頭:2142444E 23 * MS Word/Excel (xls.or.doc),文件頭:D0CF11E0 24 * MS Access (mdb),文件頭:5374616E64617264204A 25 * WordPerfect (wpd),文件頭:FF575043 26 * Postscript (eps.or.ps),文件頭:252150532D41646F6265 27 * Adobe Acrobat (pdf),文件頭:255044462D312E 28 * Quicken (qdf),文件頭:AC9EBD8F 29 * Windows Password (pwl),文件頭:E3828596 30 * ZIP Archive (zip),文件頭:504B0304 31 * RAR Archive (rar),文件頭:52617221 32 * Wave (wav),文件頭:57415645 33 * AVI (avi),文件頭:41564920 34 * Real Audio (ram),文件頭:2E7261FD 35 * Real Media (rm),文件頭:2E524D46 36 * MPEG (mpg),文件頭:000001BA 37 * MPEG (mpg),文件頭:000001B3 38 * Quicktime (mov),文件頭:6D6F6F76 39 * Windows Media (asf),文件頭:3026B2758E66CF11 40 * MIDI (mid),文件頭:4D546864 41 */ 42 43 44 public static final String TYPE_JPG = ".jpg"; 45 public static final String TYPE_GIF = ".gif"; 46 public static final String TYPE_PNG = ".png"; 47 public static final String TYPE_BMP = ".bmp"; 48 public static final String TYPE_WEBP = ".webp"; 49 public static final String TYPE_TIF = ".tif"; 50 public static final String TYPE_UNKNOWN = "unknown"; 51 52 53 public static void main(String[] args) throws FileNotFoundException { 54 String rootPath = System.getProperty("user.dir"); 55 List<String> list = new ArrayList<>(); 56 list.add(rootPath + "/image-demo/file/input/1.jpg"); 57 // 修改過后綴名 jiang webp 修改為 jpg 58 list.add(rootPath + "/image-demo/file/input/2.jpg"); 59 list.add(rootPath + "/image-demo/file/input/1111.png"); 60 list.add(rootPath + "/image-demo/file/input/100.webp"); 61 list.forEach(filename -> { 62 try { 63 File pdfFile = new File(filename); 64 // test code 65 System.out.println("圖片格式:" + getPicType(new FileInputStream(pdfFile))); 66 } catch (IOException e) { 67 e.printStackTrace(); 68 } 69 }); 70 } 71 72 /** 73 * byte數組轉換成16進制字符串 74 * 75 * @param src 76 * @return 77 */ 78 public static String bytesToHexString(byte[] src) { 79 StringBuilder stringBuilder = new StringBuilder(); 80 if (src == null || src.length <= 0) { 81 return null; 82 } 83 for (int i = 0; i < src.length; i++) { 84 int v = src[i] & 0xFF; 85 String hv = Integer.toHexString(v); 86 if (hv.length() < 2) { 87 stringBuilder.append(0); 88 } 89 stringBuilder.append(hv); 90 } 91 return stringBuilder.toString(); 92 } 93 94 /** 95 * 根據文件流判斷圖片類型 96 * 97 * @param fis 98 * @return jpg/png/gif/bmp 99 */ 100 public static String getPicType(FileInputStream fis) { 101 //讀取文件的前幾個字節來判斷圖片格式 102 byte[] b = new byte[4]; 103 try { 104 fis.read(b, 0, b.length); 105 String type = bytesToHexString(b).toUpperCase(); 106 if (type.contains("FFD8FF")) { 107 return TYPE_JPG; 108 } else if (type.contains("89504E47")) { 109 return TYPE_PNG; 110 } else if (type.contains("47494638")) { 111 return TYPE_GIF; 112 } else if (type.contains("424D")) { 113 return TYPE_BMP; 114 } else if (type.contains("52494646")) { 115 return TYPE_WEBP; 116 } else if (type.contains("49492A00")) { 117 return TYPE_TIF; 118 } else { 119 return TYPE_JPG; 120 } 121 } catch (IOException e) { 122 e.printStackTrace(); 123 } finally { 124 if (fis != null) { 125 try { 126 fis.close(); 127 } catch (IOException e) { 128 e.printStackTrace(); 129 } 130 } 131 } 132 return null; 133 } 134 135 /** 136 * 根據文件流判斷圖片類型 137 * 138 * @param fis 139 * @return jpg/png/gif/bmp 140 */ 141 public static String getPicType2(InputStream fis) { 142 //讀取文件的前幾個字節來判斷圖片格式 143 byte[] b = new byte[4]; 144 try { 145 fis.read(b, 0, b.length); 146 String type = bytesToHexString(b).toUpperCase(); 147 if (type.contains("FFD8FF")) { 148 return TYPE_JPG; 149 } else if (type.contains("89504E47")) { 150 return TYPE_PNG; 151 } else if (type.contains("47494638")) { 152 return TYPE_GIF; 153 } else if (type.contains("424D")) { 154 return TYPE_BMP; 155 } else if (type.contains("52494646")) { 156 return TYPE_WEBP; 157 } else if (type.contains("49492A00")) { 158 return TYPE_TIF; 159 } else { 160 return null; 161 } 162 } catch (IOException e) { 163 e.printStackTrace(); 164 } finally { 165 if (fis != null) { 166 try { 167 fis.close(); 168 } catch (IOException e) { 169 e.printStackTrace(); 170 } 171 } 172 } 173 return TYPE_UNKNOWN; 174 } 175 }
控制台輸出:
1 圖片格式:.jpg 2 圖片格式:.webp 3 圖片格式:.jpg 4 圖片格式:.webp