|
|
|
@ -1,14 +1,15 @@
|
|
|
|
|
package com.supervision.utils;
|
|
|
|
|
|
|
|
|
|
import com.supervision.minio.domain.MinioFile;
|
|
|
|
|
import com.supervision.minio.service.MinioService;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.apache.poi.hwpf.HWPFDocument;
|
|
|
|
|
import org.apache.poi.hwpf.extractor.WordExtractor;
|
|
|
|
|
import org.apache.poi.poifs.filesystem.FileMagic;
|
|
|
|
|
import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
|
|
|
|
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
|
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
@ -57,20 +58,43 @@ public class WordReadUtil {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String readWord(InputStream inputStream) {
|
|
|
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
|
|
|
try (inputStream) {
|
|
|
|
|
// 创建 XWPFDocument 对象
|
|
|
|
|
XWPFDocument document = new XWPFDocument(inputStream);
|
|
|
|
|
// 获取所有段落
|
|
|
|
|
byte[] fileBytes;
|
|
|
|
|
try {
|
|
|
|
|
fileBytes = inputStream.readAllBytes();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(fileBytes);
|
|
|
|
|
try (InputStream is = FileMagic.prepareToCheckMagic(byteArrayInputStream)) {
|
|
|
|
|
FileMagic fm = FileMagic.valueOf(is);
|
|
|
|
|
// 判断文件类型 docx
|
|
|
|
|
if (FileMagic.OOXML == fm) {
|
|
|
|
|
try (byteArrayInputStream) {
|
|
|
|
|
XWPFDocument document = new XWPFDocument(byteArrayInputStream);
|
|
|
|
|
List<XWPFParagraph> paragraphs = document.getParagraphs();
|
|
|
|
|
// 遍历所有段落并打印文本
|
|
|
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
|
|
|
for (XWPFParagraph paragraph : paragraphs) {
|
|
|
|
|
stringBuilder.append(paragraph.getText()).append("\n");
|
|
|
|
|
}
|
|
|
|
|
return stringBuilder.toString();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("获取笔录内容失败",e);
|
|
|
|
|
throw new RuntimeException("获取笔录内容失败", e);
|
|
|
|
|
}
|
|
|
|
|
} else if (FileMagic.OLE2 == fm) {
|
|
|
|
|
// 判断文件类型 doc
|
|
|
|
|
try (byteArrayInputStream) {
|
|
|
|
|
HWPFDocument document = new HWPFDocument(byteArrayInputStream);
|
|
|
|
|
WordExtractor extractor = new WordExtractor(document);
|
|
|
|
|
return extractor.getText();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
throw new RuntimeException("获取笔录内容失败", e);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
throw new RuntimeException("不支持的文件类型");
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
return stringBuilder.toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String readWordInMinio(MinioService minioService, String fileId) {
|
|
|
|
|