1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
| package com.ming.core.utils;
import com.google.common.collect.Sets; import lombok.AllArgsConstructor; import lombok.Getter; import org.apache.commons.lang3.StringUtils;
import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; import java.util.Arrays; import java.util.Set;
public class FileTypeUtils {
@Getter @AllArgsConstructor public enum FileType { JPG(Sets.newHashSet("FFD8FFE0", "FFD8FFE000104A4649460001", "FFD8FFEE"), Sets.newHashSet(".jpg", "jpeg"), "JPEG原始或JFIF或Exif文件格式"), PNG(Sets.newHashSet("89504E47"), Sets.newHashSet(".png"), "png格式图片,以可移植网络图形格式编码的图像"), JAVA_FILE(Sets.newHashSet("7061636B"), Sets.newHashSet(".java"), "java源码文件"), JAVA_CLASS_FILE(Sets.newHashSet("CAFEBABE"), Sets.newHashSet(".class"), "java字节码文件"), ; private Set<String> headerHexSet; private Set<String> suffixSet; private String desc; }
private static final int HEADER_BYTE_ARRAY_LENGTH = 4;
public static void main(String[] args) throws IOException { String path = ""; System.out.println(getFileHeaderHex(Files.newInputStream(Path.of(path)))); }
public static FileType getFileType(String filePath) throws IOException { return getFileType(Files.newInputStream(Path.of(filePath))); }
public static FileType getFileType(InputStream fileInputStream) throws IOException { return getFileTypeByFileHeaderHex(getFileHeaderHex(fileInputStream)); }
public static FileType getFileType(byte[] fileBytes) { return getFileTypeByFileHeaderHex(getFileHeaderHex(fileBytes)); }
private static FileType getFileTypeByFileHeaderHex(String fileHeaderHex) { assert StringUtils.isNotEmpty(fileHeaderHex); return Arrays.stream(FileType.values()) .filter(f -> f.headerHexSet.contains(fileHeaderHex)) .findAny().orElseThrow(() -> new RuntimeException("未识别该文件类型!请检查是否录入该文件标识或者为txt类型文件")); }
private static String bytesToHexString(byte[] src) { StringBuilder stringBuilder = new StringBuilder(); if (src == null || src.length <= 0) { return null; } for (int i = 0; i < src.length; i++) { String hv = Integer.toHexString(src[i] & 0xFF).toUpperCase(); if (hv.length() < 2) { stringBuilder.append(0); } stringBuilder.append(hv); } return stringBuilder.toString(); }
private static String getFileHeaderHex(byte[] fileBytes) { if (fileBytes.length <= HEADER_BYTE_ARRAY_LENGTH) { throw new RuntimeException("字节数组长度不能小于等于" + HEADER_BYTE_ARRAY_LENGTH); } byte[] headerBytes = new byte[HEADER_BYTE_ARRAY_LENGTH]; System.arraycopy(fileBytes, 0, headerBytes, 0, HEADER_BYTE_ARRAY_LENGTH); return bytesToHexString(headerBytes); }
private static String getFileHeaderHex(InputStream inputStream) throws IOException { try { byte[] b = new byte[HEADER_BYTE_ARRAY_LENGTH]; inputStream.read(b, 0, b.length); return bytesToHexString(b); } finally { inputStream.close(); } }
private FileTypeUtils() { }
}
|