JSPWiki 支持附件文件内容搜索

   最近项目组要搭一个wiki,经过筛选我们决定使用JSPWiki(网上有大量的分析),待搭完以后,发现他不支持附件文件内容搜索,也就是说,如果这篇wiki中上传了一些doc,xls等文件是不能被搜索到的,但是在jspwiki.properties配置中有如下配置:

 

jspwiki.searchProvider =LuceneSearchProvider
jspwiki.lucene.analyzer = org.apache.lucene.analysis.standard.StandardAnalyzer

 

也就是JSPWiki也是用lucene来做检索的,于是下了他的源码,看了下这个类com.ecyrd.jspwiki.search.LuceneSearchProvider,发现了如下方法:

 

protected String getAttachmentContent( Attachment att )
    {
        AttachmentManager mgr = m_engine.getAttachmentManager();
        //FIXME: Add attachment plugin structure

        String filename = att.getFileName();

        if(filename.endsWith(".txt") ||
           filename.endsWith(".xml") ||
           filename.endsWith(".ini") ||
           filename.endsWith(".html"))
        {
            InputStream attStream;

            try
            {
                attStream = mgr.getAttachmentStream( att );

                StringWriter sout = new StringWriter();
                FileUtil.copyContents( new InputStreamReader(attStream), sout );

                attStream.close();
                sout.close();

                return sout.toString();
            }
            catch (ProviderException e)
            {
                log.error("Attachment cannot be loaded", e);
                return null;
            }
            catch (IOException e)
            {
                log.error("Attachment cannot be loaded", e);
                return null;
            }
        } 
......

 

就是说支持文本文件附件(txt,xml,ini,html)的内容搜索,试了一下,上传了一个txt文件,果然是可以被查出来,。

 

于是决定给这个类动动手术,添加点功能让它可以支持doc和xls, 添加了如下代码在下面(office 2003到2007,文件格式不同,分开来写了),然后重新打包(打好的jar,我放在附件里),启动JSPWiki, 实验了一下,word 和 excel的文件可以被查出来了。

 

注意的是,这个类的作用是,在附件文件上传,用lucene建立了索引,所以实验的话,一定要重新上传文件,在改这个class之前上传的文件时没有用的。

 

......       
     else if(filename.endsWith(".doc")){
            InputStream attStream = null;
            try {
                attStream = mgr.getAttachmentStream(att);             
                WordExtractor extractor = new WordExtractor(attStream);  
                String s = extractor.getText();  
                log.debug("Extracted text: " + s + " from attachment: " + filename);
                return s;              
            } catch (Exception e) {
                log.error("Attachment cannot be loaded", e);
                return null;
            } finally {
                if(attStream != null){
                    try {
                      attStream.close();
                  }
                  catch (IOException e) {
                      log.warn("Couldn't close attachment stream for " + filename, e);
                  }
                }           
            }
        }
        
        else if(filename.endsWith(".docx")){
            InputStream attStream = null;
            try {
                attStream = mgr.getAttachmentStream(att);
                XWPFWordExtractor extractor = new XWPFWordExtractor(new XWPFDocument(attStream));  
                String s = extractor.getText();  
                log.debug("Extracted text: " + s + " from attachment: " + filename);
                return s;              
            } catch (Exception e) {
                log.error("Attachment cannot be loaded", e);
                return null;
            } finally {
                if(attStream != null){
                    try {
                      attStream.close();
                  }
                  catch (IOException e) {
                      log.warn("Couldn't close attachment stream for " + filename, e);
                  }
                }           
            }
        }
        
        else if(filename.endsWith(".xls")){
            InputStream attStream = null; 
            try {
                attStream = mgr.getAttachmentStream(att);
                HSSFWorkbook workbook=new HSSFWorkbook(attStream);
                HSSFSheet sheet=null;
                StringBuffer sb = new StringBuffer();
                for(int i = 0; i < workbook.getNumberOfSheets(); i++) {
                    sheet=workbook.getSheetAt(i);
                    if(sheet == null){
                        continue; 
                    }
                    for (int j = 0; j < sheet.getPhysicalNumberOfRows(); j++) {
                        HSSFRow row=sheet.getRow(j);
                        if(row == null){
                            continue; 
                        }
                        for (int k = 0; k < row.getLastCellNum(); k++) {
                            sb.append(row.getCell(k));   
                            sb.append(" ");
                        }                  
                    }    
                }
                String s = sb.toString();
                log.debug("Extracted text: " + s + " from attachment: " + filename);
                return s; 
                
            } catch (Exception e) {
                log.error("Attachment cannot be loaded", e);
                return null;
            } finally {
                if(attStream != null){
                    try {
                      attStream.close();
                  }
                  catch (IOException e) {
                      log.warn("Couldn't close attachment stream for " + filename, e);
                  }
                }           
            }
        }
        else if(filename.endsWith(".xlsx")){
            InputStream attStream = null; 
            try {
                attStream = mgr.getAttachmentStream(att);
                XSSFWorkbook workbook = new XSSFWorkbook(attStream);
                XSSFSheet sheet=null;
                StringBuffer sb = new StringBuffer();
                for(int i = 0; i < workbook.getNumberOfSheets(); i++) {
                    sheet = workbook.getSheetAt(i);
                    if(sheet == null){
                        continue; 
                    }
                    for (int j = 0; j < sheet.getPhysicalNumberOfRows(); j++) {
                        XSSFRow row=sheet.getRow(j);
     
                        if(row == null){
                            continue; 
                        }
                        for (int k = 0; k < row.getLastCellNum(); k++) {
                            sb.append(row.getCell(k)); 
                            sb.append(" ");
                        }                  
                    }    
                }
                String s = sb.toString();
                log.debug("Extracted text: " + s + " from attachment: " + filename);
                return s; 
                
            } catch (Exception e) {
                log.error("Attachment cannot be loaded", e);
                return null;
            } finally {
                if(attStream != null){
                    try {
                      attStream.close();
                  }
                  catch (IOException e) {
                      log.warn("Couldn't close attachment stream for " + filename, e);
                  }
                }           
            }
        }    
......
 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值