Java判断文件是否是图片

private boolean checkFile(MultipartFile file) throws IOException {
    String imageType = getImageType(ImageIO.createImageInputStream(file.getInputStream()));
    return !StringUtils.isEmpty(imageType);
}
/**
     * 获取图片类型,图片类型返回图片格式,例如JPEG
     * 非图片类型返回null
     *
     * @param inputStream
     * @return
     * @throws IOException
     */
    private static String getImageType(ImageInputStream inputStream) throws IOException {
        final Iterator<ImageReader> imageReaders = ImageIO.getImageReaders(inputStream);
        if (imageReaders.hasNext()) {
            final ImageReader imageReader = imageReaders.next();
            return imageReader.getFormatName();
        }
        return null;
    }

你可能感兴趣的:(Java,Web,java,判断是否图片)