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);
    }
}
  • 输出
相关推荐
牛马baby1 小时前
Springboot 自动装配原理是什么?SPI 原理又是什么?
java·spring boot·后端
pursue.dreams2 小时前
Spring Boot整合T-IO实现即时通讯
spring boot·websocket·t-io
-曾牛2 小时前
【LangChain4j快速入门】5分钟用Java玩转GPT-4o-mini,Spring Boot整合实战!| 附源码
java·开发语言·人工智能·spring boot·ai·chatgpt
冬天vs不冷2 小时前
SpringBoot条件注解全解析:核心作用与使用场景详解
java·spring boot·python
考虑考虑3 小时前
Springboot捕获feign抛出的异常
spring boot·后端·spring
〆、风神4 小时前
Spring Boot实战:基于策略模式+代理模式手写幂等性注解组件
spring boot·代理模式·策略模式
Anarkh_Lee4 小时前
解决 Spring Boot 多数据源环境下事务管理器冲突问题(非Neo4j请求标记了 @Transactional 尝试启动Neo4j的事务管理器)
spring boot·后端·spring
日月星辰Ace5 小时前
@TestPropertySource 造成 SpringBoot Test 中对同一个 Bean 使用不同实例
java·spring boot
XuanXu5 小时前
SpringBoot3.0启动流程研究
java·spring boot
zyk_5205 小时前
前端渲染pdf文件解决方案
javascript·pdf·react