package com.example.solr.util;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
@Slf4j
public class FileUtil {
/**
* 读取文档内容
* 目前仅支持 txt, doc, docx, pdf 格式
*
* @param file 文件对象
* @return 文档内容
*/
public static String readFileContent(MultipartFile file) {
String filename = file.getOriginalFilename();
assert filename != null;
String fileType = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
switch (fileType) {
case "TXT":
return readTxtContent(file);
case "DOC":
case "DOCX":
return readWordContent(file);
case "PDF":
return readPDFContent(file);
default:
return "";
}
}
/**
* 读取 txt 文档内容
*/
private static String readTxtContent(MultipartFile file) {
try {
return StrUtil.utf8Str(file.getBytes());
} catch (IOException e) {
e.printStackTrace();
log.error("读取 txt 文件内容时发生 IO 异常");
return "";
}
}
/**
* 读取 doc 和 docx 文档内容
*/
private static String readWordContent(MultipartFile file) {
String filename = file.getOriginalFilename();
assert filename != null;
String fileType = filename.substring(filename.lastIndexOf(".") + 1);
return "doc".equals(fileType) ? readDocContent(file) : readDocxContent(file);
}
/**
* 读取 .doc 格式的 word 文档
*
* @param file 文件对象
* @return 文本内容
*/
private static String readDocContent(MultipartFile file) {
StringBuilder content = new StringBuilder();
try (InputStream inputStream = file.getInputStream();
HWPFDocument document = new HWPFDocument(inputStream)) {
WordExtractor extractor = new WordExtractor(document);
String[] paragraphs = extractor.getParagraphText();
for (String paragraph : paragraphs) {
content.append(paragraph);
}
} catch (IOException e) {
e.printStackTrace();
log.error("读取文件内容时发生 IO 异常");
}
return content.toString();
}
/**
* 读取 .docx 格式的 word 文档
*
* @param file 文件对象
* @return 文本内容
*/
private static String readDocxContent(MultipartFile file) {
StringBuilder content = new StringBuilder();
try (InputStream inputStream = file.getInputStream();
XWPFDocument document = new XWPFDocument(inputStream)) {
for (XWPFParagraph paragraph : document.getParagraphs()) {
for (XWPFRun run : paragraph.getRuns()) {
String runText = run.getText(0);
if (runText != null) {
content.append(runText);
}
}
}
} catch (IOException e) {
e.printStackTrace();
log.error("读取文件内容时发生 IO 异常");
}
return content.toString();
}
/**
* 读取 pdf 文档内容
*/
private static String readPDFContent(MultipartFile file) {
StringBuilder content = new StringBuilder();
try (InputStream inputStream = file.getInputStream();
PDDocument document = PDDocument.load(inputStream)) {
// 检查是否是由文档转换出的 pdf 文件
if (!document.isEncrypted() && document.getNumberOfPages() > 0) {
PDFTextStripper textStripper = new PDFTextStripper();
content.append(textStripper.getText(document));
} else {
log.warn("PDF 已加密或不是由文档转换的 PDF 格式,无法读取内容!");
}
} catch (IOException e) {
e.printStackTrace();
log.error("读取文件内容时发生 IO 异常");
}
return content.toString();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
SpringBoot集成Solr全文检索.zip (25个子文件)
SpringBoot集成Solr全文检索.md 24KB
assets
image-20240325110619861.png 124KB
image-20240325101641526.png 33KB
image-20240325101622629.png 44KB
image-20240325110429464.png 87KB
image-20240325110525175.png 115KB
image-20240325101838605.png 21KB
image-20240325110345371.png 111KB
image-20240325101710805.png 20KB
image-20240325102950505.png 22KB
image-20240325101556991.png 36KB
image-20240325102919445.png 74KB
solr-example
pom.xml 3KB
src
main
resources
application.yml 274B
java
com
example
solr
repository
MyDocumentRepository.java 1KB
SolrExampleApplication.java 447B
controller
MyDocumentController.java 4KB
service
MyDocumentService.java 538B
impl
MyDocumentServiceImpl.java 2KB
entity
MyDocument.java 803B
util
FileUtil.java 5KB
config
SolrConfig.java 3KB
SpringBoot集成Solr全文检索.html 365KB
solr-example.zip 11KB
ik-analyzer-8.5.0.jar 9.23MB
共 25 条
- 1
资源评论
鬼畜的稀饭
- 粉丝: 4784
- 资源: 18
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功