判断文件是否为图片格式
原理:1、使用JDK类库解析文件数据,如果能解析成功,获得图片长宽,即为正确的图片格式
2、解析文件头信息,判断图片类型
工具代码
/**
* 判断文件后缀是否为图片文件格式,bmp|gif|jpg|jpeg|png 返回true
* @param imageFileSuffix 图片文件后缀名
* @return
*/
public static boolean isImageBySuffix(String imageFileSuffix) {
if (StringUtils.isNotEmpty(imageFileSuffix)) {
//[JPG, jpg, bmp, BMP, gif, GIF, WBMP, png, PNG, wbmp, jpeg, JPEG]
String[] formatNames = ImageIO.getReaderFormatNames();
if (ArrayUtils.isNotEmpty(formatNames)) {
for (String formatName : formatNames) {
if (imageFileSuffix.toLowerCase().equals(formatName)) {
return true;
}
}
}
}
return false;
}
/**
* 判断文件是否为图片文件格式, bmp|gif|jpg|jpeg|png 后缀的图片文件返回true
* @param imageName 图片文件名
* @return
*/
public static boolean isImageByFileName(String imageName) {
if (StringUtils.isNotEmpty(imageName)) {
String[] imageNames = imageName.split(FILE_CONNECTOR_POINT);
return isImageBySuffix(imageNames[imageNames.length - 1]);
}
return false;
}
/**
* 判断文件是否为合法图片
* @param srcPath 图片文件绝对路径
* @param checkImageName 是否校验图片文件名
* @return
*/
public static boolean isImage(String srcPath, boolean checkImageName) {
if (checkImageName) {
return isImageByFileName(srcPath);
} else {
if (StringUtils.isNotEmpty(srcPath)) {
File imageFile = new File(srcPath);
if (imageFile.isFile() && imageFile.length() > 0) {
return isImage(imageFile);
}
}
}
return false;
}
/**
* 判断文件流是否为合法图片
* @param file 图片文件
* @return
*/
public static boolean isImage(File file) {
return isImage((Object)file);
}
/**
* 判断文件流是否为合法图片
* @param srcInputStream 图片文件的流
* @return
*/
public static boolean isImage(InputStream srcInputStream) {
return isImage((Object)srcInputStream);
}
/**
* 判断文件流是否为合法图片
* @param srcInputStream 图片文件的流
* @return
*/
public static boolean isImage(URL url) {
return isImage((Object)url);
}
/**
* 图片文件读取
* @param obj (URL|InputStream|File)
* @return
*/
private static boolean isImage(Object obj) {
Image image = null;
if (obj != null) {
try {
if (obj instanceof URL) {
image = ImageIO.read((URL)obj);
} else if (obj instanceof InputStream) {
image = ImageIO.read((InputStream)obj);
} else if (obj instanceof File) {
image = ImageIO.read((File)obj);
} else {
throw new IllegalArgumentException("不支持这种类型["+obj.getClass().getCanonicalName()+"]");
}
} catch (IOException e) {
e.printStackTrace();
}
if (image != null && image.getWidth(null) > 0 && image.getHeight(null) > 0) {
return true;
}
}
return false;
}
/**
* 获取图片类型
* JPG图片头信息:FFD8FF
* PNG图片头信息:89504E47
* GIF图片头信息:47494638
* BMP图片头信息:424D
*
* @param is 图片文件流
* @return 图片类型:jpg|png|gif|bmp
*/
public static String getImageType(InputStream is) {
String type = null;
if (is != null) {
byte[] b = new byte[4];
try {
is.read(b, 0, b.length);
} catch (IOException e) {
e.printStackTrace();
}
String hexStr = HexConverter.byteArrayToHexString(b, true);//图片文件流前4个字节的头信息(子文字母)
if (hexStr != null) {
if (hexStr.startsWith(JPG_HEX)) {
type = JPG;
} else if (hexStr.startsWith(PNG_HEX)) {
type = PNG;
} else if (hexStr.startsWith(GIF_HEX)) {
type = GIF;
} else if (hexStr.startsWith(BMP_HEX)) {
type = BMP;
}
}
}
return type;
}
/**
* 获取图片类型
* JPG图片头信息:FFD8FF
* PNG图片头信息:89504E47
* GIF图片头信息:47494638
* BMP图片头信息:424D
*
* @param file 图片文件
* @return 图片类型:jpg|png|gif|bmp
* @throws FileNotFoundException 未找到文件
*/
public static String getImageType(File file) throws FileNotFoundException {
return getImageType(new FileInputStream(file));
}
/**
* 根据文件头,判断文件流是否为合法图片
* @param is 文件流
* @return
*/
public static boolean isImageByHeader(InputStream is) {
return isImageBySuffix(getImageType(is));
}
/**
* 根据文件头,判断文件流是否为合法图片
* @param file 图片文件
* @return
* @throws FileNotFoundException
*/
public static boolean isImageByHeader(File file) throws FileNotFoundException {
return isImageBySuffix(getImageType(file));
}
测试代码
/**
* 测试文件是否为图片
* @throws FileNotFoundException
* @throws MalformedURLException
*/
@SuppressWarnings("resource")
@Test
public void testIsImage() throws FileNotFoundException, MalformedURLException {
String imageName = "simple.jpg";
String srcPath = IMAGE_PATH + imageName;
Assert.assertTrue(ImageUtil.isImage(srcPath, true));
Assert.assertTrue(ImageUtil.isImage(new File(srcPath)));
Assert.assertTrue(ImageUtil.isImage(new FileInputStream(srcPath)));
String baiduLogoUrl = "https://www.baidu.com/img/bd_logo1.png";//百度logo图片
Assert.assertTrue(ImageUtil.isImage(new URL(baiduLogoUrl)));
String textName = "text.txt";
String txtPath = FILE_PATH + textName;
Assert.assertFalse(ImageUtil.isImage(txtPath, true));
}
@SuppressWarnings("resource")
@Test
public void testIsImageByHeader() throws FileNotFoundException {
String imageName = "simple.jpg";
String srcPath = IMAGE_PATH + imageName;
Assert.assertTrue(ImageUtil.isImageByHeader(new FileInputStream(srcPath)));
imageName = "bd_logo1.png";
srcPath = IMAGE_PATH + imageName;
Assert.assertTrue(ImageUtil.isImageByHeader(new File(srcPath)));
imageName = "simple.bmp";
srcPath = IMAGE_PATH + imageName;
Assert.assertTrue(ImageUtil.isImageByHeader(new File(srcPath)));
imageName = "simple.gif";
srcPath = IMAGE_PATH + imageName;
Assert.assertTrue(ImageUtil.isImageByHeader(new File(srcPath)));
}
完整源码:https://github.com/ConstXiong/xtools
【Java面试题与答案】整理推荐