lucene读取word,excel,pdf

前面在写lucene入门的时候,例子只能对txt文档建立索引,不能对word,excel,pdf建立索引,要读取这些文档的内容,需要额外的jar包,好在apache这个开源组织好,提供了对这些文档解析的开源jar包

 

索引和查询,我就不再写出来了,前面文章有,下面只将这三种文档的读取方法贴在下面

 

1.首先来看WORD文档:

这里用的是poi,相关jar包(http://poi.apache.org/)可以到apache官网上去下载,然后加到工程中(以下所要用的jar包也是,不再重复说)。一个poi.jar还不行,还需要将poi-scratchpad.jar包导入才行

[java]  view plain copy
  1. public static String readWord(String path) {  
  2.     StringBuffer content = new StringBuffer("");// 文档内容  
  3.     try {  
  4.   
  5.         HWPFDocument doc = new HWPFDocument(new FileInputStream(path));  
  6.         Range range = doc.getRange();  
  7.         int paragraphCount = range.numParagraphs();// 段落  
  8.         for (int i = 0; i < paragraphCount; i++) {// 遍历段落读取数据  
  9.             Paragraph pp = range.getParagraph(i);  
  10.             content.append(pp.text());  
  11.         }  
  12.   
  13.     } catch (Exception e) {  
  14.         e.printStackTrace();  
  15.     }  
  16.     return content.toString().trim();  
  17. }  


 

2.再来看EXCEL文档:

这里用的是jxl包,但jxl包(http://www.andykhan.com/jexcelapi/ )目前还尚不支持2007或更高的版本,但poi可以,现在相信开源的强大了,solr在今年3月份出的3.1版,5月份就出了3.2版,可以看出更新的速度

下面的例子,是用jxl包读取excel2003的,有兴趣的可以去查一下,用poi去读07版的excel,好像要加入很多关联jar包才行

 

[java]  view plain copy
  1. public static String readExcel(String path) throws Exception {  
  2.     FileInputStream fis = new FileInputStream(path);  
  3.     StringBuilder sb = new StringBuilder();  
  4.     jxl.Workbook rwb = Workbook.getWorkbook(fis);  
  5.     Sheet[] sheet = rwb.getSheets();  
  6.     for (int i = 0; i < sheet.length; i++) {  
  7.         Sheet rs = rwb.getSheet(i);  
  8.         for (int j = 0; j < rs.getRows(); j++) {  
  9.             Cell[] cells = rs.getRow(j);  
  10.             for (int k = 0; k < cells.length; k++)  
  11.                 sb.append(cells[k].getContents());  
  12.         }  
  13.     }  
  14.     fis.close();  
  15.     return sb.toString();  
  16. }  


3.最后来看PDF文档:

这里用的是PDFBox,相关jar包可以到apache官网上去下载:http://pdfbox.apache.org/download.html

这里要注意,如果只单单导入pdfbox.jar包,还会报错,还需要导入commons-logging.jar和fontbox.jar包才行

[java]  view plain copy
  1.  public static String readPdf(String path) throws Exception {   
  2.            StringBuffer content = new StringBuffer("");// 文档内容 FileInputStream fis = new  
  3.       FileInputStream(path); PDFParser p = new PDFParser(fis); p.parse();  
  4.       PDFTextStripper ts = new PDFTextStripper();  
  5.       content.append(ts.getText(p.getPDDocument())); fis.close(); return  
  6.       content.toString().trim();   
  7. }  


如果提取pdf文档的时候都会抛出异常:java.lang.Throwable: Warning: You did not close the PDF Document,请参考下面资料:

1.http://lqw.iteye.com/blog/721568

2.http://blog.csdn.net/rxr1st/article/details/2204460

 

在solr官网上看到:

Rich Document Parsing and Indexing (PDF, Word, HTML, etc) using Apache Tika

Tika好像是把poi,pdfbox等一些解析jar包容到一起了,下面看看如何在solr中实现对pdf的解析,估计要看配置文件才行

参考资料:

1.http://blog.163.com/lewutian@126/blog/static/163824796201041131910140/

2.http://blog.csdn.net/iamwangbao/archive/2009/11/04/4767387.aspx

转载于:https://my.oschina.net/softwarechina/blog/172989

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值