📅  最后修改于: 2020-11-10 04:28:49             🧑  作者: Mango
除内容外,Tika还从文件中提取元数据。元数据不过是文件随附的其他信息。如果考虑音频文件,则艺术家名称,专辑名称,标题位于元数据之下。
可扩展元数据平台(XMP)是用于处理和存储与文件内容有关的信息的标准。它是由Adobe Systems Inc.创建的。 XMP提供了用于定义,创建和处理元数据的标准。您可以将此标准嵌入几种文件格式,例如PDF , JPEG , JPEG , GIF , jpg , HTML等。
Tika使用Property类来遵循XMP属性定义。它提供了PropertyType和ValueType枚举来捕获元数据的名称和值。
这个类实现多种接口,如ClimateForcast ,CativeCommons,地理,TIFF等提供各种元数据模型的支持。另外,此类提供了多种方法来从文件中提取内容。
我们可以使用names()方法从文件的元数据对象中提取文件的所有元数据名称的列表。它以字符串数组形式返回所有名称。使用元数据的名称,我们可以使用get()方法获取值。它采用元数据名称,并返回与其关联的值。
String[] metadaNames = metadata.names();
String value = metadata.get(name);
每当我们使用parse()解析文件时,我们都会传递一个空的元数据对象作为参数之一。此方法提取给定文件的元数据(如果该文件包含任何元数据),并将其放置在元数据对象中。因此,在使用parse()解析文件之后,我们可以从该对象中提取元数据。
Parser parser = new AutoDetectParser();
BodyContentHandler handler = new BodyContentHandler();
Metadata metadata = new Metadata(); //empty metadata object
FileInputStream inputstream = new FileInputStream(file);
ParseContext context = new ParseContext();
parser.parse(inputstream, handler, metadata, context);
// now this metadata object contains the extracted metadata of the given file.
metadata.metadata.names();
下面给出的是从文本文件中提取元数据的完整程序。
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
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.parser.Parser;
import org.apache.tika.sax.BodyContentHandler;
import org.xml.sax.SAXException;
public class GetMetadata {
public static void main(final String[] args) throws IOException, TikaException {
//Assume that boy.jpg is in your current directory
File file = new File("boy.jpg");
//Parser method parameters
Parser 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);
System.out.println(handler.toString());
//getting the list of all meta data elements
String[] metadataNames = metadata.names();
for(String name : metadataNames) {
System.out.println(name + ": " + metadata.get(name));
}
}
}
将以上代码另存为GetMetadata.java,并使用以下命令从命令提示符运行:
javac GetMetadata .java
java GetMetadata
下面给出的是boy.jpg的快照
如果执行上述程序,它将为您提供以下输出-
X-Parsed-By: org.apache.tika.parser.DefaultParser
Resolution Units: inch
Compression Type: Baseline
Data Precision: 8 bits
Number of Components: 3
tiff:ImageLength: 3000
Component 2: Cb component: Quantization table 1, Sampling factors 1 horiz/1 vert
Component 1: Y component: Quantization table 0, Sampling factors 2 horiz/2 vert
Image Height: 3000 pixels
X Resolution: 300 dots
Original Transmission Reference:
53616c7465645f5f2368da84ca932841b336ac1a49edb1a93fae938b8db2cb3ec9cc4dc28d7383f1
Image Width: 4000 pixels
IPTC-NAA record: 92 bytes binary data
Component 3: Cr component: Quantization table 1, Sampling factors 1 horiz/1 vert
tiff:BitsPerSample: 8
Application Record Version: 4
tiff:ImageWidth: 4000
Content-Type: image/jpeg
Y Resolution: 300 dots
我们还可以获取所需的元数据值。
我们可以使用元数据类的add()方法添加新的元数据值。下面给出的是此方法的语法。在这里,我们添加作者姓名。
metadata.add(“author”,”Tutorials point”);
元数据类具有预定的性质,包括从像类继承的属性ClimateForcast ,CativeCommons,地理等,以支持各种数据模型。下面显示的是从Tika实施的TIFF接口继承的SOFTWARE数据类型的用法,以遵循TIFF图像格式的XMP元数据标准。
metadata.add(Metadata.SOFTWARE,"ms paint");
以下给出的完整程序演示了如何将元数据值添加到给定文件。这里,元数据元素的列表显示在输出中,以便您在添加新值之后可以观察列表中的更改。
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
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.parser.Parser;
import org.apache.tika.sax.BodyContentHandler;
import org.xml.sax.SAXException;
public class AddMetadata {
public static void main(final String[] args) throws IOException, SAXException, TikaException {
//create a file object and assume sample.txt is in your current directory
File file = new File("Example.txt");
//Parser method parameters
Parser parser = new AutoDetectParser();
BodyContentHandler handler = new BodyContentHandler();
Metadata metadata = new Metadata();
FileInputStream inputstream = new FileInputStream(file);
ParseContext context = new ParseContext();
//parsing the document
parser.parse(inputstream, handler, metadata, context);
//list of meta data elements before adding new elements
System.out.println( " metadata elements :" +Arrays.toString(metadata.names()));
//adding new meta data name value pair
metadata.add("Author","Tutorials Point");
System.out.println(" metadata name value pair is successfully added");
//printing all the meta data elements after adding new elements
System.out.println("Here is the list of all the metadata
elements after adding new elements");
System.out.println( Arrays.toString(metadata.names()));
}
}
将上面的代码另存为AddMetadata.java类,并在命令提示符下运行它-
javac AddMetadata .java
java AddMetadata
下面给出的是Example.txt的内容
Hi students welcome to tutorialspoint
如果执行上述程序,它将为您提供以下输出-
metadata elements of the given file :
[Content-Encoding, Content-Type]
enter the number of metadata name value pairs to be added 1
enter metadata1name:
Author enter metadata1value:
Tutorials point metadata name value pair is successfully added
Here is the list of all the metadata elements after adding new elements
[Content-Encoding, Author, Content-Type]
您可以使用set()方法将值设置为现有的元数据元素。使用set()方法设置date属性的语法如下-
metadata.set(Metadata.DATE, new Date());
您还可以使用set()方法为属性设置多个值。使用set()方法为Author属性设置多个值的语法如下-
metadata.set(Metadata.AUTHOR, "ram ,raheem ,robin ");
下面给出的是演示set()方法的完整程序。
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
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.parser.Parser;
import org.apache.tika.sax.BodyContentHandler;
import org.xml.sax.SAXException;
public class SetMetadata {
public static void main(final String[] args) throws IOException,SAXException, TikaException {
//Create a file object and assume example.txt is in your current directory
File file = new File("example.txt");
//parameters of parse() method
Parser parser = new AutoDetectParser();
BodyContentHandler handler = new BodyContentHandler();
Metadata metadata = new Metadata();
FileInputStream inputstream = new FileInputStream(file);
ParseContext context = new ParseContext();
//Parsing the given file
parser.parse(inputstream, handler, metadata, context);
//list of meta data elements elements
System.out.println( " metadata elements and values of the given file :");
String[] metadataNamesb4 = metadata.names();
for(String name : metadataNamesb4) {
System.out.println(name + ": " + metadata.get(name));
}
//setting date meta data
metadata.set(Metadata.DATE, new Date());
//setting multiple values to author property
metadata.set(Metadata.AUTHOR, "ram ,raheem ,robin ");
//printing all the meta data elements with new elements
System.out.println("List of all the metadata elements after adding new elements ");
String[] metadataNamesafter = metadata.names();
for(String name : metadataNamesafter) {
System.out.println(name + ": " + metadata.get(name));
}
}
}
将以上代码另存为SetMetadata.java并从命令提示符处运行-
javac SetMetadata.java
java SetMetadata
下面是example.txt的内容。
Hi students welcome to tutorialspoint
如果执行上述程序,它将提供以下输出。在输出中,您可以观察到新添加的元数据元素。
metadata elements and values of the given file :
Content-Encoding: ISO-8859-1
Content-Type: text/plain; charset = ISO-8859-1
Here is the list of all the metadata elements after adding new elements
date: 2014-09-24T07:01:32Z
Content-Encoding: ISO-8859-1
Author: ram, raheem, robin
Content-Type: text/plain; charset = ISO-8859-1