/**
* doc格式文件转换成html支持图片上传至阿里云
*
* @param input
* @return
* @throws IOException
* @throws ParserConfigurationException
* @throws TransformerException
*/
private String doc2Html(InputStream input) throws IOException, ParserConfigurationException, TransformerException {
HWPFDocument wordDocument = new HWPFDocument(input);
WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
//设置图片存放的位置
wordToHtmlConverter.setPicturesManager((content, pictureType, suggestedName, widthInches, heightInches) -> {
String path = "resource/wordPicture/" + UUID.randomUUID() + "." + pictureType.getExtension();
try {
InputStream in = new ByteArrayInputStream(content);
//上传到阿里云或者本地
ossUtil.upload(path, in);
in.close();
} catch (Exception e) {
e.printStackTrace();
path = "upload picture exception";
} finally {
}
return path;
});
//解析word文档
wordToHtmlConverter.processDocument(wordDocument);
org.w3c.dom.Document htmlDocument = wordToHtmlConverter.getDocument();
//也可以使用字符数组流获取解析的内容
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream outStream = new BufferedOutputStream(baos);
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();
baos.close();
return content;
}//调用
@RequestMapping("/testDoc2Html")
public voide test(@RequestParam MultipartFile file,
HttpServletResponse response) throws Exception {
InputStream input = file.getInputStream();
String content = doc2Html(input);
System.out.println(content);
}
【使用poi进行word doc文件转成html】
推荐阅读
- Java|Java基础——数组
- 人工智能|干货!人体姿态估计与运动预测
- java简介|Java是什么(Java能用来干什么?)
- Java|规范的打印日志
- Linux|109 个实用 shell 脚本
- 程序员|【高级Java架构师系统学习】毕业一年萌新的Java大厂面经,最新整理
- Spring注解驱动第十讲--@Autowired使用
- SqlServer|sql server的UPDLOCK、HOLDLOCK试验
- jvm|【JVM】JVM08(java内存模型解析[JMM])
- 技术|为参加2021年蓝桥杯Java软件开发大学B组细心整理常见基础知识、搜索和常用算法解析例题(持续更新...)