Apache Tika
Apache Tika 是一个开源的 Java 库,被誉为文件处理领域的“瑞士军刀”。它的核心使命是:从各种格式的文档中检测类型并提取元数据和文本内容。
无论你交给它的是 PDF、Word、Excel、图片、音频还是视频,Tika 都能通过统一的接口告诉你:“这是什么类型的文件”以及“文件里写了什么”。
基础配置
添加依赖
xml
<properties>
<tika.version>3.2.3</tika.version>
</properties>
<dependencies>
<!-- Apache Tika 检测库 -->
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-core</artifactId>
<version>${tika.version}</version>
</dependency>
<!-- Apache Tika 解析内容库 -->
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-parsers-standard-package</artifactId>
<version>${tika.version}</version>
</dependency>
</dependencies>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
创建 TikaUtil
java
package io.github.atengk.tika.util;
import org.apache.tika.Tika;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.sax.BodyContentHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* Apache Tika 工具类
* <p>
* 提供文件类型检测、文本内容提取、元数据解析等能力
*
* @author Ateng
* @since 2026-02-09
*/
public final class TikaUtil {
private static final Logger log = LoggerFactory.getLogger(TikaUtil.class);
/**
* 默认最大文本提取长度
*/
private static final int DEFAULT_MAX_CONTENT_LENGTH = 100_000;
/**
* 线程安全的 Tika 实例
*/
private static final Tika TIKA = new Tika();
/**
* 自动检测解析器
*/
private static final AutoDetectParser PARSER = new AutoDetectParser();
private TikaUtil() {
}
/* ========================= type ========================= */
/**
* 检测文件 MIME 类型
*
* @param file 文件对象
* @return MIME 类型,失败返回 null
*/
public static String detect(File file) {
if (file == null) {
return null;
}
try {
return TIKA.detect(file);
} catch (Exception e) {
log.warn("Detect file type failed: {}", file.getAbsolutePath(), e);
return null;
}
}
/**
* 检测字节数据 MIME 类型
*
* @param data 文件字节数据
* @return MIME 类型,失败返回 null
*/
public static String detect(byte[] data) {
if (data == null || data.length == 0) {
return null;
}
try {
return TIKA.detect(data);
} catch (Exception e) {
log.warn("Detect byte[] type failed", e);
return null;
}
}
/**
* 检测输入流 MIME 类型
*
* @param inputStream 输入流
* @return MIME 类型,失败返回 null
*/
public static String detect(InputStream inputStream) {
if (inputStream == null) {
return null;
}
try {
return TIKA.detect(inputStream);
} catch (Exception e) {
log.warn("Detect InputStream type failed", e);
return null;
}
}
/**
* 是否为图片类型
*
* @param mimeType MIME 类型
* @return 是否为图片
*/
public static boolean isImage(String mimeType) {
return mimeType != null && mimeType.startsWith("image/");
}
/**
* 是否需要 OCR 处理
*
* @param mimeType MIME 类型
* @return 是否需要 OCR
*/
public static boolean needOcr(String mimeType) {
if (mimeType == null) {
return false;
}
return isImage(mimeType);
}
/**
* 是否为音频类型
*
* @param mimeType MIME 类型
* @return 是否为音频
*/
public static boolean isAudio(String mimeType) {
return mimeType != null && mimeType.startsWith("audio/");
}
/**
* 是否为视频类型
*
* @param mimeType MIME 类型
* @return 是否为视频
*/
public static boolean isVideo(String mimeType) {
return mimeType != null && mimeType.startsWith("video/");
}
/**
* 是否为 PDF
*
* @param mimeType MIME 类型
* @return 是否为 PDF
*/
public static boolean isPdf(String mimeType) {
return "application/pdf".equals(mimeType);
}
/**
* 是否为 Office 文档
*
* @param mimeType MIME 类型
* @return 是否为 Office 文档
*/
public static boolean isOffice(String mimeType) {
if (mimeType == null) {
return false;
}
return mimeType.startsWith("application/msword")
|| mimeType.startsWith("application/vnd.ms-")
|| mimeType.startsWith("application/vnd.openxmlformats-officedocument");
}
/**
* 是否为可解析文本类型
*
* @param mimeType MIME 类型
* @return 是否可能包含正文文本
*/
public static boolean isTextual(String mimeType) {
if (mimeType == null) {
return false;
}
return mimeType.startsWith("text/")
|| isPdf(mimeType)
|| isOffice(mimeType);
}
/**
* 校验 MIME 类型是否在白名单中
*
* @param mimeType MIME 类型
* @param allowed 允许的 MIME 类型集合
* @return 是否允许
*/
public static boolean isAllowed(String mimeType, Set<String> allowed) {
if (mimeType == null || allowed == null || allowed.isEmpty()) {
return false;
}
return allowed.contains(mimeType);
}
/**
* 校验文件扩展名与 MIME 是否匹配
*
* @param file 文件
* @param mimeType MIME 类型
* @return 是否匹配
*/
public static boolean isExtensionMatch(File file, String mimeType) {
if (file == null || mimeType == null) {
return false;
}
String name = file.getName().toLowerCase();
if (name.endsWith(".pdf")) {
return isPdf(mimeType);
}
if (name.endsWith(".docx") || name.endsWith(".doc")) {
return isOffice(mimeType);
}
if (name.endsWith(".png") || name.endsWith(".jpg") || name.endsWith(".jpeg")) {
return isImage(mimeType);
}
return true;
}
/**
* 是否为可安全解析文件
*
* @param file 文件
* @param allowedMime 允许的 MIME 类型
* @param maxBytes 最大文件大小
* @return 是否可解析
*/
public static boolean canParse(File file, Set<String> allowedMime, long maxBytes) {
if (isEmpty(file) || isTooLarge(file, maxBytes)) {
return false;
}
String mimeType = detect(file);
return isAllowed(mimeType, allowedMime) && isExtensionMatch(file, mimeType);
}
/* ========================= text ========================= */
/**
* 提取文件文本内容
*
* @param file 文件对象
* @return 文本内容,失败返回空字符串
*/
public static String parseText(File file) {
if (file == null) {
return "";
}
try (InputStream inputStream = new FileInputStream(file)) {
return parseText(inputStream, DEFAULT_MAX_CONTENT_LENGTH);
} catch (Exception e) {
log.warn("Parse text from file failed: {}", file.getAbsolutePath(), e);
return "";
}
}
/**
* 提取字节数据文本内容
*
* @param data 文件字节数据
* @return 文本内容,失败返回空字符串
*/
public static String parseText(byte[] data) {
if (data == null || data.length == 0) {
return "";
}
try (InputStream inputStream = new ByteArrayInputStream(data)) {
return parseText(inputStream, DEFAULT_MAX_CONTENT_LENGTH);
} catch (Exception e) {
log.warn("Parse text from byte[] failed", e);
return "";
}
}
/**
* 提取输入流文本内容
*
* @param inputStream 输入流
* @param maxContentLength 最大提取字符数,< 0 表示不限制
* @return 文本内容,失败返回空字符串
*/
public static String parseText(InputStream inputStream, int maxContentLength) {
if (inputStream == null) {
return "";
}
try {
int limit = maxContentLength < 0 ? -1 : maxContentLength;
BodyContentHandler handler = new BodyContentHandler(limit);
Metadata metadata = new Metadata();
ParseContext context = new ParseContext();
PARSER.parse(inputStream, handler, metadata, context);
return handler.toString();
} catch (Exception e) {
log.warn("Parse text from InputStream failed", e);
return "";
}
}
/* ========================= metadata ========================= */
/**
* 解析文件元数据
*
* @param file 文件对象
* @return 元数据 Map,失败返回空 Map
*/
public static Map<String, String> parseMetadata(File file) {
if (file == null) {
return Collections.emptyMap();
}
try (InputStream inputStream = new FileInputStream(file)) {
return parseMetadata(inputStream);
} catch (Exception e) {
log.warn("Parse metadata from file failed: {}", file.getAbsolutePath(), e);
return Collections.emptyMap();
}
}
/**
* 获取指定元数据值
*
* @param file 文件
* @param key 元数据 key
* @return 元数据值,不存在返回 null
*/
public static String getMetadata(File file, String key) {
if (file == null || key == null) {
return null;
}
Map<String, String> metadata = parseMetadata(file);
return metadata.get(key);
}
/**
* 解析字节数据元数据
*
* @param data 文件字节数据
* @return 元数据 Map,失败返回空 Map
*/
public static Map<String, String> parseMetadata(byte[] data) {
if (data == null || data.length == 0) {
return Collections.emptyMap();
}
try (InputStream inputStream = new ByteArrayInputStream(data)) {
return parseMetadata(inputStream);
} catch (Exception e) {
log.warn("Parse metadata from byte[] failed", e);
return Collections.emptyMap();
}
}
/**
* 解析输入流元数据
*
* @param inputStream 输入流
* @return 元数据 Map,失败返回空 Map
*/
public static Map<String, String> parseMetadata(InputStream inputStream) {
if (inputStream == null) {
return Collections.emptyMap();
}
try {
BodyContentHandler handler = new BodyContentHandler(-1);
Metadata metadata = new Metadata();
ParseContext context = new ParseContext();
PARSER.parse(inputStream, handler, metadata, context);
return toMap(metadata);
} catch (Exception e) {
log.warn("Parse metadata from InputStream failed", e);
return Collections.emptyMap();
}
}
/* ========================= full ========================= */
/**
* 同时解析文本内容和元数据
*
* @param file 文件对象
* @return 解析结果,失败返回 null
*/
public static TikaResult parseAll(File file) {
if (file == null) {
return null;
}
try (InputStream inputStream = new FileInputStream(file)) {
return parseAll(inputStream, DEFAULT_MAX_CONTENT_LENGTH);
} catch (Exception e) {
log.warn("Parse all from file failed: {}", file.getAbsolutePath(), e);
return null;
}
}
/**
* 同时解析文本内容和元数据
*
* @param inputStream 输入流
* @param maxContentLength 最大提取字符数
* @return 解析结果,失败返回 null
*/
public static TikaResult parseAll(InputStream inputStream, int maxContentLength) {
if (inputStream == null) {
return null;
}
try {
int limit = maxContentLength < 0 ? -1 : maxContentLength;
BodyContentHandler handler = new BodyContentHandler(limit);
Metadata metadata = new Metadata();
ParseContext context = new ParseContext();
PARSER.parse(inputStream, handler, metadata, context);
return new TikaResult(handler.toString(), toMap(metadata));
} catch (Exception e) {
log.warn("Parse all from InputStream failed", e);
return null;
}
}
/* ========================= helper ========================= */
private static Map<String, String> toMap(Metadata metadata) {
if (metadata == null || metadata.size() == 0) {
return Collections.emptyMap();
}
Map<String, String> map = new HashMap<>(metadata.size());
for (String name : metadata.names()) {
map.put(name, metadata.get(name));
}
return map;
}
/* ========================= result ========================= */
/**
* Tika 解析结果封装
*/
public static final class TikaResult {
private final String content;
private final Map<String, String> metadata;
public TikaResult(String content, Map<String, String> metadata) {
this.content = content;
this.metadata = metadata;
}
public String getContent() {
return content;
}
public Map<String, String> getMetadata() {
return metadata;
}
}
/* ========================= size ========================= */
/**
* 是否为空文件
*
* @param file 文件
* @return 是否为空
*/
public static boolean isEmpty(File file) {
return file == null || !file.exists() || file.length() == 0;
}
/**
* 是否超过最大文件大小
*
* @param file 文件
* @param maxBytes 最大字节数
* @return 是否超限
*/
public static boolean isTooLarge(File file, long maxBytes) {
if (file == null || maxBytes <= 0) {
return false;
}
return file.length() > maxBytes;
}
}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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
使用测试
java
package io.github.atengk.tika;
import io.github.atengk.tika.util.TikaUtil;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.Map;
/**
* Apache Tika 工具类测试
*/
public class TikaTests {
/**
* 测试文件类型检测(File)
*/
@Test
void testDetectByFile() {
File file = new File("D:\\Temp\\pdf\\demo_simple.pdf");
String mimeType = TikaUtil.detect(file);
System.out.println("MIME Type (File): " + mimeType);
}
/**
* 测试文件是否为图片类型
*/
@Test
void testisImage() {
File file = new File("D:\\temp\\demo.pdf");
String mimeType = TikaUtil.detect(file);
boolean isImage = TikaUtil.isImage(mimeType);
System.out.println("MIME Type isImage: " + isImage);
}
/**
* 测试文件类型检测(byte[])
*/
@Test
void testDetectByBytes() {
byte[] data = "Hello Tika".getBytes(StandardCharsets.UTF_8);
String mimeType = TikaUtil.detect(data);
System.out.println("MIME Type (byte[]): " + mimeType);
}
/**
* 测试文本内容提取(File)
*/
@Test
void testParseTextByFile() {
File file = new File("D:\\Temp\\word\\demo.docx");
String content = TikaUtil.parseText(file);
System.out.println("Text Content (File):");
System.out.println(content);
}
/**
* 测试文本内容提取(byte[])
*/
@Test
void testParseTextByBytes() {
byte[] data = "Apache Tika Test Content".getBytes(StandardCharsets.UTF_8);
String content = TikaUtil.parseText(data);
System.out.println("Text Content (byte[]):");
System.out.println(content);
}
/**
* 测试元数据解析(File)
*/
@Test
void testParseMetadataByFile() {
File file = new File("D:\\Temp\\word\\demo.docx");
Map<String, String> metadata = TikaUtil.parseMetadata(file);
System.out.println("Metadata (File):");
metadata.forEach((key, value) ->
System.out.println(key + " = " + value)
);
}
/**
* 测试同时解析文本和元数据
*/
@Test
void testParseAll() {
File file = new File("D:\\Temp\\word\\demo.docx");
TikaUtil.TikaResult result = TikaUtil.parseAll(file);
if (result == null) {
System.out.println("Parse result is null");
return;
}
System.out.println("Full Parse Result:");
System.out.println("---- Content ----");
System.out.println(result.getContent());
System.out.println("---- Metadata ----");
result.getMetadata().forEach((key, value) ->
System.out.println(key + " = " + value)
);
}
/**
* 测试超长文本限制
*/
@Test
void testParseTextWithLimit() {
File file = new File("test-files/large.pdf");
String content = TikaUtil.parseText(file);
System.out.println("Limited Text Length: " + content.length());
}
/**
* 测试异常场景(文件不存在)
*/
@Test
void testFileNotExists() {
File file = new File("test-files/not-exists.pdf");
String mimeType = TikaUtil.detect(file);
String content = TikaUtil.parseText(file);
Map<String, String> metadata = TikaUtil.parseMetadata(file);
System.out.println("MIME Type: " + mimeType);
System.out.println("Content: " + content);
System.out.println("Metadata size: " + metadata.size());
}
}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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144