Java读取上传的word文档内容
@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();
}
本文来自博客园,作者:格,转载请注明原文链接:https://www.cnblogs.com/yuangyuan/p/16497342.html
浙公网安备 33010602011771号