java.lang.IllegalArgumentException: The document is really a OOXML file (解决)

异常原因:java.lang.IllegalArgumentException: The document is really a OOXML file

因为项目需要用到在线预览word功能,所以在网上找了代码,运行后却报错了。如下:
在这里插入图片描述
以上实现代码摘自优快云 点我,载你过去。。。

错误原因

这是由于上述代码中的 HWPFDocument 对象只能读取2003之前版本的word, 也就是说仅支持 .doc格式。
如果你想读取2007之后版本的word (.docx格式),则需要使用 XWPFDocument 对象。
但是,具体的实现二者是有差异的,有兴趣的可以自行查找。

解决

既然知道了问题所在,那我们就更换文件类型:
在这里插入图片描述
运行后,妥了!word已成功被转换成html。。。

依赖

这里记录一下我使用的poi依赖版本:

<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>4.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>4.0.1</version>
        </dependency>
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值