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;
    }

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

发布了142 篇原创文章 · 获赞 258 · 访问量 16万+

猜你喜欢

转载自blog.csdn.net/conconbenben/article/details/103467532