/**
* 获取文件头
*
* @param file 文件字节码
* @param length 获取头文件字节码长度
* @return 文件头
* @since 2022年3月25日16点55分
*/
private static String getFileHeader(byte[] file, int length) {
if (null == file || file.length <= 0 || length > file.length) {
return "";
}
StringBuilder builder = new StringBuilder();
for (int i = 0; i < length; i++) {
int bt = file[i] & 0xFF;
String str = Integer.toHexString(bt);
if (str.length() < 2) {
builder.append(0);
}
builder.append(str);
}
return builder.toString();
}
public static void main(String[] args) {
File file1 = new File("C:\\Users\\lenove\\Desktop\\手机.png");
System.out.println(getFileHeader(FileUtil.readBytes(file1),3));
}
Java 获取文件头(判断文件类型)
于 2022-03-25 16:55:47 首次发布