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);
    }
}
  • 输出
相关推荐
hdsoft_huge1 天前
Java & Spring Boot常见异常全解析:原因、危害、处理与防范
java·开发语言·spring boot
阿幸软件杂货间1 天前
Office转PDF转换器v1.0.py
开发语言·pdf·c#
AD钙奶-lalala1 天前
SpringBoot实现WebSocket服务端
spring boot·后端·websocket
毕设源码-朱学姐1 天前
【开题答辩全过程】以 4S店汽车维修保养管理系统为例,包含答辩的问题和答案
java·spring boot·汽车
BXCQ_xuan1 天前
软件工程实践二:Spring Boot 知识回顾
java·spring boot·后端
wuxuanok1 天前
SpringBoot -原理篇
java·spring boot·spring
云动雨颤1 天前
Spring Boot配置优化:Tomcat+数据库+缓存+日志,全场景教程
数据库·spring boot·tomcat
二饭1 天前
Spring Boot 项目启动报错:MongoSocketOpenException 连接被拒绝排查日记
java·spring boot·后端
reembarkation1 天前
使用pdfjs-dist 预览pdf,并添加文本层的实现
前端·javascript·pdf
reembarkation1 天前
vue-pdf 实现blob数据的预览
javascript·vue.js·pdf