Apache tika 实现各种文档内容解析示例代码

 更新时间:2024年07月10日 10:20:38   作者:北执南念  
这篇文章主要介绍了Apache tika 实现各种文档内容解析,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧

Apache tika 实现各种文档内容解析

1、依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>cn.js</groupId>
    <artifactId>TikaResouce</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.0</version>
    </parent>
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.apache.tika</groupId>
                    <artifactId>tika-bom</artifactId>
                    <version>2.8.0</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tika</groupId>
            <artifactId>tika-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tika</groupId>
            <artifactId>tika-parsers-standard-package</artifactId>
        </dependency>
    </dependencies>
</project>

2、配置文件

新建一个 tika-config.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<properties>
    <encodingDetectors>
        <encodingDetector class="org.apache.tika.parser.html.HtmlEncodingDetector">
            <params>
                <param name="markLimit" type="int">64000</param>
            </params>
        </encodingDetector>
        <encodingDetector class="org.apache.tika.parser.txt.UniversalEncodingDetector">
            <params>
                <param name="markLimit" type="int">64001</param>
            </params>
        </encodingDetector>
        <encodingDetector class="org.apache.tika.parser.txt.Icu4jEncodingDetector">
            <params>
                <param name="markLimit" type="int">64002</param>
            </params>
        </encodingDetector>
    </encodingDetectors>
</properties>

3、配置类

package cn.js.config;
import java.io.IOException;
import java.io.InputStream;
import org.apache.tika.Tika;
import org.apache.tika.config.TikaConfig;
import org.apache.tika.detect.Detector;
import org.apache.tika.exception.TikaException;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.Parser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.xml.sax.SAXException;
/**
 * tika配置类
 */
@Configuration
public class MyTikaConfig {
    @Autowired
    private ResourceLoader resourceLoader;
    @Bean
    public Tika tika() throws TikaException, IOException, SAXException {
        Resource resource = resourceLoader.getResource("classpath:tika-config.xml");
        InputStream inputStream = resource.getInputStream();
        TikaConfig config = new TikaConfig(inputStream);
        Detector detector = config.getDetector();
        Parser autoDetectParser = new AutoDetectParser(config);
        return new Tika(detector, autoDetectParser);
    }
}

controller

package cn.js.controller;
import org.apache.tika.Tika;
import org.apache.tika.exception.TikaException;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
@RestController
@RequestMapping("/tika")
public class TikaController {
    @Resource
    private Tika tika;
    @PostMapping("/pdf")
    public void TikaDemon(@RequestParam("file") MultipartFile file) throws IOException, TikaException {
        InputStream inputStream = file.getInputStream();
        String s = tika.parseToString(inputStream);
        System.out.println(s);
    }
}

到此这篇关于Apache tika 实现各种文档内容解析的文章就介绍到这了,更多相关Apache tika 文档内容解析内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • apache ab工具页面压力测试返回结果含义解释

    apache ab工具页面压力测试返回结果含义解释

    这篇文章主要介绍了apache ab工具页面压力测试返回结果含义解释,ab工具的使用非常简单,但返回结果中的数据有点多,看不懂的话就需要看看本文了,需要的朋友可以参考下
    2015-07-07
  • 一道题理解Linux中sort命令的多个参数

    一道题理解Linux中sort命令的多个参数

    今天小编就为大家分享一篇关于一道题理解Linux中sort命令的多个参数,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-03-03
  • Centos7安装redis6.2.6全过程

    Centos7安装redis6.2.6全过程

    本文介绍了在CentOS7上安装Redis的过程,包括下载、安装依赖(gcc)、解压编译安装、修改配置、启动服务及自启配置等步骤
    2026-04-04
  • 每天一个linux命令(61):wget命令详解

    每天一个linux命令(61):wget命令详解

    本篇文章主要介绍了wget命令,Linux系统中的wget是一个下载文件的工具,非常具有实用价值,需要的朋友可以参考下。
    2016-11-11
  • linux下安装nodejs及npm的方法

    linux下安装nodejs及npm的方法

    本篇文章主要介绍了linux下安装nodejs及npm的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-06-06
  • 阿里云CentOS挂载新数据盘的方法

    阿里云CentOS挂载新数据盘的方法

    本篇文章主要介绍了阿里云CentOS挂载新数据盘的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-12-12
  • CentOS下MySQL的彻底卸载的几种方法

    CentOS下MySQL的彻底卸载的几种方法

    本篇文章主要介绍了CentOS下MySQL的彻底卸载的几种方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08
  • Linux进程地址空间的使用及说明

    Linux进程地址空间的使用及说明

    Linux进程地址空间通过VMA和mm_struct管理,包含代码段、数据段、堆栈、mmap等区域,利用页表实现隔离,处理缺页异常,支持匿名与文件映射,系统调用如brk/mmap用于内存分配与管理
    2025-08-08
  • linux的CPU、内存查看命令方式

    linux的CPU、内存查看命令方式

    这篇文章主要介绍了linux的CPU、内存查看命令方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2025-07-07
  • 手把手教你在腾讯云上搭建hive3.1.2的方法

    手把手教你在腾讯云上搭建hive3.1.2的方法

    这篇文章主要介绍了手把手教你在腾讯云上搭建hive3.1.2的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07

最新评论