java判别图片格式GIF JPG

原文地址:http://blog.csdn.net/kehengqun1/article/details/49252549感谢博主
判断文件是否为GIF文件
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;
}

判断文件是否为JPG文件
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;
}

猜你喜欢

转载自yeluotiying.iteye.com/blog/2347449