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

代码仓库:

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* 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();
}
}