Tika is a collection of tools for content extraction (a toolkit for text extracting). It integrates the POI, PDFBox and provides a unified interface for text extraction work. Second, Tika also provides a convenient extension API to enrich its support for third-party file formats.
Tika provides support for the following file formats:
PDF-Via PDFBox
ms-*-Via POI
HTML-use nekohtml to defragment an irregular HTML into XHTML
OpenOffice Format-Tika offers
Archive-zip, TAR, gzip, bzip, etc.
Rtf-tika offers
Java Class-class parsing is done by ASM
Image-only meta-data extraction for images is supported
Xml
The Tika API is very handy, and the core is parser interface, which defines a parse method:
public void Parse (InputStream stream, ContentHandler handler,metadata Metadata, Parsecontext context)
Use the stream parameter to pass the file stream that needs to be parsed, the text content is passed into the handler, and the metadata is updated to metadata.
You can use the Tika parserutils tool to get an appropriate parser for parsing based on the mime-type of the file. Or Tika also provides a autodetectparser to find the right parser, depending on the particular format of the binary file (for example, Magic Code).
1. Parser interface
void Parse (InputStream stream, ContentHandler handler,metadata Metadata, Parsecontext context) throws IOException, Saxexception, tikaexception;
InputStream: Document input raw byte stream. It does not close InputStream in this method.
ParserContext: Parse the context of the processing. This parameter is used when the user needs to customize the parsing process. How to control? Inside it is a map that stores the key-value pairs of the interface (class) and its instances with a map. These interfaces (classes) can only work if the required interfaces in parser are used to parse the document.
Contenthandler:xhtml Sax event handler. Writes structured information for the input document to this handler in XHTML format.
Metadata: Document Meta data. is both an input parameter and an output parameter. Input parameters can help the parser to better understand the format of the document.
2. Select Parser Parser
(1) Determine only one document format (in HTML format)
Parser Parser = new Htmlparser ();
Parser.parse (stream, handler, metadata, context);
(2) determines that a format in multiple document formats is used
Map<mediatype, parser> parsersbytype = new Hashmap<mediatype, Parser > ();
Parsersbytype.put (Mediatype.parse ("text/html"), New Htmlparser ());
Parsersbytype.put (Mediatype.parse ("Application/xml"), New Xmlparser ());
Compositeparser parser = new Compositeparser (); //parser Collection
Parser.setparsers ( Parsersbytype);
Parser.setfallback (New Txtparser ());
Metadata Metadata = new Metadata ();
Metadata.set (Metadata.content_type, "text/html");
Parser.parse (stream, handler, metadata, context);
(3) Indeterminate document Format
Parser Parser = new Autodetectparser ();
Parser.parse (stream, handler, metadata, context);
3, Tika two ways to use
1) mode one
Public String Filetotxt (File f) {Parser Parser = new Autodetectparser (); InputStream is = null;try {Metadata Metadata = new Metadata (); Metadata.set (Metadata.author, "Bo Shuai"); It has no effect because it is covered out by Metadata.set (Metadata.resource_name_key, F.getname ()); is = new FileInputStream (f); ContentHandler handler = new Bodycontenthandler (); Parsecontext context = new Parsecontext (); Context.set (Parser.class, Parser);p Arser.parse (is, handler, metadata, context); for (String name:metadata.names ()) {System.out.println (name+ ":" +metadata.get (name));} return handler.tostring ();} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} catch (Saxexception e) {e.printstacktrace ();} catch (Tikaexception e) {e.printstacktrace ();} finally {if (is = = null) {try {is.close ()} catch (IOException e) {E.printstacktrace ();}}} return null;}
2) way two (efficiency no way one high)
Public String Tikatool (File f) throws IOException, tikaexception {Tika Tika = new Tika (); Metadata Metadata = new Metadata (); Metadata.set (Metadata.author, "wave Handsome"); It has no effect because it is covered off Metadata.set (Metadata.resource_name_key, F.getname ()); String str = tika.parsetostring (new FileInputStream (f), metadata); for (string Name:metadata.names ()) { System.out.println (name+ ":" +metadata.get (name));} Return tika.parsetostring (f); return str;}
4. Create an index using Tika
Document doc = new document (); Metadata Metadata = new Metadata ();d Oc.add (New Field ("Content", new Tika (). Parse (new FileInputStream (file), Metadata))) ;
Reference: http://blog.csdn.net/gezhonglei2007/article/details/8078104
Tika Basic Use