【JAVA】word读取常见问题(涉及格式:doc、docx、rtf)

异常

java.lang.IllegalArgumentException: The document is really a RTF file

问题原因:
文档表面上是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"));

java.lang.IllegalArgumentException: The document is really a OOXML file

问题原因:
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();

java.lang.IllegalArgumentException: The document is really a HTML file

问题原因:
word文件其实是一个html文件

解决方法:
直接使用FileInputStream流读取


java.lang.IndexOutOfBoundsException: Block xxx not found

问题原因:
文件损坏


org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException: No valid entries or contents found, this is not a valid OOXML (Office Open XML) file

问题原因:
使用 XWPFDocument 读取 doc 文件时,会报错该错


java.io.IOException: Zip bomb detected!

问题报错:

java.io.IOException: Zip bomb detected! The file would exceed the max. ratio of compressed file size to the size of the expanded data.
This may indicate that the file is used to inflate memory usage and thus could pose a security risk.
You can adjust this limit via ZipSecureFile.setMinInflateRatio() if you need to work with files which exceed this limit.
Uncompressed size: 212101, Raw/compressed size: 2108, ratio: 0.009939
Limits: MIN_INFLATE_RATIO: 0.010000, Entry: word/fonts/font2.odttf

问题原因:
什么是Zip bomb? 一个里面包含了很多重复的、或者很多的递归操作的小文件,在解压的时候,需要占用巨大的空间,从而导致系统崩溃。
poi在读取word的时候,会检测文件的压缩率,检测的目的是为了防止大量空间被占用导致系统崩溃
poi默认的压缩率是0.01,当低于这个值的时候(比如上面的压缩率为0.009939小于默认的0.01),会抛出上面的错误。

解决方法:
在代码中设置更小的压缩率

ZipSecureFile.setMinInflateRatio(0.001);
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以使用Apache POI和iText库来实现Java中的RTF转换成Word。 首先需要使用iText库将RTF文件转换成DOCX文件,然后使用Apache POI读取DOCX文件并保存为Word格式。 以下是实现步骤: 1. 添加iText和POI依赖库: ```xml <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.13</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` 2. 使用iText将RTF转换成DOCX: ```java import com.itextpdf.text.Document; import com.itextpdf.text.rtf.RtfParser; import com.itextpdf.text.rtf.parser.RtfDestination; import com.itextpdf.text.rtf.parser.RtfListener; import com.itextpdf.text.rtf.parser.RtfParserUtils; import com.itextpdf.text.rtf.parser.RtfSource; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; public class RtfToDocxConverter { public static void convert(String rtfFilePath, String docxFilePath) throws Exception { FileInputStream input = new FileInputStream(new File(rtfFilePath)); FileOutputStream output = new FileOutputStream(new File(docxFilePath)); Document document = new Document(); RtfParser parser = new RtfParser(document); parser.convertRtfDocument(input, output); document.close(); input.close(); output.close(); } } ``` 3. 使用POI读取DOCX并保存为Word格式: ```java import org.apache.poi.xwpf.usermodel.XWPFDocument; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; public class DocxToWordConverter { public static void convert(String docxFilePath, String wordFilePath) throws Exception { FileInputStream input = new FileInputStream(new File(docxFilePath)); XWPFDocument document = new XWPFDocument(input); FileOutputStream output = new FileOutputStream(new File(wordFilePath)); document.write(output); document.close(); output.close(); } } ``` 最后,调用以上两个方法即可完成RTF转换成Word的操作。 ```java String rtfFilePath = "test.rtf"; String docxFilePath = "test.docx"; String wordFilePath = "test.doc"; RtfToDocxConverter.convert(rtfFilePath, docxFilePath); DocxToWordConverter.convert(docxFilePath, wordFilePath); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尤成军军军

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值