server_commit
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
package io.v.nutz.base.utils;
|
||||
|
||||
import fr.opensagres.poi.xwpf.converter.xhtml.XHTMLConverter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.poi.hwpf.HWPFDocument;
|
||||
import org.apache.poi.hwpf.converter.WordToHtmlConverter;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFPictureData;
|
||||
import org.jsoup.Jsoup;
|
||||
import org.jsoup.nodes.Document;
|
||||
import org.jsoup.nodes.Element;
|
||||
import org.jsoup.select.Elements;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.transform.OutputKeys;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author jug
|
||||
* @date 2024/02/29
|
||||
*/
|
||||
@Slf4j
|
||||
public class WordUtil {
|
||||
|
||||
public static String checkConvert2Html(File file) throws Exception {
|
||||
String fileName = file.getName().toLowerCase();
|
||||
if (fileName.endsWith(".doc")) {
|
||||
return doc2Html(file);
|
||||
} else if (fileName.endsWith(".docx")) {
|
||||
return docx2Html(file);
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public static String doc2Html(File file) throws Exception {
|
||||
try (FileInputStream inputStream = new FileInputStream(file)) {
|
||||
HWPFDocument wordDocument = new HWPFDocument(inputStream);
|
||||
WordToHtmlConverter converter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
|
||||
converter.setPicturesManager((bytes, pictureType, s, v, v1) -> {
|
||||
String type = pictureType.name();
|
||||
return "data:image/" + type + ";base64," + Base64.getEncoder().encodeToString(bytes);
|
||||
});
|
||||
converter.processDocument(wordDocument);
|
||||
Transformer transformer = TransformerFactory.newInstance().newTransformer();
|
||||
transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
|
||||
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
||||
transformer.setOutputProperty(OutputKeys.METHOD, "html");
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
transformer.transform(new DOMSource(converter.getDocument()), new StreamResult(outputStream));
|
||||
return outputStream.toString(StandardCharsets.UTF_8);
|
||||
} catch (Exception e) {
|
||||
log.error("Error converting doc to html: " + e.getMessage());
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public static String docx2Html(File file) throws Exception {
|
||||
try (FileInputStream inputStream = new FileInputStream(file);
|
||||
XWPFDocument document = new XWPFDocument(inputStream)) {
|
||||
|
||||
List<XWPFPictureData> list = document.getAllPictures();
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
XHTMLConverter.getInstance().convert(document, outputStream, null);
|
||||
String html = new String(outputStream.toByteArray());
|
||||
Document doc = Jsoup.parse(html);
|
||||
Elements elements = doc.getElementsByTag("img");
|
||||
|
||||
if (elements != null && elements.size() > 0 && list != null) {
|
||||
for (Element element : elements) {
|
||||
String src = element.attr("src");
|
||||
for (XWPFPictureData data : list) {
|
||||
if (src.contains(data.getFileName())) {
|
||||
String type = src.substring(src.lastIndexOf(".") + 1);
|
||||
String base64 = "data:image/" + type + ";base64," + Base64.getEncoder().encodeToString(data.getData());
|
||||
element.attr("src", base64);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return doc.html();
|
||||
} catch (Exception e) {
|
||||
log.error("Error converting docx to html: " + e.getMessage());
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user