天天看點

lucene中好用的兩個工具luke和Tika

  1. package test; 
  2. import java.io.File; 
  3. import java.io.FileInputStream; 
  4. import java.io.FileNotFoundException; 
  5. import java.io.IOException; 
  6. import java.io.InputStream; 
  7. import org.apache.tika.exception.TikaException; 
  8. import org.apache.tika.metadata.Metadata; 
  9. import org.apache.tika.parser.AutoDetectParser; 
  10. import org.apache.tika.parser.ParseContext; 
  11. import org.apache.tika.parser.Parser; 
  12. import org.apache.tika.sax.BodyContentHandler; 
  13. import org.apache.tika.sax.ContentHandlerDecorator; 
  14. import org.xml.sax.ContentHandler; 
  15. import org.xml.sax.SAXException; 
  16. public class TikaTest { 
  17.     public static void main(String[] args) { 
  18.         String classPath = TikaTest.class.getResource("/").getPath(); 
  19.         classPath += "doc/test.doc"; 
  20.         System.out.println(classPath); 
  21.         File f = new File(classPath); 
  22.         Parser parser = new AutoDetectParser(); 
  23.         InputStream input = null; 
  24.         try { 
  25.             input = new FileInputStream(f); 
  26.             ContentHandler handler = new BodyContentHandler(); 
  27.             ParseContext context = new ParseContext(); 
  28.             context.set(Parser.class, parser); 
  29.             Metadata data = new Metadata(); 
  30.             parser.parse(input, handler, data, context); 
  31.             System.out.println(handler.toString()); 
  32.         } catch (FileNotFoundException e) { 
  33.             // TODO Auto-generated catch block 
  34.             e.printStackTrace(); 
  35.         } catch (Exception e) { 
  36.             // TODO Auto-generated catch block 
  37.             e.printStackTrace(); 
  38.         } finally{ 
  39.             if(input!=null){ 
  40.                 try { 
  41.                     input.close(); 
  42.                 } catch (IOException e) { 
  43.                     // TODO Auto-generated catch block 
  44.                     e.printStackTrace(); 
  45.                 } 
  46.             } 
  47.         } 
  48.     } 

繼續閱讀