日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2023/11/30 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 lucene解决全文检索word2003,word2007的办法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在上一篇文章中 ,lucene只能全文檢索word2003,無法檢索2007,并且只能加載部分內(nèi)容,無法加載全文內(nèi)容。為解決此問題,找到了如下方法

POI 讀取word (word 2003 和 word 2007)

? ? 最近在給客戶做系統(tǒng)的時(shí)候,用戶提出需求,要能夠?qū)?word 文件,現(xiàn)在 microsoft word 有好幾個(gè)版本 97、2003、2007的,這三個(gè)版本存儲(chǔ)數(shù)據(jù)的格式上都有相當(dāng)大的差別,而現(xiàn)在 97 基本上已經(jīng)退出市場(chǎng),幾乎沒有人用這個(gè)版本了, 所以在我們的系統(tǒng)中只考慮 2003 版本和 2007 版本的,因?yàn)槲覀冎灰竽軌蜃x取 word 中的文字內(nèi)容即可,其中的文字樣式、圖片等信息可以忽略,也不用直接操作 word 文件, 所以我們選擇 用 apache 的 POI 進(jìn)行讀取。

?

??? 讀取 2003 版本(.doc)的word文件相對(duì)來說比較簡單,只需要 poi-3.5-beta6-20090622.jar 和 poi-scratchpad-3.5-beta6-20090622.jar 兩個(gè) jar 包即可, 而 2007 版本(.docx)就麻煩多,我說的這個(gè)麻煩不是我們寫代碼的時(shí)候麻煩,是要導(dǎo)入的 jar 包比較的多,有如下 7 個(gè)之多:
?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?即可,因?yàn)樗枰钠渌?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 文件的時(shí)候不會(huì)讀取 word 文件中的圖片信息, 還有就是對(duì)于 2007 版的 word(.docx), 如果 word 文件中有表格,所有表格中的數(shù)據(jù)都會(huì)在讀取出來的字符串的最后。

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 中文字內(nèi)容的測(cè)試類<br />* @createDate 2009-07-25* @author Carl He*/ public class Test {public static void main(String[] args) {try {//word 2003: 圖片不會(huì)被讀取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 圖片不會(huì)被讀取, 表格中的數(shù)據(jù)會(huì)被放在字符串的最后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();}} }

 找到方法后,我們對(duì)上一篇文章indexer.java的源碼進(jìn)行更改,新增函數(shù)getDocument2007(),getDocument2003()

? ?本版本lucene是4.9

public static Document getDocument2007(File file) throws Exception {String docPath = file.getAbsolutePath();String title = file.getName();// 鍒涘緩DocumentDocument document = new Document();OPCPackage opcPackage = POIXMLDocument.openPackage(docPath);POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);String cont = extractor.getText();document.add(new StringField("filename", title, Field.Store.YES));//TOKENIZED//document.add(new Field("contents", contents));document.add(new TextField("contents", cont,Field.Store.YES));document.add(new TextField("path", docPath, Field.Store.YES));document.add(new StringField("indexDate",DateTools.dateToString(new Date(), DateTools.Resolution.DAY),Field.Store.YES));return document;}public static Document getDocument2003(File file) throws Exception {String docPath = file.getAbsolutePath();String title = file.getName();// 鍒涘緩DocumentDocument document = new Document();InputStream is = new FileInputStream(new File(docPath));WordExtractor ex = new WordExtractor(is);//is鏄疻ORD鏂囦歡鐨処nputStream String cont = ex.getText();document.add(new StringField("filename", title, Field.Store.YES));//TOKENIZEDdocument.add(new TextField("contents", cont,Field.Store.YES));document.add(new TextField("path", docPath, Field.Store.YES));document.add(new StringField("indexDate",DateTools.dateToString(new Date(), DateTools.Resolution.DAY),Field.Store.YES));return document;}

  

 同時(shí)修改for循環(huán)中的讀取文件

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

轉(zhuǎn)載于:https://www.cnblogs.com/zzlp/p/4757568.html

總結(jié)

以上是生活随笔為你收集整理的lucene解决全文检索word2003,word2007的办法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。