1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
| public class ImgeMimeTypeUtil {
public static String getMimeType(String filename) { try { String mimeType = readType(filename); return String.format("image/%s", mimeType); } catch (IOException e) { e.printStackTrace(); } return null; }
public static String readType(String filename) throws IOException {
FileInputStream fis = null; try { File f = new File(filename); if (!f.exists() || f.isDirectory() || f.length() < 8) { throw new IOException("the file [" + f.getAbsolutePath() + "] is not image !"); }
fis = new FileInputStream(f); byte[] bufHeaders = readInputStreamAt(fis, 0, 8); if (isJPEGHeader(bufHeaders)) { long skiplength = f.length() - 2 - 8; byte[] bufFooters = readInputStreamAt(fis, skiplength, 2); if (isJPEGFooter(bufFooters)) { return "jpeg"; } } if (isPNG(bufHeaders)) { return "png"; } if (isGIF(bufHeaders)) {
return "gif"; } if (isWEBP(bufHeaders)) { return "webp"; } if (isBMP(bufHeaders)) { return "bmp"; } if (isICON(bufHeaders)) { return "ico"; } throw new IOException("the image's format is unkown!");
} catch (FileNotFoundException e) { throw e; } finally { try { if (fis != null) fis.close(); } catch (Exception e) { } }
}
private static boolean compare(byte[] buf, byte[] markBuf) { for (int i = 0; i < markBuf.length; i++) { byte b = markBuf[i]; byte a = buf[i];
if (a != b) { return false; } } return true; }
private static byte[] readInputStreamAt(FileInputStream fis, long skiplength, int length) throws IOException { byte[] buf = new byte[length]; fis.skip(skiplength); int read = fis.read(buf, 0, length); return buf; } private static boolean isBMP(byte[] buf){ byte[] markBuf = "BM".getBytes(); return compare(buf, markBuf); }
private static boolean isICON(byte[] buf) { byte[] markBuf = {0, 0, 1, 0, 1, 0, 32, 32}; return compare(buf, markBuf); } private static boolean isWEBP(byte[] buf) { byte[] markBuf = "RIFF".getBytes(); return compare(buf, markBuf); }
private static boolean isGIF(byte[] buf) { byte[] markBuf = "GIF89a".getBytes(); if(compare(buf, markBuf)) { return true; } markBuf = "GIF87a".getBytes(); if(compare(buf, markBuf)) { return true; } return false; } private static boolean isPNG(byte[] buf) { byte[] markBuf = {(byte) 0x89,0x50,0x4E,0x47,0x0D,0x0A,0x1A,0x0A}; return compare(buf, markBuf); }
private static boolean isJPEGHeader(byte[] buf) { byte[] markBuf = {(byte) 0xff, (byte) 0xd8}; return compare(buf, markBuf); }
private static boolean isJPEGFooter(byte[] buf) { byte[] markBuf = {(byte) 0xff, (byte) 0xd9}; return compare(buf, markBuf); } }
|