Java通过InputStream判断word格式)
1. 传入inputStream文件流
2. 读取文件流前八个字节到字节流数组中
byte[] byteArray = new byte[8];
inputStream.read(byteArray, 0, 8);
3. 转为字符串
StringBuilder sb = new StringBuilder();
for (byte b : byteArray) {
sb.append(String.format("%02X", b));
}
4. 对比字符串判断文件类型
if (hexString.startsWith("D0CF11E0A1B11AE1")) {
return "doc";
} else if (hexString.startsWith("504B030414000600")) {
return "docx";
} else {
return "unknown";
}
5. 完整代码
/**
* 通过流判断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();
}
文章来源地址https://www.toymoban.com/news/detail-607620.html
文章来源:https://www.toymoban.com/news/detail-607620.html
到了这里,关于Java通过InputStream判断word格式的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!