springboot集成tika解析word,pdf,xls文件文本内容

介绍

Apache Tika 是一个开源的内容分析工具包,用于从各种文档格式中提取文本和元数据。它支持多种文档类型,包括但不限于文本文件、HTML、PDF、Microsoft Office 文档、图像文件等。Tika 的主要功能包括内容检测、文本提取和元数据提取。

官网

https://tika.apache.org/

Apache Tika 的功能

  • 内容检测:识别文件的 MIME 类型。
  • 文本提取:从文档中提取纯文本内容。
  • 元数据提取:从文档中提取元数据(如标题、作者、创建日期等)。

与Springboot集成案例

添加pom依赖

xml 复制代码
<dependency>
      <groupId>org.apache.tika</groupId>
      <artifactId>tika-core</artifactId>
      <version>2.9.1</version>
    </dependency>

    <dependency>
      <groupId>org.apache.tika</groupId>
      <artifactId>tika-parsers-standard-package</artifactId>
      <version>2.9.1</version>
    </dependency>

创建工具类

java 复制代码
public class MyFileUtils {
    public static String doParse(String filePath) throws TikaException, SAXException, IOException {
        try(InputStream inputStream = new FileInputStream(filePath)){
            BodyContentHandler handler = new BodyContentHandler(-1);
            Metadata metadata = new Metadata();
            ParseContext parseContext = new ParseContext();
            AutoDetectParser detectParser = new AutoDetectParser();
            detectParser.parse(inputStream, handler, metadata, parseContext);
            return handler.toString();
        }
    }

}

测试

java 复制代码
public class MyFileUtilsTest {
    public static void main(String[] args) {
        String filePath = "D:/tmp/测试附件.xls";
        String content = null;
        try {
            content = MyFileUtils.doParse(filePath);
        } catch (TikaException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(content);
    }
}
  • 输出
相关推荐
sunwenjian8868 小时前
Springboot项目本地连接并操作MySQL数据库
数据库·spring boot·mysql
杰克尼9 小时前
苍穹外卖--day08
java·数据库·spring boot·mybatis·notepad++
Java面试题总结9 小时前
Spring @Validated失效?原因、排查与高效解决方案全解析
java·spring boot·spring
eggwyw10 小时前
springboot和springframework版本依赖关系
java·spring boot·后端
韩立学长10 小时前
Springboot奶茶加盟信息管理系统m307m786(程序、源码、数据库、调试部署方案及开发环境)系统界面展示及获取方式置于文档末尾,可供参考。
数据库·spring boot·后端
开longlong了吗?12 小时前
Luan Takeaway System:基于Spring Boot + Spring Cloud的外卖业务系统
spring boot·后端·spring cloud
刘 大 望13 小时前
SpringAI Tool Calling(工具调用)
java·spring boot·spring·ai·maven·intellij-idea·文心一言
dovens13 小时前
Spring Boot(快速上手)
java·spring boot·后端
热爱生活的五柒13 小时前
Word 论文里参考文献经常在修改后错乱,如何解决
word