文档库--解析docx
This commit is contained in:
+3
-3
@@ -1729,7 +1729,7 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
|
|||||||
|
|
||||||
String wordContent = null;
|
String wordContent = null;
|
||||||
try {
|
try {
|
||||||
wordContent = ReadWordUtil.readWord(ossFile.getUrl());
|
wordContent = ReadWordUtil.readWord(ossFile.getUrl(),uploadpath);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("文档库: 读取word内容失败");
|
log.error("文档库: 读取word内容失败");
|
||||||
}
|
}
|
||||||
@@ -1872,7 +1872,7 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
|
|||||||
|| ossFile.getFileName().endsWith(".DOCX")) {
|
|| ossFile.getFileName().endsWith(".DOCX")) {
|
||||||
String wordContent = null;
|
String wordContent = null;
|
||||||
try {
|
try {
|
||||||
wordContent = ReadWordUtil.readWord(ossFile.getUrl());
|
wordContent = ReadWordUtil.readWord(ossFile.getUrl(),uploadpath);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("文档库:读取word内容失败");
|
log.error("文档库:读取word内容失败");
|
||||||
}
|
}
|
||||||
@@ -2665,7 +2665,7 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
|
|||||||
|| ossFile.getFileName().endsWith(".DOCX")) {
|
|| ossFile.getFileName().endsWith(".DOCX")) {
|
||||||
String wordContent = null;
|
String wordContent = null;
|
||||||
try {
|
try {
|
||||||
wordContent = ReadWordUtil.readWord(ossFile.getUrl());
|
wordContent = ReadWordUtil.readWord(ossFile.getUrl(),uploadpath);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("文档库:读取word文件内容失败");
|
log.error("文档库:读取word文件内容失败");
|
||||||
}
|
}
|
||||||
|
|||||||
+56
-11
@@ -1,6 +1,7 @@
|
|||||||
package com.jero.modules.document.utils;
|
package com.jero.modules.document.utils;
|
||||||
|
|
||||||
import com.jero.common.exception.JeroBootException;
|
import com.jero.common.exception.JeroBootException;
|
||||||
|
import com.jero.common.util.oss.CosBootUtil;
|
||||||
import org.apache.poi.POIXMLDocument;
|
import org.apache.poi.POIXMLDocument;
|
||||||
import org.apache.poi.POIXMLTextExtractor;
|
import org.apache.poi.POIXMLTextExtractor;
|
||||||
import org.apache.poi.hwpf.extractor.WordExtractor;
|
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 org.apache.poi.xwpf.extractor.XWPFWordExtractor;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
import static com.jero.modules.document.service.impl.BussDocumentLibraryEOServiceImpl.copyFile;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description
|
* @description
|
||||||
* @date 2022/3/21 17:04
|
* @date 2022/3/21 17:04
|
||||||
* @auth zhn
|
* @auth zhn
|
||||||
*/
|
*/
|
||||||
public class ReadWordUtil {
|
public class ReadWordUtil {
|
||||||
|
public static String readWord(String path,String uploadpath) {
|
||||||
public static String readWord(String path) {
|
|
||||||
// String path = "E:\\DeskTop\\1111.doc";
|
// String path = "E:\\DeskTop\\1111.doc";
|
||||||
// path = "E:\\DeskTop\\22222.docx";
|
// path = "E:\\DeskTop\\22222.docx";
|
||||||
// String path = "E:\\DeskTop\\标准所ISO-用户手册.doc";
|
// String path = "E:\\DeskTop\\标准所ISO-用户手册.doc";
|
||||||
String str = "";
|
String str = "";
|
||||||
try {
|
try {
|
||||||
if (path.endsWith(".doc")) {
|
if (path.endsWith(".doc")) {
|
||||||
InputStream is = new FileInputStream(new File(path));
|
if(CosBootUtil.doesObjectExist(path)){
|
||||||
WordExtractor ex = new WordExtractor(is);
|
InputStream in = CosBootUtil.download(path);
|
||||||
str = ex.getText();
|
WordExtractor ex = new WordExtractor(in);
|
||||||
ex.close();
|
str = ex.getText();
|
||||||
|
ex.close();
|
||||||
|
}
|
||||||
} else if (path.endsWith("docx")) {
|
} else if (path.endsWith("docx")) {
|
||||||
OPCPackage opcPackage = POIXMLDocument.openPackage(path);
|
//先把文件存到本地, 用完后在删除
|
||||||
POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
|
if(CosBootUtil.doesObjectExist(path)){
|
||||||
str = extractor.getText();
|
InputStream in = CosBootUtil.download(path);
|
||||||
extractor.close();
|
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 {
|
} else {
|
||||||
throw new JeroBootException("此文件不是word文件");
|
throw new JeroBootException("此文件不是word文件");
|
||||||
}
|
}
|
||||||
@@ -44,4 +63,30 @@ public class ReadWordUtil {
|
|||||||
throw new JeroBootException("文件内容读取失败");
|
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("文件内容读取失败");
|
||||||
|
// }
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user