import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.converter.WordToHtmlConverter;
import org.apache.poi.xwpf.converter.core.BasicURIResolver;
import org.apache.poi.xwpf.converter.core.FileImageExtractor;
import org.apache.poi.xwpf.converter.xhtml.XHTMLConverter;
import org.apache.poi.xwpf.converter.xhtml.XHTMLOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.w3c.dom.Document;
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.*;
/**
* @date
* @description
*/
public class WordToHtmlUtil {
private static final Logger logger = LoggerFactory.getLogger(WordToHtmlUtil.class);
/**
* 上传Word文档,返回解析后的Html
*/
public static String docToHtmlText(MultipartFile file) throws Exception {
//使用字符数组流获取解析的内容
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream outStream = new BufferedOutputStream(baos);
try {
//将上传的文件传入Document转换
HWPFDocument wordDocument = new HWPFDocument(file.getInputStream());
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(document);
//将读取到的图片上传并添加链接地址
//wordToHtmlConverter.setPicturesManager((imageStream, pictureType, name, width, height) -> {
// try {
// //首先要判断图片是否能识别
// if (pictureType.equals(PictureType.UNKNOWN)) {
// return "[不能识别的图片]";
// }
// //此处上传到自己的文件服务器 todo
// String qiNiuName = "";//文件名
// boolean upload = FileUtil.upload(new FileInputStream(fileImage), qiNiuName);
// return "上传后的图片地址";
//
// } catch (Exception e) {
// logger.info("upload exception", e);
// }
// return "[图片上传失败]";
//});
// word文档转Html文档
wordToHtmlConverter.processDocument(wordDocument);
Document htmlDocument = wordToHtmlConverter.getDocument();
DOMSource domSource = new DOMSource(htmlDocument);
StreamResult streamResult = new StreamResult(outStream);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer serializer = factory.newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.METHOD, "html");
serializer.transform(domSource, streamResult);
String content = baos.toString();
logger.info("docToHtmlText--->{}", content);
return content;
} catch (Exception e) {
logger.error("docToHtmlText 异常", e);
} finally {
baos.close();
outStream.close();
}
return "";
}
/**
* 上传docx文档,返回解析后的Html
*/
public static String docxToHtmlText(MultipartFile file) throws Exception {
ByteArrayOutputStream htmlStream = new ByteArrayOutputStream();
try {
// 将上传的文件传入Document转换
XWPFDocument docxDocument = new XWPFDocument(file.getInputStream());
XHTMLOptions options = XHTMLOptions.create();
// 设置图片存储路径
String path = System.getProperty("java.io.tmpdir");
String firstImagePathStr = path + "/" + System.currentTimeMillis();
options.setExtractor(new FileImageExtractor(new File(firstImagePathStr)));
options.URIResolver(new BasicURIResolver(firstImagePathStr));
// 转换html
docxDocument.createNumbering();
XHTMLConverter.getInstance().convert(docxDocument, htmlStream, options);
String htmlStr = htmlStream.toString();
String middleImageDirStr = "/word/media";
String imageDirStr = firstImagePathStr + middleImageDirStr;
File imageDir = new File(imageDirStr);
String[] imageList = imageDir.list();
if (imageList != null) {
for (int i = 0; i < imageList.length; i++) {
try {
String oneImagePathStr = imageDirStr + "/" + imageList[i];
File fileImage = new File(oneImagePathStr);
if (fileImage.exists()) {
String name = fileImage.getName();
String suffix = name.substring(name.indexOf("."), name.length()).toLowerCase();
//此处上传到自己的文件服务器 todo
String qiNiuName = "";//文件名
//boolean upload = FileUtil.upload(new FileInputStream(fileImage), qiNiuName);
//if (!upload) {
// continue;
//} else {
// //修改文档中的图片信息
// htmlStr = htmlStr.replace(oneImagePathStr, "上传后的图片地址");
//}
}
} catch (Exception e) {
logger.info("upload docxToHtmlText exception", e);
}
}
}
//删除图片路径
File firstImagePath = new File(firstImagePathStr);
FileUtils.deleteDirectory(firstImagePath);
return htmlStr;
} catch (Exception e) {
logger.error("docxToHtmlText 解析异常", e);
} finally {
if (htmlStream != null) {
htmlStream.close();
}
}
return "";
}
public static void main(String[] args) {
try {
String content = docxToHtmlText(getMulFileByPath("C:\\Users\\xxxx\\Desktop\\新建文件.docx"));
System.out.println(content);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取MultipartFile文件
*
* @param picPath
* @return
*/
public static MultipartFile getMulFileByPath(String picPath) {
FileItem fileItem = createFileItem(picPath);
MultipartFile mfile = new CommonsMultipartFile(fileItem);
return mfile;
}
private static FileItem createFileItem(String filePath) {
FileItemFactory factory = new DiskFileItemFactory(16, null);
String textFieldName = "textField";
int num = filePath.lastIndexOf(".");
String extFile = filePath.substring(num);
FileItem item = factory.createItem(textFieldName, "text/plain", true,
"MyFileName" + extFile);
File newfile = new File(filePath);
int bytesRead = 0;
byte[] buffer = new byte[8192];
try {
FileInputStream fis = new FileInputStream(newfile);
OutputStream os = item.getOutputStream();
while ((bytesRead = fis.read(buffer, 0, 8192))
!= -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
return item;
}
}
代码如上,pom文件引包如下,经自测该版本可用。
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId>
<version>1.0.6</version>
</dependency>
word中的中文,在生成HTML之后,并非以中文显示,但不影响在浏览器中显示。
生成的HTML内容,可以用cn.hutool.http.HtmlUtil的这俩方法处理一下,不然在手机端看不友好,因为保留了完整的word格式。
String div = HtmlUtil.unwrapHtmlTag(articleContent, "div");
String head = HtmlUtil.removeHtmlTag(div, "head");最终的head字段,既有完成内容,也有富文本格式,还能适配PC和APP端。