With features such as full-text search, you may want to search the contents of uploaded files. When creating an index such as ElasticSearch, extract the text from the file and add it to the index document.
Extracting tools include POI and Tika.
POI:https://poi.apache.org/ Tika:https://tika.apache.org/
POI
Java API for Microsoft Documents You can create, edit, and extract Word, Excel, and PowerPoint.
Introduced POI library
build.gradle
// https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml
compile group: 'org.apache.poi', name: 'poi-ooxml', version: '4.1.0'
Excel⇒text
ExcelExtractor.java
import java.io.File;
import java.io.IOException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xssf.extractor.XSSFExcelExtractor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbookFactory;
public class ExcelExtractor {
public static void main(String[] args) throws InvalidFormatException, IOException {
XSSFWorkbook workbook = XSSFWorkbookFactory.createWorkbook(new File("/data/test.xlsx"), true);
XSSFExcelExtractor excel = new XSSFExcelExtractor(workbook);
//Extract text
System.out.println(excel.getText());
//close
excel.close();
}
}
Word⇒text
WordExtractor.java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
public class WordExtractor {
public static void main(String[] args) throws FileNotFoundException, IOException {
XWPFDocument doc = new XWPFDocument(new FileInputStream(new File("/data/test.docx")));
XWPFWordExtractor word = new XWPFWordExtractor(doc);
//Extract text
System.out.println(word.getText());
//close
word.close();
}
}
PowerPoint⇒text
PowerPointExtractor.java
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.xslf.extractor.XSLFPowerPointExtractor;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
public class PowerPointExtractor {
public static void main(String[] args) throws FileNotFoundException, IOException {
XMLSlideShow ppt = new XMLSlideShow(new FileInputStream("/data/test.pptx"));
XSLFPowerPointExtractor powerPointExtractor = new XSLFPowerPointExtractor(ppt);
//Extract text
System.out.println(powerPointExtractor.getText());
//close
powerPointExtractor.close();
}
}
Tika
The Apache Tika™ toolkit detects and extracts metadata and text from over a thousand different file types。 Document metadata, text extraction, file type detection, and more.
build.gradle
// https://mvnrepository.com/artifact/org.apache.tika/tika-core
compile group: 'org.apache.tika', name: 'tika-core', version: '1.22'
// https://mvnrepository.com/artifact/org.apache.tika/tika-parsers
compile group: 'org.apache.tika', name: 'tika-parsers', version: '1.22'
TikaExtractor.java
import java.io.File;
import java.io.IOException;
import org.apache.tika.Tika;
import org.apache.tika.exception.TikaException;
public class TikaExtractor {
public static void main(String[] args) throws IOException, TikaException {
Tika tika = new Tika();
String data = tika.parseToString(new File("/data/test.pdf"));
System.out.println(data);
}
}
It is convenient because it can be extracted from text files, PDF, Word, Excel, PowerPoint, etc.
that's all