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);
    }
}
  • 输出
相关推荐
sxlishaobin9 小时前
SpringBoot集成MyBatis的SQL拦截器实战
spring boot·sql·mybatis
码客日记10 小时前
Spring Boot 全局跨域配置与前后端联调避坑
java·spring boot·后端
闪电悠米11 小时前
黑马点评-Redisson-02_reentrant_lock
java·spring boot·redis·分布式·缓存
我登哥MVP11 小时前
Spring Boo从“会用”到“精通”:Spring Boot 入门
java·spring boot·后端·spring·maven·intellij-idea·mybatis
biubiubiu070611 小时前
SpringBoot3.5.4 AOP环绕通知使用
java·spring boot
砍材农夫12 小时前
物联网实战:Spring Boot MQTT | 模拟器Paho客户端拆解核心点
java·javascript·网络·spring boot·后端·物联网
我登哥MVP12 小时前
Spring Boot 从“会用”到“精通”:自动装配原理
java·spring boot·后端·spring·tomcat·maven·intellij-idea
二哈赛车手14 小时前
新人笔记---继图片搜索功能后续以及AI网络搜索功能一些经验与踩坑点,吐槽一下自己在做这方面的崩溃瞬间
java·网络·人工智能·spring boot·笔记·spring
2601_9611940214 小时前
2026四六级翻译预测12篇|四级六级汉译英范文PDF
pdf
2601_9611940214 小时前
2026四六级翻译预测|四级六级汉译英热点+范文PDF
pdf