大家好!
代码如下:
/**
* 判断图片base64字符串的文件格式
* @param base64
* @return
*/
public static String getBase64Type(String base64) {
byte[] b = Base64.getDecoder().decode(base64);
String type = "";
if (0x424D == ((b[0] & 0xff) << 8 | (b[1] & 0xff))) {
type = "bmp";
} else if (0x8950 == ((b[0] & 0xff) << 8 | (b[1] & 0xff))) {
type = "png";
} else if (0xFFD8 == ((b[0] & 0xff) << 8 | (b[1] & 0xff))) {
type = "jpg";
} else if (0x49492A00 == ((b[0] & 0xff) << 24 | (b[1] & 0xff) << 16 | (b[2] & 0xff) << 8 | (b[3] & 0xff))) {
type = "tif";
}
return type;
}
只需将base64字符串传入即可,此处我只对以上类型进行了判断,欢迎补充~?