问题原因:
文档表面上是doc后缀,但是本质上是rtf格式
解决方案一:
1、将文件名称从 .doc 后缀换成 .rtf (也可以通过拷贝出一个 .rtf 文件的方式)
2、使用RTF格式读取内容
String rtfFileAbsPath = "C:\\Users\\cjyou\\Desktop\\a306dedb-3c65-47b5-9c02-d87a61d1ffe2.rtf";
RTFEditorKit rtf = new RTFEditorKit();
DefaultStyledDocument styledDoc = new DefaultStyledDocument();
InputStream rtfin = new FileInputStream(rtfFileAbsPath);
rtf.read(in, styledDoc, 0);
String content = new String(styledDoc.getText(0, styledDoc.getLength()).getBytes("ISO8859_1"));
问题原因:
doc格式的文件,会验证文件头,判断是否是正常的doc文件
1、当docx文件,改后缀为doc文件时,会报出该错
解决方案:
1、将文件名称从 .doc 后缀换成 .docx (也可以通过拷贝出一个 .docx 文件的方式)
2、使用docx格式读取内容
String fileAbsPath = "C:\\Users\\cjyou\\Desktop\\a306dedb-3c65-47b5-9c02-d87a61d1ffe2.docx";
InputStream in = new FileInputStream(fileAbsPath);
XWPFDocument docx = new XWPFDocument(in);
XWPFWordExtractor extractor = new XWPFWordExtractor(docx);
content = extractor.getText();
问题原因:
使用 XWPFDocument 读取 doc 文件时,会报错该错