fix:修复抽取空文件内容报错
This commit is contained in:
@@ -5,11 +5,11 @@ import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import com.adc.da.report.constant.ReportConstants;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.poi.hslf.usermodel.HSLFShape;
|
||||
import org.apache.poi.hslf.usermodel.HSLFSlide;
|
||||
import org.apache.poi.hslf.usermodel.HSLFTextShape;
|
||||
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
||||
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
||||
import org.apache.poi.ss.usermodel.Cell;
|
||||
import org.apache.poi.ss.usermodel.Row;
|
||||
import org.apache.poi.xslf.usermodel.XMLSlideShow;
|
||||
@@ -22,7 +22,6 @@ import org.apache.poi.hwpf.extractor.WordExtractor;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
||||
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
|
||||
import org.apache.poi.hslf.usermodel.HSLFSlideShow;
|
||||
import org.apache.poi.hslf.extractor.PowerPointExtractor;
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.text.PDFTextStripper;
|
||||
|
||||
@@ -31,6 +30,7 @@ import org.apache.pdfbox.text.PDFTextStripper;
|
||||
*
|
||||
* @author caihaohan
|
||||
*/
|
||||
@Slf4j
|
||||
public class FileContentUtil {
|
||||
|
||||
public static String readFileContent(File file) throws IOException {
|
||||
@@ -39,55 +39,58 @@ public class FileContentUtil {
|
||||
StringBuilder content = new StringBuilder();
|
||||
|
||||
FileInputStream fis = new FileInputStream(file);
|
||||
|
||||
switch (fileExtension) {
|
||||
case ReportConstants.FILE_EXTENSIONS_XLS:
|
||||
HSSFWorkbook workbookXls = new HSSFWorkbook(fis);
|
||||
content = new StringBuilder(readExcelContent(workbookXls));
|
||||
break;
|
||||
case ReportConstants.FILE_EXTENSIONS_XLSX:
|
||||
XSSFWorkbook workbookXlsx = new XSSFWorkbook(fis);
|
||||
content = new StringBuilder(readExcelContent(workbookXlsx));
|
||||
break;
|
||||
case ReportConstants.FILE_EXTENSIONS_DOC:
|
||||
HWPFDocument documentDoc = new HWPFDocument(fis);
|
||||
WordExtractor extractorDoc = new WordExtractor(documentDoc);
|
||||
content = new StringBuilder(extractorDoc.getText());
|
||||
break;
|
||||
case ReportConstants.FILE_EXTENSIONS_DOCX:
|
||||
XWPFDocument documentDocx = new XWPFDocument(fis);
|
||||
XWPFWordExtractor extractorDocx = new XWPFWordExtractor(documentDocx);
|
||||
content = new StringBuilder(extractorDocx.getText());
|
||||
break;
|
||||
case ReportConstants.FILE_EXTENSIONS_PPT:
|
||||
HSLFSlideShow ppt = new HSLFSlideShow(fis);
|
||||
for (HSLFSlide slide : ppt.getSlides()) {
|
||||
for (HSLFShape shape : slide.getShapes()) {
|
||||
if (shape instanceof HSLFTextShape) {
|
||||
HSLFTextShape textShape = (HSLFTextShape) shape;
|
||||
content.append(textShape.getText()).append("\n");
|
||||
try {
|
||||
switch (fileExtension) {
|
||||
case ReportConstants.FILE_EXTENSIONS_XLS:
|
||||
HSSFWorkbook workbookXls = new HSSFWorkbook(fis);
|
||||
content = new StringBuilder(readExcelContent(workbookXls));
|
||||
break;
|
||||
case ReportConstants.FILE_EXTENSIONS_XLSX:
|
||||
XSSFWorkbook workbookXlsx = new XSSFWorkbook(fis);
|
||||
content = new StringBuilder(readExcelContent(workbookXlsx));
|
||||
break;
|
||||
case ReportConstants.FILE_EXTENSIONS_DOC:
|
||||
HWPFDocument documentDoc = new HWPFDocument(fis);
|
||||
WordExtractor extractorDoc = new WordExtractor(documentDoc);
|
||||
content = new StringBuilder(extractorDoc.getText());
|
||||
break;
|
||||
case ReportConstants.FILE_EXTENSIONS_DOCX:
|
||||
XWPFDocument documentDocx = new XWPFDocument(fis);
|
||||
XWPFWordExtractor extractorDocx = new XWPFWordExtractor(documentDocx);
|
||||
content = new StringBuilder(extractorDocx.getText());
|
||||
break;
|
||||
case ReportConstants.FILE_EXTENSIONS_PPT:
|
||||
HSLFSlideShow ppt = new HSLFSlideShow(fis);
|
||||
for (HSLFSlide slide : ppt.getSlides()) {
|
||||
for (HSLFShape shape : slide.getShapes()) {
|
||||
if (shape instanceof HSLFTextShape) {
|
||||
HSLFTextShape textShape = (HSLFTextShape) shape;
|
||||
content.append(textShape.getText()).append("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case ReportConstants.FILE_EXTENSIONS_PPTX:
|
||||
XMLSlideShow pptx = new XMLSlideShow(fis);
|
||||
for (XSLFSlide slide : pptx.getSlides()) {
|
||||
for (XSLFShape shape : slide.getShapes()) {
|
||||
if (shape instanceof XSLFTextShape) {
|
||||
XSLFTextShape textShape = (XSLFTextShape) shape;
|
||||
content.append(textShape.getText()).append("\n");
|
||||
break;
|
||||
case ReportConstants.FILE_EXTENSIONS_PPTX:
|
||||
XMLSlideShow pptx = new XMLSlideShow(fis);
|
||||
for (XSLFSlide slide : pptx.getSlides()) {
|
||||
for (XSLFShape shape : slide.getShapes()) {
|
||||
if (shape instanceof XSLFTextShape) {
|
||||
XSLFTextShape textShape = (XSLFTextShape) shape;
|
||||
content.append(textShape.getText()).append("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case ReportConstants.FILE_EXTENSIONS_PDF:
|
||||
PDDocument document = PDDocument.load(fis);
|
||||
PDFTextStripper stripper = new PDFTextStripper();
|
||||
content = new StringBuilder(stripper.getText(document));
|
||||
default:
|
||||
break;
|
||||
case ReportConstants.FILE_EXTENSIONS_PDF:
|
||||
PDDocument document = PDDocument.load(fis);
|
||||
PDFTextStripper stripper = new PDFTextStripper();
|
||||
content = new StringBuilder(stripper.getText(document));
|
||||
default:
|
||||
}
|
||||
} catch (Exception e) {
|
||||
fis.close();
|
||||
log.info("文件内容抽取失败,原因可能是上传的文件为空文件");
|
||||
}
|
||||
fis.close();
|
||||
return content.toString();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user