java 多种方式判断文件是否为图片

    xiaoxiao2022-07-07  157

    在很多时间我们需要判断一个文件是否为图片,再或者我们需要获取一个文件的类型是否为我们需要的(这种场景在上传文件接收的时候,非常必要)

    方式一:使用ImageIO 判断图片宽高

    public static boolean isImage(InputStream inputStream) { if (inputStream == null) { return false; } Image img; try { img = ImageIO.read(inputStream); return !(img == null || img.getWidth(null) <= 0 || img.getHeight(null) <= 0); } catch (Exception e) { return false; } }

    方式二:判断文件头信息

    public static String getImageType(File srcFilePath) { FileInputStream imgFile; byte[] b = new byte[10]; int l = -1; try { imgFile = new FileInputStream(srcFilePath); l = imgFile.read(b); imgFile.close(); } catch (Exception e) { return null; } if (l == 10) { byte b0 = b[0]; byte b1 = b[1]; byte b2 = b[2]; byte b3 = b[3]; byte b6 = b[6]; byte b7 = b[7]; byte b8 = b[8]; byte b9 = b[9]; if (b0 == (byte) 'G' && b1 == (byte) 'I' && b2 == (byte) 'F') { return "gif"; } else if (b1 == (byte) 'P' && b2 == (byte) 'N' && b3 == (byte) 'G') { return "png"; } else if (b6 == (byte) 'J' && b7 == (byte) 'F' && b8 == (byte) 'I' && b9 == (byte) 'F') { return "jpg"; } else { return null; } } else { return null; } }

    方式三:获取文件头信息,该方法可以获取所有文件的类型

    /** * @param is * @return * @throws IOException * @author jiangzeyin * @date 2016-8-17 */ public static FileType getFileType(InputStream is) throws IOException { byte[] src = new byte[28]; is.read(src, 0, 28); StringBuilder stringBuilder = new StringBuilder(""); if (src == null || src.length <= 0) { return null; } for (int i = 0; i < src.length; i++) { int v = src[i] & 0xFF; String hv = Integer.toHexString(v).toUpperCase(); if (hv.length() < 2) { stringBuilder.append(0); } stringBuilder.append(hv); } FileType[] fileTypes = FileType.values(); for (FileType fileType : fileTypes) { if (stringBuilder.toString().startsWith(fileType.getValue())) { return fileType; } } return null; }

    同时还是需要一个枚举类:

    package com.yoke.util.file; /** * Created by jiangzeyin on 2017/3/15. */ public enum FileType { /** * JPEG */ JPEG("FFD8FF", "jpg"), /** * PNG */ PNG("89504E47", "png"), /** * GIF */ GIF("47494638", "gif"), /** * TIFF */ TIFF("49492A00"), /** * Windows bitmap */ BMP("424D"), /** * CAD */ DWG("41433130"), /** * Adobe photoshop */ PSD("38425053"), /** * Rich Text Format */ RTF("7B5C727466"), /** * XML */ XML("3C3F786D6C"), /** * HTML */ HTML("68746D6C3E"), /** * Outlook Express */ DBX("CFAD12FEC5FD746F "), /** * Outlook */ PST("2142444E"), /** * doc;xls;dot;ppt;xla;ppa;pps;pot;msi;sdw;db */ OLE2("0xD0CF11E0A1B11AE1"), /** * Microsoft Word/Excel */ XLS_DOC("D0CF11E0"), /** * Microsoft Access */ MDB("5374616E64617264204A"), /** * Word Perfect */ WPB("FF575043"), /** * Postscript */ EPS_PS("252150532D41646F6265"), /** * Adobe Acrobat */ PDF("255044462D312E"), /** * Windows Password */ PWL("E3828596"), /** * ZIP Archive */ ZIP("504B0304"), /** * ARAR Archive */ RAR("52617221"), /** * WAVE */ WAV("57415645"), /** * AVI */ AVI("41564920"), /** * Real Audio */ RAM("2E7261FD"), /** * Real Media */ RM("2E524D46"), /** * Quicktime */ MOV("6D6F6F76"), /** * Windows Media */ ASF("3026B2758E66CF11"), /** * MIDI */ MID("4D546864"); private String value = ""; private String ext = ""; FileType(String value) { this.value = value; } FileType(String value, String ext) { this(value); this.ext = ext; } public String getExt() { return ext; } public String getValue() { return value; } } 相关资源:java判断是否为图片的步骤和方法
    最新回复(0)