java判别图片格式GIF JPG

该博客介绍如何使用Java进行图片类型的判别,包括检查文件是否为GIF或JPG格式的方法。
摘要由CSDN通过智能技术生成
原文地址:http://blog.csdn.net/kehengqun1/article/details/49252549感谢博主
[size=medium]判断文件是否为GIF文件[/size]

private boolean isGifFile(File file) {
try {
FileInputStream inputStream = new FileInputStream(file);
int[] flags = new int[5];
flags[0] = inputStream.read();
flags[1] = inputStream.read();
flags[2] = inputStream.read();
flags[3] = inputStream.read();
inputStream.skip(inputStream.available() - 1);
flags[4] = inputStream.read();
inputStream.close();
return flags[0] == 71 && flags[1] == 73 && flags[2] == 70 && flags[3] == 56 && flags[4] == 0x3B;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return false;
}

[size=medium]判断文件是否为JPG文件[/size]

private boolean isJpgFile(File file){
try {
FileInputStream bin = new FileInputStream(file);
int b[] = new int[4];
b[0] = bin.read();
b[1] = bin.read();
bin.skip(bin.available() - 2);
b[2] = bin.read();
b[3] = bin.read();
bin.close();
return b[0] == 255 && b[1] == 216 && b[2] == 255 && b[3] == 217;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值