Apache Tika 缺少许多元数据键值对

Missing many metadata Key-value pairs with Apache Tika

我正在尝试使用下面给出的 Apache Tika.The 代码获取 JAVA 中文件的元数据,

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

import org.apache.tika.exception.TikaException;
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.xml.sax.SAXException;

public class tika {

public static void main(final String[] args) throws IOException, SAXException, TikaException {
      File file = new File("C:\HTML\viewer\files\doc.doc");

      AutoDetectParser parser = new AutoDetectParser();
      BodyContentHandler handler = new BodyContentHandler();
      Metadata metadata = new Metadata();
      FileInputStream inputstream = new FileInputStream(file);
      ParseContext context = new ParseContext();
      parser.parse(inputstream, handler, metadata, context);

      String[] metadataNames = metadata.names();
      System.out.println(metadataNames.length);
        for (String key : metadataNames) {
            String value = metadata.get(key);
            System.out.println(key + ": " + value);
        }
      
   }
}

问题是我只得到下面给出的两个键值对,

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
2
X-TIKA:Parsed-By: org.apache.tika.parser.EmptyParser
Content-Type: application/x-tika-msoffice

我收到来自 SLF4J 的警告,我认为这对于获得所需的输出不是问题,如果是,请告诉我。

它是一个 Maven 项目,下面给出了我的依赖项,

<dependency>
  <groupId>org.apache.tika</groupId>
  <artifactId>tika-core</artifactId>
  <version>2.3.0</version>
</dependency>
<dependency>
  <groupId>org.apache.tika</groupId>
  <artifactId>tika-parsers</artifactId>
  <version>2.3.0</version>
  <type>pom</type>
</dependency>

我只得到 'X-TIKA:Parsed-By' 和 'Content-Type' 键值。我想要更多元数据值,例如作者、评论、大小等。

不胜感激,谢谢

您必须使用特定的解析器来检索 Microsoft 文档属性。

首先添加如下依赖:

<dependency>
  <groupId>org.apache.tika</groupId>
  <artifactId>tika-core</artifactId>
  <version>2.3.0</version>
</dependency>
<dependency>
  <groupId>org.apache.tika</groupId>
  <artifactId>tika-parsers-standard-package</artifactId>
  <version>2.3.0</version>
</dependency>

接下来,你可以这样做:

BodyContentHandler handler = new BodyContentHandler();
Metadata metadata = new Metadata();
FileInputStream inputstream = new FileInputStream(new File("C:\HTML\viewer\files\doc.doc"));
ParseContext pcontext = new ParseContext();

// Specific parser
OOXMLParser msOfficeParser = new OOXMLParser();
msOfficeParser .parse(inputstream, handler, metadata, pcontext);

System.out.println("Contents of the document:" + handler.toString());
System.out.println("Metadata of the document:");

String[] metadataNames = metadata.names();

for (String name : metadataNames) {
  System.out.println(name + ": " + metadata.get(name));
}

你将获得这样的东西:

Metadata of the document: cp:revision: 3
extended-properties:AppVersion: 16.0000 meta:paragraph-count: 1
meta:word-count: 83 extended-properties:Application: Microsoft Office
Word meta:last-author: Microsoft Office User

参考文献: