JAVA POI报错:The document is really a OOXML file

一、问题描述

有个需求就是读取word中的内容,poi可以实现这种需求,网上找了下示例,代码如下:

    /**
     * 读取doc文件内容
     *
     * @param fs 想要读取的文件对象
     * @return 返回文件内容
     * @throws IOException
     */
    public static String readDoc(FileInputStream fs) throws IOException {
        StringBuilder result = new StringBuilder();
        WordExtractor re = new WordExtractor(fs);
        result.append(re.getText());
        re.close();
        return result.toString();
    }

    public static String readDocToStr(File file) throws IOException {
        return readDoc(new FileInputStream(file));
    }

    public static void main(String[] args) {
        File file = new File("D:\\xxx\\xxx\\1\\file\\2021\\04\\28\\34a58ac4faa4222712a4329ac60f34f9\\34a58ac4faa4222712a4329ac60f34f9.docx");
        try {
            System.out.println(readDocToStr(file));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

依赖如下:

    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>5.0.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
      <version>5.0.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-scratchpad</artifactId>
      <version>5.0.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml-full</artifactId>
      <version>5.0.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>5.0.0</version>
    </dependency>

上面的代码运行会报如下错误:

Exception in thread "main" java.lang.IllegalArgumentException: The document is really a OOXML file
	at org.apache.poi.hwpf.HWPFDocumentCore.verifyAndBuildPOIFS(HWPFDocumentCore.java:126)
	at org.apache.poi.hwpf.extractor.WordExtractor.<init>(WordExtractor.java:52)

OOXML错误。

二、解决方法

报上面的错误就是不能正确的解析docx文档导致的,改成如下就行了:

    static String read(InputStream is) throws Exception {
        System.out.println(FileMagic.valueOf(is));
        String text = "";
        if (FileMagic.valueOf(is) == FileMagic.OLE2) {
            WordExtractor ex = new WordExtractor(is);
            text = ex.getText();
            ex.close();
        } else if (FileMagic.valueOf(is) == FileMagic.OOXML) {
            XWPFDocument doc = new XWPFDocument(is);
            XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
            text = extractor.getText();
            extractor.close();
        }
        return text;
    }

    public static void main(String[] args) throws Exception {
        InputStream is = new BufferedInputStream(new FileInputStream("D:\\xxx\\xxx\\1\\file\\2021\\04\\28\\34a58ac4faa4222712a4329ac60f34f9\\34a58ac4faa4222712a4329ac60f34f9.docx")); //really a OOXML Word file
        System.out.println(read(is));
        is.close();
    }
  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值