@RequestMapping(value = "save", method = RequestMethod.POST) public R save(@RequestParam("imgFile") MultipartFile file) { String fileName = file.getOriginalFilename(); System.out.println("原始文件名:" + fileName); String content = ""; try { byte [] byteArr = file.getBytes(); InputStream inputStream = new ByteArrayInputStream(byteArr); if (fileName.endsWith(".doc")) { WordExtractor ex = new WordExtractor(inputStream); content = ex.getText(); } else if (fileName.endsWith("docx")) { OPCPackage opcPackage =OPCPackage.open(inputStream); POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage); content = extractor.getText(); opcPackage.close(); } else { System.out.println("此文件不是word文件!"); } inputStream.close(); System.out.println("文件内容::"+content); } catch (XmlException e) { e.printStackTrace(); } catch (OpenXML4JException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return R.ok(); }
标签:word,System,fileName,content,文档,file,Java,inputStream,out From: https://www.cnblogs.com/RedOrange/p/17091591.html