准备jar包
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.17</version>
</dependency>
path:读取文档路径
public static String readWord(String path) throws Exception {
if (path.endsWith(".doc")) {
URL url = new URL(path);
InputStream is = url.openStream();
WordExtractor ex = new WordExtractor(is);
return ex.getText();
} else if (path.endsWith("docx")) {
URL url = new URL(path);
InputStream is = url.openStream();
POIXMLTextExtractor extractor = new XWPFWordExtractor(OPCPackage.open(is));
return extractor.getText();
}
return null ;
}