提交 706b0f52 编写于 作者: O o2null

Merge branch 'fix/toHtml' into 'develop'

通过word导入表单支持docx,修正字符集

See merge request o2oa/o2oa!376
......@@ -2,16 +2,19 @@ package com.x.general.assemble.control.jaxrs.office;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.converter.PicturesManager;
import org.apache.poi.hwpf.converter.WordToHtmlConverter;
import org.apache.poi.hwpf.usermodel.PictureType;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
......@@ -27,22 +30,42 @@ import com.x.base.core.project.logger.LoggerFactory;
import fr.opensagres.poi.xwpf.converter.xhtml.XHTMLConverter;
import fr.opensagres.poi.xwpf.converter.xhtml.XHTMLOptions;
import fr.opensagres.xdocreport.converter.Options;
import fr.opensagres.xdocreport.converter.docx.poi.xhtml.XWPF2XHTMLConverter;
public class ActionToHtml extends BaseAction {
private static Logger logger = LoggerFactory.getLogger(ActionToHtml.class);
private static final Logger LOGGER = LoggerFactory.getLogger(ActionToHtml.class);
private static final String TYPE_DOC = "application/msword";
private static final String TYPE_DOCX = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
/**
* 只能使用 xdocreport 2.0.2 版本,2.0.3版本报错 Caused by: java.lang.NoSuchFieldError:
* Factory at
* fr.opensagres.poi.xwpf.converter.core.styles.XWPFStylesDocument$FontsDocumentVisitor.visitDocumentPart(XWPFStylesDocument.java:1600)
*
* @param effectivePerson
* @param bytes
* @param disposition
* @return
* @throws ParserConfigurationException
* @throws IOException
* @throws TransformerException
* @throws ExceptionUnsupportType
*/
ActionResult<Wo> execute(EffectivePerson effectivePerson, byte[] bytes, FormDataContentDisposition disposition)
throws Exception {
throws ParserConfigurationException, IOException, TransformerException, ExceptionUnsupportType {
LOGGER.debug("{} execute, fileName:{}.", effectivePerson::getDistinguishedName, disposition::getFileName);
ActionResult<Wo> result = new ActionResult<>();
Tika tika = new Tika();
String type = tika.detect(bytes);
Wo wo = new Wo();
switch (type) {
case ("application/msword"):
case (TYPE_DOC):
wo.setValue(this.doc(bytes));
break;
case ("application/vnd.openxmlformats-officedocument.wordprocessingml.document"):
case (TYPE_DOCX):
wo.setValue(this.docx(bytes));
break;
default:
......@@ -52,15 +75,11 @@ public class ActionToHtml extends BaseAction {
return result;
}
private String doc(byte[] bytes) throws Exception {
private String doc(byte[] bytes) throws ParserConfigurationException, IOException, TransformerException {
WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(
DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
wordToHtmlConverter.setPicturesManager(new PicturesManager() {
public String savePicture(byte[] content, PictureType pictureType, String suggestedName, float widthInches,
float heightInches) {
return suggestedName;
}
});
wordToHtmlConverter.setPicturesManager((byte[] content, PictureType pictureType, String suggestedName,
float widthInches, float heightInches) -> suggestedName);
try (ByteArrayInputStream in = new ByteArrayInputStream(bytes);
ByteArrayOutputStream out = new ByteArrayOutputStream()) {
HWPFDocument wordDocument = new HWPFDocument(in);
......@@ -68,29 +87,29 @@ public class ActionToHtml extends BaseAction {
Document htmlDocument = wordToHtmlConverter.getDocument();
DOMSource domSource = new DOMSource(htmlDocument);
StreamResult streamResult = new StreamResult(out);
TransformerFactory tf = TransformerFactory.newInstance();
TransformerFactory tf = TransformerFactory.newDefaultInstance();
Transformer serializer = tf.newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "GB2312");// 编码格式
serializer.setOutputProperty(OutputKeys.ENCODING, StandardCharsets.UTF_8.name());// 编码格式
serializer.setOutputProperty(OutputKeys.INDENT, "yes");// 是否用空白分割
serializer.setOutputProperty(OutputKeys.METHOD, "html");// 输出类型
serializer.transform(domSource, streamResult);
String content = new String(out.toByteArray());
return content;
return new String(out.toByteArray(), StandardCharsets.UTF_8);
}
}
private String docx(byte[] bytes) throws Exception {
private String docx(byte[] bytes) throws IOException {
try (ByteArrayInputStream in = new ByteArrayInputStream(bytes);
ByteArrayOutputStream out = new ByteArrayOutputStream()) {
XWPFDocument document = new XWPFDocument(in);
XHTMLOptions options = XHTMLOptions.create();
XHTMLConverter.getInstance().convert(document, out, options);
return new String(out.toByteArray());
Options options = Options.getFrom("DOCX");
XHTMLOptions xhtmlOptions = XWPF2XHTMLConverter.getInstance().toXHTMLOptions(options);
XHTMLConverter.getInstance().convert(document, out, xhtmlOptions);
return new String(out.toByteArray(), StandardCharsets.UTF_8);
}
}
public static class Wo extends WrapString {
private static final long serialVersionUID = 6581539366197332222L;
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册