文档库--解析docx

This commit is contained in:
zhn
2022-06-10 08:36:56 +08:00
parent b94c0e767a
commit 78558b63ce
2 changed files with 59 additions and 14 deletions
@@ -1729,7 +1729,7 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
String wordContent = null;
try {
wordContent = ReadWordUtil.readWord(ossFile.getUrl());
wordContent = ReadWordUtil.readWord(ossFile.getUrl(),uploadpath);
} catch (Exception e) {
log.error("文档库: 读取word内容失败");
}
@@ -1872,7 +1872,7 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
|| ossFile.getFileName().endsWith(".DOCX")) {
String wordContent = null;
try {
wordContent = ReadWordUtil.readWord(ossFile.getUrl());
wordContent = ReadWordUtil.readWord(ossFile.getUrl(),uploadpath);
} catch (Exception e) {
log.error("文档库:读取word内容失败");
}
@@ -2665,7 +2665,7 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
|| ossFile.getFileName().endsWith(".DOCX")) {
String wordContent = null;
try {
wordContent = ReadWordUtil.readWord(ossFile.getUrl());
wordContent = ReadWordUtil.readWord(ossFile.getUrl(),uploadpath);
} catch (Exception e) {
log.error("文档库:读取word文件内容失败");
}
@@ -1,6 +1,7 @@
package com.jero.modules.document.utils;
import com.jero.common.exception.JeroBootException;
import com.jero.common.util.oss.CosBootUtil;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLTextExtractor;
import org.apache.poi.hwpf.extractor.WordExtractor;
@@ -8,32 +9,50 @@ import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import static com.jero.modules.document.service.impl.BussDocumentLibraryEOServiceImpl.copyFile;
/**
* @description
* @date 2022/3/21 17:04
* @auth zhn
*/
public class ReadWordUtil {
public static String readWord(String path) {
public static String readWord(String path,String uploadpath) {
// String path = "E:\\DeskTop\\1111.doc";
// path = "E:\\DeskTop\\22222.docx";
// String path = "E:\\DeskTop\\标准所ISO-用户手册.doc";
String str = "";
try {
if (path.endsWith(".doc")) {
InputStream is = new FileInputStream(new File(path));
WordExtractor ex = new WordExtractor(is);
str = ex.getText();
ex.close();
if(CosBootUtil.doesObjectExist(path)){
InputStream in = CosBootUtil.download(path);
WordExtractor ex = new WordExtractor(in);
str = ex.getText();
ex.close();
}
} else if (path.endsWith("docx")) {
OPCPackage opcPackage = POIXMLDocument.openPackage(path);
POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
str = extractor.getText();
extractor.close();
//先把文件存到本地, 用完后在删除
if(CosBootUtil.doesObjectExist(path)){
InputStream in = CosBootUtil.download(path);
String fileNowPath = uploadpath + "/tempZip/" ;
File file = new File(fileNowPath);
if (file.exists()) {
file.delete();
}
file.mkdirs();
File fileOld = new File(path);
long currentTime = System.currentTimeMillis();
copyFile(in, fileNowPath + File.separator + currentTime + fileOld.getName());
File fileTemp = new File(fileNowPath + File.separator + currentTime + fileOld.getName());
String pathTemp = fileTemp.getPath();
OPCPackage opcPackage = POIXMLDocument.openPackage(pathTemp);
POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
str = extractor.getText().trim();
extractor.close();
fileTemp.delete();
}
} else {
throw new JeroBootException("此文件不是word文件");
}
@@ -44,4 +63,30 @@ public class ReadWordUtil {
throw new JeroBootException("文件内容读取失败");
}
}
// public static String readWord(String path) {
//// String path = "E:\\DeskTop\\1111.doc";
//// path = "E:\\DeskTop\\22222.docx";
//// String path = "E:\\DeskTop\\标准所ISO-用户手册.doc";
// String str = "";
// try {
// if (path.endsWith(".doc")) {
// InputStream is = new FileInputStream(new File(path));
// WordExtractor ex = new WordExtractor(is);
// str = ex.getText();
// ex.close();
// } else if (path.endsWith("docx")) {
// OPCPackage opcPackage = POIXMLDocument.openPackage(path);
// POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
// str = extractor.getText();
// extractor.close();
// } else {
// throw new JeroBootException("此文件不是word文件");
// }
// return str;
// } catch (Exception e) {
// throw new JeroBootException("文件内容读取失败");
// }catch (NoSuchFieldError e){
// throw new JeroBootException("文件内容读取失败");
// }
// }
}