Java通过InputStream判断word格式
-
- [1. 传入inputStream文件流](#1. 传入inputStream文件流)
- [2. 读取文件流前八个字节到字节流数组中](#2. 读取文件流前八个字节到字节流数组中)
- [3. 转为字符串](#3. 转为字符串)
- [4. 对比字符串判断文件类型](#4. 对比字符串判断文件类型)
- [5. 完整代码](#5. 完整代码)
Java通过InputStream判断word格式)
1. 传入inputStream文件流
2. 读取文件流前八个字节到字节流数组中
java
byte[] byteArray = new byte[8];
inputStream.read(byteArray, 0, 8);
3. 转为字符串
java
StringBuilder sb = new StringBuilder();
for (byte b : byteArray) {
sb.append(String.format("%02X", b));
}
4. 对比字符串判断文件类型
java
if (hexString.startsWith("D0CF11E0A1B11AE1")) {
return "doc";
} else if (hexString.startsWith("504B030414000600")) {
return "docx";
} else {
return "unknown";
}
5. 完整代码
java
/**
* 通过流判断word格式
* @param inputStream
* @return
* @throws IOException
*/
public static String detectFileType(InputStream inputStream) throws IOException {
byte[] byteArray = new byte[8];
try{
inputStream.read(byteArray, 0, 8);
String hexString = bytesToHexString(byteArray);
if (hexString.startsWith("D0CF11E0A1B11AE1")) {
return "doc";
} else if (hexString.startsWith("504B030414000600")) {
return "docx";
} else {
return "unknown";
}
} finally {
if (inputStream !=null) {
inputStream.close();
}
}
}
private static String bytesToHexString(byte[] byteArray) {
StringBuilder sb = new StringBuilder();
for (byte b : byteArray) {
sb.append(String.format("%02X", b));
}
return sb.toString();
}