本文主要根據後臺接口識別Office文件類型這一話題作一些分享,主要方向仍是放在不能獲取到文件名狀況下的Office文件識別。java
若是後端接口能夠獲取到完成的文件名稱,則整個過程會變得很輕鬆,若是不考慮到客戶惡意修改後綴名的狀況,咱們只須要對獲取到的文件名進行截取便可,整個截取的代碼種類也不少,下面分享一個個人實現。web
public static String parseFileType(String originName) { if (StringUtils.isBlank(originName)) { return null; } else { int i = originName.lastIndexOf("\\"); int j = originName.lastIndexOf("/"); int index = i > j ? i : j; if (index == originName.length() - 1) { return null; } else { String name = originName.substring(index + 1); if (!name.contains(".")) { return null; } else { if (name.lastIndexOf("." ) + 1 == name.length()) { return null; } else { return name.substring(name.lastIndexOf(".") + 1, name.length()).toLowerCase(); } } } } }
咱們根據經常使用文件類型擁有必定固定的文件頭這種方式能夠實現對接口中的文件流作必定程度上的文件識別,這裏分享一個簡單的實現方式。後端
public static String getFileTypeByStream(byte[] b) { Map FILE_TYPE_MAP = new HashMap(); FILE_TYPE_MAP.put("d0cf11e0a1b11ae10000","doc"); //MS Excel 注意:word、msi 和 excel的文件頭同樣 FILE_TYPE_MAP.put("504b0304", "docx");//docx,xlxs,pptx文件 FILE_TYPE_MAP.put("d0cf11e0a1b11ae10000", "wps");//WPS文字wps、表格et、演示dps都是同樣的 FILE_TYPE_MAP.put("255044462D312E", "pdf"); //Adobe Acrobat (pdf) StringBuilder stringBuilder = new StringBuilder(); if (b == null || b.length <= 0) { return null; } for (byte element : b) { int v = element & 0xFF; String hv = Integer.toHexString(v); if (hv.length() < 2) { stringBuilder.append(0); } stringBuilder.append(hv); } String filetypeHex = String.valueOf(stringBuilder.toString()).substring(0, 20); System.out.println(filetypeHex); Iterator<Entry<String, String>> entryiterator = FILE_TYPE_MAP.entrySet().iterator(); while (entryiterator.hasNext()) { Entry<String, String> entry = entryiterator.next(); String fileTypeHexKey = entry.getKey().toUpperCase(); if (filetypeHex.toUpperCase().startsWith(fileTypeHexKey)) { return entry.getValue(); } } return null; }
這種方式對一些圖片視頻文件類型識別較爲準確,可是對於office文件的識別表現的不是太盡如人意,他沒法區分excel,ppt,word等三種文件,且有些形式的office文件識別還存在必定的匹配錯誤。app
根據微軟文檔給出的信息能夠知道MS Office是以二進制進行存儲的,須要根據其文檔給出的讀取方式進行讀取,這樣應該是能夠判斷各個文件類型的,筆者沒有對這個文檔作深刻研究,這裏主要講一下我對新版本office的處理方式(xlxs、pptx、docx)的處理方式,細心的讀者可能已經發現docx和zip的16進制文件頭是基本一致,咱們將docx文件使用壓縮文件打開,能夠發現其中包含一個文件:[Content_Types].xml:dom
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"> <Default Extension="png" ContentType="image/png"/> <Default Extension="bin" ContentType="application/vnd.openxmlformats-officedocument.oleObject"/> <Default Extension="jpeg" ContentType="image/jpeg"/><Default Extension="emf" ContentType="image/x-emf"/> <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/> <Default Extension="xml" ContentType="application/xml"/> <Default Extension="jpg" ContentType="image/jpeg"/> <Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/> <Override PartName="/customXml/itemProps1.xml" ContentType="application/vnd.openxmlformats-officedocument.customXmlProperties+xml"/> <Override PartName="/word/numbering.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml"/> <Override PartName="/word/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml"/> <Override PartName="/word/settings.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml"/> <Override PartName="/word/webSettings.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.webSettings+xml"/> <Override PartName="/word/footnotes.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml"/> <Override PartName="/word/endnotes.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.endnotes+xml"/> <Override PartName="/word/header1.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml"/> <Override PartName="/word/header2.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml"/> <Override PartName="/word/footer1.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml"/> <Override PartName="/word/footer2.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml"/> <Override PartName="/word/header3.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml"/> <Override PartName="/word/footer3.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml"/> <Override PartName="/word/header4.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml"/> <Override PartName="/word/footer4.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml"/> <Override PartName="/word/fontTable.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml"/> <Override PartName="/word/theme/theme1.xml" ContentType="application/vnd.openxmlformats-officedocument.theme+xml"/> <Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/> <Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/> </Types>
其基本包含了整個文件中包含的內容,咱們對其中一個key-value進行識別便可,docx的標誌性PartName爲"/word/document.xml「,xlsx的標誌性PartName爲/xl/workbook.xml。ide
private static List<String> parseFilePartNameList(MultipartFile file) throws IOException, JDOMException{ List<String> partNames=new ArrayList<>(); ZipInputStream zipStream = new ZipInputStream(file.getInputStream()); BufferedInputStream bufferStream = new BufferedInputStream(zipStream); ZipEntry entry; while ((entry = zipStream.getNextEntry()) != null) { String fileName = entry.getName(); if (fileName.equals("[Content_Types].xml")) { SAXBuilder builder = new SAXBuilder(); byte[] xmlbytes = new byte[(int) entry.getSize()]; bufferStream.read(xmlbytes, 0, (int) entry.getSize()); InputStream byteArrayInputStream = new ByteArrayInputStream(xmlbytes); org.jdom.Document document = builder.build(byteArrayInputStream); org.jdom.Element foo = document.getRootElement(); List<Element> chilLst = foo.getChildren(); for (Element child : chilLst) { String partNameValue = child.getAttributeValue("PartName"); if (StringUtils.isBlank(partNameValue)) { partNames.add(partNameValue); } } } } return partNames; }