java判断文件是否是图片的几种方法

java判断文件是否是图片的几种方法


方法1:利用ImageIO创建图片流,创建成功即为图片

    /**
     * @param file 文件路径
     * @return 是否是图片
     */
    public static boolean isImage(File file) {
        if (file!=null && file.exists() && file.isFile()) {
            ImageInputStream iis = null;
            try {
                iis = ImageIO.createImageInputStream(file);
            } catch (IOException e) {
                return false;
            }
            Iterator iter = ImageIO.getImageReaders(iis);
            if (iter.hasNext()) {
                return true;
            }
        }
        return false;
    }

方法2:ImageIO读BufferedImage

    /**
     * @param file 文件路径
     * @return 是否是图片 true-是 false-否
     */
    public static boolean isImage(File file) {
        if (file!=null && file.exists() && file.isFile()) {
            try {
                BufferedImage bi = ImageIO.read(file);
                if (bi != null) {
                    return true;
                }
            } catch (IOException e) {
                return false;
            }

        }
        return false;
    }

方法3:取Image宽高

     /**
     * 通过文件头
     *
     * @param file 源文件
     * @return
     */
    public static boolean isImage(File file) {
        int len = 10;
        BufferedInputStream imgFile = null;
        try {
            imgFile = new BufferedInputStream(new FileInputStream(file));
            Image img;
            try {
                img = ImageIO.read(imgFile);
                return !(img == null || img.getWidth(null) <= 0 || img.getHeight(null) <= 0);
            } catch (Exception e) {
                return false;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (imgFile != null) {
                try {

                    imgFile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

方法4:取头文件(该方法参考阿里开源库simpleimage)

    /**
     * 通过文件头
     *
     * @param file 源文件
     * @return
     */
    public static boolean isImage(File file) {
        int len = 10;
        BufferedInputStream imgFile = null;
        try {
            imgFile = new BufferedInputStream(new FileInputStream(file));
            ImageFormat format = identifyFormat(imgFile);
            if (format != null) {
                return true;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (imgFile != null) {
                try {
                    imgFile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

    public static boolean isJPEG(InputStream source) throws IOException {
        InputStream iis = source;

        if (!source.markSupported()) {
            throw new IllegalArgumentException("Input stream must support mark");
        }

        iis.mark(30);
        // If the first two bytes are a JPEG SOI marker, it's probably
        // a JPEG file. If they aren't, it definitely isn't a JPEG file.
        try {
            int byte1 = iis.read();
            int byte2 = iis.read();
            if ((byte1 == 0xFF) && (byte2 == 0xD8)) {
                return true;
            }
        } finally {
            iis.reset();
        }

        return false;
    }

    public static boolean isBMP(InputStream in) throws IOException {
        if (!in.markSupported()) {
            throw new IllegalArgumentException("Input stream must support mark");
        }
        byte[] b = new byte[2];
        try {
            in.mark(30);
            in.read(b);
        } finally {
            in.reset();
        }

        return (b[0] == 0x42) && (b[1] == 0x4d);
    }

    public static boolean isGIF(InputStream in) throws IOException {
        if (!in.markSupported()) {
            throw new IllegalArgumentException("Input stream must support mark");
        }
        byte[] b = new byte[6];
        try {
            in.mark(30);
            in.read(b);
        } finally {
            in.reset();
        }
        return b[0] == 'G' && b[1] == 'I' && b[2] == 'F' && b[3] == '8' &&
                (b[4] == '7' || b[4] == '9') && b[5] == 'a';
    }

    public static boolean isPNG(InputStream in) throws IOException {
        if (!in.markSupported()) {
            throw new IllegalArgumentException("Input stream must support mark");
        }

        byte[] b = new byte[8];
        try {
            in.mark(30);
            in.read(b);
        } finally {
            in.reset();
        }

        return (b[0] == (byte) 137 && b[1] == (byte) 80 && b[2] == (byte) 78 && b[3] == (byte) 71 &&
                b[4] == (byte) 13
                && b[5] == (byte) 10 && b[6] == (byte) 26 && b[7] == (byte) 10);
    }

    public static boolean isTIFF(InputStream in) throws IOException {
        if (!in.markSupported()) {
            throw new IllegalArgumentException("Input stream must support mark");
        }
        byte[] b = new byte[4];
        try {
            in.mark(30);
            in.read(b);
        } finally {
            in.reset();
        }

        return ((b[0] == (byte) 0x49 && b[1] == (byte) 0x49 && b[2] == (byte) 0x2a &&
                b[3] == (byte) 0x00) || (b[0] == (byte) 0x4d
                && b[1] == (byte) 0x4d
                && b[2] == (byte) 0x00 && b[3] == (byte) 0x2a));
    }

    public static ImageFormat identifyFormat(InputStream in) throws IOException {
        if (isJPEG(in)) {
            return ImageFormat.JPEG;
        }

        if (isPNG(in)) {
            return ImageFormat.PNG;
        }

        if (isGIF(in)) {
            return ImageFormat.GIF;
        }

        if (isBMP(in)) {
            return ImageFormat.BMP;
        }

        if (isTIFF(in)) {
            return ImageFormat.TIFF;
        }

        return null;
    }

该方法有一定局限性,但对常规图片判断还是可行的。

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT_熊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值