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);
    }
}
  • 输出
相关推荐
一只叫煤球的猫1 小时前
为什么Java里面,Service 层不直接返回 Result 对象?
java·spring boot·面试
小当家.1052 小时前
从零构建项目认知:如何画出一张合格的系统架构图(以供应链系统为例)
java·spring boot·学习·架构·系统架构·供应链·实习
悟能不能悟2 小时前
springboot如何通过url地址获得这个地址的文件
java·spring boot·后端
沛沛老爹2 小时前
Web开发者突围AI战场:Agent Skills元工具性能优化实战指南——像优化Spring Boot一样提升AI吞吐量
java·开发语言·人工智能·spring boot·性能优化·架构·企业开发
yangminlei2 小时前
Spring Boot 响应式 WebFlux 从入门到精通
java·spring boot·后端
曹轲恒2 小时前
SpringBoot配置文件
java·spring boot
DCTANT3 小时前
【原创】使用更优雅的方式改造MyBatisPlus逻辑删除插件
spring boot·后端·mysql·kotlin·mybatis·mybatisplus
l***21784 小时前
Spring Boot 整合 log4j2 日志配置教程
spring boot·单元测试·log4j
内存不泄露4 小时前
基于 Spring Boot 的医院预约挂号系统(全端协同)设计与实现
java·vue.js·spring boot·python·flask
袁慎建@ThoughtWorks4 小时前
如何发布自定义 Spring Boot Starter
java·spring boot·后端