单词:
导入 org.apache.lucene.document.Document;
导入 org.apache.lucene.document.Field;
导入 org.apache.poi.hwpf.extractor.WordExtractor;
导入java.io.File;
导入 java.io.InputStream;
导入 java.io.FileInputStream;
导入com.search.code.Index;
public Document getDocument(Index index, String url, String title, InputStream is) throws DocCenterException {
字符串正文文本 = null;
尝试 {
WordExtractor ex = new WordExtractor(is);//is是WORD文件的InputStream
bodyText = ex.getText();
if(!bodyText.equals("")){
index.AddIndex(url, 标题, bodyText);
}
}catch (DocCenterException e) {
throw new DocCenterException("无法从 Mocriosoft Word 文档中提取内容", e);
}捕获(异常e){
e.printStackTrace();
}
}
返回空值;
}
Excel:
导入 org.apache.lucene.document.Document;
导入 org.apache.lucene.document.Field;
导入 org.apache.poi.hwpf.extractor.WordExtractor;
导入 org.apache.poi.hssf.usermodel.HSSFWorkbook;
导入 org.apache.poi.hssf.usermodel.HSSFSheet;
导入 org.apache.poi.hssf.usermodel.HSSFRow;
导入 org.apache.poi.hssf.usermodel.HSSFCell;
导入java.io.File;
导入 java.io.InputStream;
导入 java.io.FileInputStream;
导入com.search.code.Index;
public Document getDocument(Index index, String url, String title, InputStream is) throws DocCenterException {
StringBuffer 内容 = new StringBuffer();
尝试{
HSSFWorkbook workbook = new HSSFWorkbook(is);//创建对Excel工作簿文件的引用
for (int numSheets = 0; numSheets < workbook.getNumberOfSheets(); numSheets++) {
if (null != workbook.getSheetAt(numSheets)) {
HSSFSheet aSheet = workbook.getSheetAt(numSheets);//获得一张sheet
for (int rowNumOfSheet = 0; rowNumOfSheet <= aSheet.getLastRowNum(); rowNumOfSheet++) {
if (null != aSheet.getRow(rowNumOfSheet)) {
HSSFRow aRow = aSheet.getRow(rowNumOfSheet); //获得一个行
for (短 cellNumOfRow = 0; cellNumOfRow <= aRow.getLastCellNum(); cellNumOfRow++) {
if (null != aRow.getCell(cellNumOfRow)) {
HSSFCell aCell = aRow.getCell(cellNumOfRow);//获得列值
content.append(aCell.getStringCellValue());
}
}
}
}
}
}
if(!content.equals("")){
index.AddIndex(url, 标题, content.toString());
}
}catch (DocCenterException e) {
throw new DocCenterException("无法从 Mocriosoft Word 文档中提取内容", e);
}catch(异常e){
System.out.println("已运行xlRead() : " + e );
}
返回空值;
}
微软幻灯片软件:
导入 java.io.InputStream;
导入 org.apache.lucene.document.Document;
导入 org.apache.poi.hslf.HSLFSlideShow;
导入 org.apache.poi.hslf.model.TextRun;
导入 org.apache.poi.hslf.model.Slide;
导入 org.apache.poi.hslf.usermodel.SlideShow;
public Document getDocument(索引索引,字符串url,字符串标题,InputStream is)
抛出 DocCenterException {
StringBuffer 内容 = new StringBuffer("");
尝试{
SlideShow ss = new SlideShow(new HSLFSlideShow(is));//is为文件的InputStream,创建SlideShow
Slide[] Slides = ss.getSlides();//获得每张幻灯片
for(int i=0;i<slides.length;i++){
TextRun[] t = Slides[i].getTextRuns();//为了获取幻灯片的文字内容,创建TextRun
for(int j=0;j<t.length;j++){
content.append(t[j].getText());//这里粘贴文字内容加到content中去
}
content.append(slides[i].getTitle());
}
index.AddIndex(url, 标题, content.toString());
}catch(异常前){
System.out.println(例如.toString());
}
返回空值;
}
PDF:
导入 java.io.InputStream;
导入java.io.IOException;
导入 org.apache.lucene.document.Document;
导入 org.pdfbox.cos.COSDocument;
导入 org.pdfbox.pdfparser.PDFParser;
导入 org.pdfbox.pdmodel.PDDocument;
导入 org.pdfbox.pdmodel.PDDocumentInformation;
导入 org.pdfbox.util.PDFTextStripper;
导入com.search.code.Index;
public Document getDocument(索引索引,字符串url,字符串标题,InputStream is)抛出DocCenterException {
COSDocument cosDoc = null;
尝试 {
cosDoc = parseDocument(is);
} catch (IOException e) {
关闭COSDocument(cosDoc);
throw new DocCenterException("无法处理该PDF文档", e);
}
if (cosDoc.isEncrypted()) {
if (cosDoc != null)
关闭COSDocument(cosDoc);
throw new DocCenterException("该PDF文档是加密文档,无法处理");
}
字符串 docText = null;
尝试 {
PDFTextStripper 剥离器 = new PDFTextStripper();
docText = stripper.getText(new PDDocument(cosDoc));
} catch (IOException e) {
关闭COSDocument(cosDoc);
throw new DocCenterException("无法处理该PDF文档", e);
}
PDDocument pdDoc = null;
尝试 {
pdDoc = new PDDocument(cosDoc);
PDDocumentInformation docInfo = pdDoc.getDocumentInformation();
if(docInfo.getTitle()!=null && !docInfo.getTitle().equals("")){
标题 = docInfo.getTitle();
}
} catch (异常 e) {
关闭COSDocument(cosDoc);
关闭PDDocument(pdDoc);
System.err.println("无法获取该PDF文档的元数据" + e.getMessage());
} 最后 {
关闭COSDocument(cosDoc);
关闭PDDocument(pdDoc);
}
返回空值;
}
私有静态 COSDocument parseDocument(InputStream is) 抛出 IOException {
PDFParser 解析器 = new PDFParser(is);
解析器.parse();
返回 parser.getDocument();
}
私有无效 closeCOSDocument(COSDocument cosDoc) {
if (cosDoc != null) {
尝试 {
cosDoc.close();
} catch (IOException e) {
}
}
}
私有无效 closePDDocument(PDDocument pdDoc) {
if (pdDoc != null) {
尝试 {
pdDoc.close();
} catch (IOException e) {
}
}
}
代码复制可能出错,不过代码经过测试,绝对能用,POI为3.0-rc4,PDFBOX为0.7.3
兴趣点:http://jakarta.apache.org/poi/index.html
PDFBOX:http://www.pdfbox.org/