WORT:
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.poi.hwpf.extractor.WordExtractor;
java.io.File importieren;
import java.io.InputStream;
import java.io.FileInputStream;
import com.search.code.Index;
öffentliches Dokument getDocument(Indexindex, String-URL, String-Titel, InputStream ist) löst eine DocCenterException { aus
String bodyText = null;
versuchen {
WordExtractor ex = new WordExtractor(is);//is是WORD文件的InputStream
bodyText = ex.getText();
if(!bodyText.equals("")){
index.AddIndex(url, title, bodyText);
}
}catch (DocCenterException e) {
throw new DocCenterException("无法从该Mocriosoft Word文档中提取内容", e);
}catch(Ausnahme e){
e.printStackTrace();
}
}
null zurückgeben;
}
Excel:
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
java.io.File importieren;
import java.io.InputStream;
import java.io.FileInputStream;
import com.search.code.Index;
öffentliches Dokument getDocument(Indexindex, String-URL, String-Titel, InputStream ist) löst eine DocCenterException { aus
StringBuffer-Inhalt = new StringBuffer();
versuchen{
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 (short 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, title, content.toString());
}
}catch (DocCenterException e) {
throw new DocCenterException("无法从该Mocriosoft Word文档中提取内容", e);
}catch(Ausnahme e) {
System.out.println("已运行xlRead() : " + e );
}
null zurückgeben;
}
Steckdose:
import java.io.InputStream;
import org.apache.lucene.document.Document;
import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.model.TextRun;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.usermodel.SlideShow;
öffentliches Dokument getDocument(Indexindex, String-URL, String-Titel, InputStream ist)
wirft DocCenterException {
StringBuffer content = new StringBuffer("");
versuchen{
SlideShow ss = new SlideShow(new HSLFSlideShow(is));//is 为文件的InputStream,建立SlideShow
Slide[] slides = ss.getSlides();//Slide[] slides = ss.getSlides();//
for(int i=0;i<slides.length;i++){
TextRun[] t = slides[i].getTextRuns();//Sie können TextRun verwenden
for(int j=0;j<t.length;j++){
content.append(t[j].getText());//Erweiterte Inhaltsverzeichnisse
}
content.append(slides[i].getTitle());
}
index.AddIndex(url, title, content.toString());
}catch(Ausnahme ex){
System.out.println(ex.toString());
}
null zurückgeben;
}
PDF:
import java.io.InputStream;
import java.io.IOException;
import org.apache.lucene.document.Document;
import org.pdfbox.cos.COSDocument;
import org.pdfbox.pdfparser.PDFParser;
import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.pdmodel.PDDocumentInformation;
import org.pdfbox.util.PDFTextStripper;
import com.search.code.Index;
öffentliches Dokument getDocument(Indexindex, String-URL, String-Titel, InputStream ist)wirft DocCenterException {
COSDocument cosDoc = null;
versuchen {
cosDoc = parseDocument(is);
} Catch (IOException e) {
closeCOSDocument(cosDoc);
throw new DocCenterException("无法处理该PDF文档", e);
}
if (cosDoc.isEncrypted()) {
if (cosDoc != null)
closeCOSDocument(cosDoc);
throw new DocCenterException("该PDF文档是加密文档,无法处理");
}
String docText = null;
versuchen {
PDFTextStripper stripper = new PDFTextStripper();
docText = stripper.getText(new PDDocument(cosDoc));
} Catch (IOException e) {
closeCOSDocument(cosDoc);
throw new DocCenterException("无法处理该PDF文档", e);
}
PDDocument pdDoc = null;
versuchen {
pdDoc = neues PDDocument(cosDoc);
PDDocumentInformation docInfo = pdDoc.getDocumentInformation();
if(docInfo.getTitle()!=null && !docInfo.getTitle().equals("")){
title = docInfo.getTitle();
}
} Catch (Ausnahme e) {
closeCOSDocument(cosDoc);
closePDDocument(pdDoc);
System.err.println("无法取得该PDF文档的元数据" + e.getMessage());
} Endlich {
closeCOSDocument(cosDoc);
closePDDocument(pdDoc);
}
null zurückgeben;
}
privates statisches COSDocument parseDocument(InputStream is) wirft IOException {
PDFParser parser = new PDFParser(is);
parser.parse();
return parser.getDocument();
}
private void closeCOSDocument(COSDocument cosDoc) {
if (cosDoc != null) {
versuchen {
cosDoc.close();
} Catch (IOException e) {
}
}
}
private void closePDDocument(PDDocument pdDoc) {
if (pdDoc != null) {
versuchen {
pdDoc.close();
} Catch (IOException e) {
}
}
}
代码复制可能出错, 不过代码经过测试, 绝对能用, POI为3.0-rc4, PDFBOX为0.7.3
POI: http://jakarta.apache.org/poi/index.html
PDFBOX: http://www.pdfbox.org/