码迷,mamicode.com
首页 > Web开发 > 详细

lucene解决全文检索word2003,word2007的办法

时间:2015-08-25 16:26:02      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:

在上一篇文章中 ,lucene只能全文检索word2003,无法检索2007。为解决此问题,找到了如下方法

POI 读取word (word 2003 和 word 2007)

    最近在给客户做系统的时候,用户提出需求,要能够导入 word 文件,现在 microsoft word 有好几个版本 97、2003、2007的,这三个版本存储数据的格式上都有相当大的差别,而现在 97 基本上已经退出市场,几乎没有人用这个版本了, 所以在我们的系统中只考虑 2003 版本和 2007 版本的,因为我们只要求能够读取 word 中的文字内容即可,其中的文字样式、图片等信息可以忽略,也不用直接操作 word 文件, 所以我们选择 用 apache 的 POI 进行读取。

 

    读取 2003 版本(.doc)的word文件相对来说比较简单,只需要 poi-3.5-beta6-20090622.jar 和 poi-scratchpad-3.5-beta6-20090622.jar 两个 jar 包即可, 而 2007 版本(.docx)就麻烦多,我说的这个麻烦不是我们写代码的时候麻烦,是要导入的 jar 包比较的多,有如下 7 个之多:
 1. openxml4j-bin-beta.jar
 2. poi-3.5-beta6-20090622.jar
 3. poi-ooxml-3.5-beta6-20090622.jar
 4 .dom4j-1.6.1.jar
 5. geronimo-stax-api_1.0_spec-1.0.jar
 6. ooxml-schemas-1.0.jar
 7. xmlbeans-2.3.0.jar
其中 4-7 是 poi-ooxml-3.5-beta6-20090622.jar 所依赖的 jar 包(在 poi-bin-3.5-beta6-20090622.tar.gz 中的 ooxml-lib 目录下可以找到)。

 

    编写代码之前我们得先下载所需要的 jar 包, 我们只需下载 poi-bin-3.5-beta6-20090622.tar.gz 和 openxml4j-bin-beta.jar 即可,因为所需要的其他 jar 包都能在 poi-bin-3.5-beta6-20090622.tar.gz 中找到, 下面是下载地址:
poi-bin-3.5-beta6-20090622.tar.gz:http://apache.etoak.com/poi/dev/bin/poi-bin-3.5-beta6-20090622.tar.gz
openxml4j-bin-beta.jar:http://mirror.optus.net/sourceforge/o/op/openxml4j/openxml4j-bin-beta.jar
 
    下方是读取 word 文件的 Java 代码,值得注意的是: POI 在读取 word 文件的时候不会读取 word 文件中的图片信息, 还有就是对于 2007 版的 word(.docx), 如果 word 文件中有表格,所有表格中的数据都会在读取出来的字符串的最后。

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLTextExtractor;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;

/**
 * POI 读取 word 2003 和 word 2007 中文字内容的测试类<br />
 * @createDate 2009-07-25
 * @author Carl He
 */
public class Test {
    public static void main(String[] args) {
        try {
            //word 2003: 图片不会被读取
              InputStream is = new FileInputStream(new File("c://files//2003.doc"));
            WordExtractor ex = new WordExtractor(is);
            String text2003 = ex.getText();
            System.out.println(text2003);

            //word 2007 图片不会被读取, 表格中的数据会被放在字符串的最后
            OPCPackage opcPackage = POIXMLDocument.openPackage("c://files//2007.docx");
            POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
            String text2007 = extractor.getText();
            System.out.println(text2007);
			
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 找到方法后,我们对上一篇文章indexer.java的源码进行更改,新增函数getDocument2007()

  public static Document getDocument2007(File file) throws Exception {

    	String docPath = file.getAbsolutePath();
  		String title = file.getName();
  		
  		// 鍒涘缓Document
  		Document document = new Document();
	  	OPCPackage opcPackage = POIXMLDocument.openPackage(docPath);
		POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
		String cont = extractor.getText();
  		document.add(new Field("filename", title, Field.Store.YES,
  				Field.Index.ANALYZED));//TOKENIZED
  		//document.add(new Field("contents", contents));
  		document.add(new Field("contents", cont,Field.Store.YES,Field.Index.ANALYZED));
  		//document.add(new Field("path", docPath, Field.Store.YES,Field.Index.ANALYZED));
  		document.add(new Field("indexDate",DateTools.dateToString(new Date(), DateTools.Resolution.DAY),
          		Field.Store.YES,Field.Index.NOT_ANALYZED));
  		return document;
  	}
    public static Document getDocument2003(File file) throws Exception {
		String docPath = file.getAbsolutePath();
		String title = file.getName();
		
		// 鍒涘缓Document
		Document document = new Document();
		StringBuffer contents = new StringBuffer("");// 鏂囨。鍐呭
        try {
        	FileInputStream fs = new FileInputStream(docPath);
            HWPFDocument doc = new HWPFDocument(fs);
            Range range = doc.getRange();
            int paragraphCount = range.numParagraphs();// 娈佃惤
            for (int i = 0; i < paragraphCount; i++) {// 閬嶅巻娈佃惤璇诲彇鏁版嵁
                Paragraph pp = range.getParagraph(i);
                contents.append(pp.text());
            } 

        } catch (Exception e) {

        }
        String cont = contents.toString().trim();

		
		document.add(new Field("filename", title, Field.Store.YES,
				Field.Index.ANALYZED));//TOKENIZED
		//document.add(new Field("contents", contents));
		document.add(new Field("contents", cont,Field.Store.YES,Field.Index.ANALYZED));
		//document.add(new Field("path", docPath, Field.Store.YES,Field.Index.ANALYZED));
		document.add(new Field("indexDate",DateTools.dateToString(new Date(), DateTools.Resolution.DAY),
        		Field.Store.YES,Field.Index.NOT_ANALYZED));
		return document;
	}

  同时修改for循环中的读取文件

 if(files[i].getName().endsWith(".doc")){
doc = getDocument2003(files[i]);
}else if(files[i].getName().endsWith(".docx")){
doc = getDocument2007(files[i]);
}

lucene解决全文检索word2003,word2007的办法

标签:

原文地址:http://www.cnblogs.com/zzlp/p/4757568.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!