apache-tika从ppt-pdf-xls读取文本

简介: apache-tika从ppt-pdf-xls读取文本


你若要喜爱自己的价值,你就得给世界创造价值。——歌德

代码仓库:

GitHub - apache/tika: The Apache Tika toolkit detects and extracts metadata and text from over a thousand different file types (such as PPT, XLS, and PDF).

官网:

https://tika.apache.org/

快速开始:

Apache Tika – Getting Started with Apache Tika

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.tika.example;
import static java.nio.charset.StandardCharsets.UTF_8;
import java.io.File;
import java.io.InputStream;
import org.apache.commons.io.FileUtils;
import org.xml.sax.ContentHandler;
import org.apache.tika.config.TikaConfig;
import org.apache.tika.detect.Detector;
import org.apache.tika.io.TikaInputStream;
import org.apache.tika.langdetect.optimaize.OptimaizeLangDetector;
import org.apache.tika.language.detect.LanguageDetector;
import org.apache.tika.language.detect.LanguageResult;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.metadata.TikaCoreProperties;
import org.apache.tika.mime.MediaType;
import org.apache.tika.mime.MimeTypes;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.Parser;
import org.apache.tika.sax.BodyContentHandler;
/**
 * Demonstrates how to call the different components within Tika: its
 * {@link Detector} framework (aka MIME identification and repository), its
 * {@link Parser} interface, its {@link org.apache.tika.language.LanguageIdentifier} and other goodies.
 * <p>
 * It also shows the "easy way" via {@link AutoDetectParser}
 */
public class MyFirstTika {
    public static void main(String[] args) throws Exception {
        String filename = args[0];
        TikaConfig tikaConfig = TikaConfig.getDefaultConfig();
        Metadata metadata = new Metadata();
        String text = parseUsingComponents(filename, tikaConfig, metadata);
        System.out.println("Parsed Metadata: ");
        System.out.println(metadata);
        System.out.println("Parsed Text: ");
        System.out.println(text);
        System.out.println("-------------------------");
        metadata = new Metadata();
        text = parseUsingAutoDetect(filename, tikaConfig, metadata);
        System.out.println("Parsed Metadata: ");
        System.out.println(metadata);
        System.out.println("Parsed Text: ");
        System.out.println(text);
    }
    public static String parseUsingAutoDetect(String filename, TikaConfig tikaConfig,
                                              Metadata metadata) throws Exception {
        System.out.println("Handling using AutoDetectParser: [" + filename + "]");
        AutoDetectParser parser = new AutoDetectParser(tikaConfig);
        ContentHandler handler = new BodyContentHandler();
        TikaInputStream stream = TikaInputStream.get(new File(filename), metadata);
        parser.parse(stream, handler, metadata, new ParseContext());
        return handler.toString();
    }
    public static String parseUsingComponents(String filename, TikaConfig tikaConfig,
                                              Metadata metadata) throws Exception {
        MimeTypes mimeRegistry = tikaConfig.getMimeRepository();
        System.out.println("Examining: [" + filename + "]");
        metadata.set(TikaCoreProperties.RESOURCE_NAME_KEY, filename);
        System.out.println(
                "The MIME type (based on filename) is: [" + mimeRegistry.detect(null, metadata) +
                        "]");
        InputStream stream = TikaInputStream.get(new File(filename));
        System.out.println(
                "The MIME type (based on MAGIC) is: [" + mimeRegistry.detect(stream, metadata) +
                        "]");
        stream = TikaInputStream.get(new File(filename));
        Detector detector = tikaConfig.getDetector();
        System.out.println("The MIME type (based on the Detector interface) is: [" +
                detector.detect(stream, metadata) + "]");
        LanguageDetector langDetector = new OptimaizeLangDetector().loadModels();
        LanguageResult lang =
                langDetector.detect(FileUtils.readFileToString(new File(filename), UTF_8));
        System.out.println("The language of this content is: [" + lang.getLanguage() + "]");
        // Get a non-detecting parser that handles all the types it can
        Parser parser = tikaConfig.getParser();
        // Tell it what we think the content is
        MediaType type = detector.detect(stream, metadata);
        metadata.set(Metadata.CONTENT_TYPE, type.toString());
        // Have the file parsed to get the content and metadata
        ContentHandler handler = new BodyContentHandler();
        parser.parse(stream, handler, metadata, new ParseContext());
        return handler.toString();
    }
}


相关文章
|
4月前
|
Java API Apache
使用 Apache PDFBox 操作PDF文件
Apache PDFBox库是一个开源的Java工具,专门用于处理PDF文档。它允许用户创建全新的PDF文件,编辑现有的PDF文档,以及从PDF文件中提取内容。此外,Apache PDFBox还提供了一些命令行实用工具。
112 6
|
10月前
|
Java Apache
通过Apache PDFBox将pdf转换为word
通过Apache PDFBox将pdf转换为word
484 0
|
10月前
|
Java Apache
通过Apache PDFBox将pdf与图片互相转换以及合并与拆分pdf
通过Apache PDFBox将pdf与图片互相转换以及合并与拆分pdf
389 0
|
分布式计算 Hadoop Apache
【资料合集】Apache Flink 精选PDF下载
Apache Flink是一款分布式、高性能的开源流式处理框架,本文收集整理了Apache Flink相关的PDF和文章资料,供大家参考。
26192 0
|
应用服务中间件 Apache nginx
《实战Nginx:取代Apache的高性能Web服务器》PDF电子书
刚刚上网瞎逛的时候发现的,觉得还行吧。。呵呵。。几时无聊的时候学学这玩意。。。先记录下来。。。 下载地址:http://blog.sina.com.cn/s/blog_62b8c29b0100hwsv.html
1307 0
|
3月前
|
消息中间件 Kafka Apache
Apache Flink 是一个开源的分布式流处理框架
Apache Flink 是一个开源的分布式流处理框架
499 5
|
2月前
|
消息中间件 API Apache
官宣|阿里巴巴捐赠的 Flink CDC 项目正式加入 Apache 基金会
本文整理自阿里云开源大数据平台徐榜江 (雪尽),关于阿里巴巴捐赠的 Flink CDC 项目正式加入 Apache 基金会。
1438 2
官宣|阿里巴巴捐赠的 Flink CDC 项目正式加入 Apache 基金会
|
2月前
|
SQL Java API
官宣|Apache Flink 1.19 发布公告
Apache Flink PMC(项目管理委员)很高兴地宣布发布 Apache Flink 1.19.0。
1387 2
官宣|Apache Flink 1.19 发布公告
|
2月前
|
SQL Apache 流计算
Apache Flink官方网站提供了关于如何使用Docker进行Flink CDC测试的文档
【2月更文挑战第25天】Apache Flink官方网站提供了关于如何使用Docker进行Flink CDC测试的文档
159 3

推荐镜像

更多