poi解析word文档转换成html(包括图片解析)
需求:将本地上传的word文档解析并放入数据库中
代码:
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
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 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.Picture;
import org.apache.poi.hwpf.usermodel.PictureType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.w3c.dom.Document;
import com.google.common.io.Files;
import com.zhaozhi.writing.service.service.FileService;
import com.zhaozhi.writing.service.service.WordParseService;
import com.zhaozhi.writing.service.util.FileUtil;
@Service
public class WordParseServiceImpl implements WordParseService {
@Autowired
private FileService fileService;
@Override
public String docToHtmlResult(MultipartFile file) throws Exception {
HWPFDocument wordDocument = new HWPFDocument(file.getInputStream());
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(document);
long currentTime = System.currentTimeMillis();
//设置图片URL
wordToHtmlConverter.setPicturesManager( new PicturesManager()
{
public String savePicture( byte[] content,
PictureType pictureType, String suggestedName,
float widthInches, float heightInches )
{
return FileUtil.OSS_DOMAIN+"/"+currentTime+"_"+suggestedName;
}
} );
// save pictures
List<Picture> pics = wordDocument.getPicturesTable().getAllPictures();
if (pics != null) {
for (int i = 0; i < pics.size(); i++) {
Picture pic = (Picture) pics.get(i);
System.out.println();
try {
String path = System.getProperty("java.io.tmpdir")+"/"+currentTime+"_"+pic.suggestFullFileName();
File picFile = new File(path);
Files.write(pic.getContent(),picFile );
fileService.upload(picFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
wordToHtmlConverter.processDocument(wordDocument);
Document htmlDocument = wordToHtmlConverter.getDocument();
ByteArrayOutputStream out = new ByteArrayOutputStream();
DOMSource domSource = new DOMSource(htmlDocument);
StreamResult streamResult = new StreamResult(out);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = tf.newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.METHOD, "html");
serializer.transform(domSource, streamResult);
out.close();
return new String(out.toByteArray());
}
}
通过poi包中的WordToHtmlConverter类实现word与html的转换;
关于图片的解析思路:先将word文档中的图片抽取出来,把图片流生成到tomcat的临时文件中(详见代码):
String path = System.getProperty("java.io.tmpdir")+"/"+currentTime+"_"+pic.suggestFullFileName();
File picFile = new File(path);
Files.write(pic.getContent(),picFile );
再将临时文件上传到阿里云服务器上生成图片url,最后将url地址放到html中进行展示即可;
poi解析word文档转换成html(包括图片解析)的更多相关文章
- JAVA:借用OpenOffice将上传的Word文档转换成Html格式
为什么会想起来将上传的word文档转换成html格式呢?设想,如果一个系统需要发布在页面的文章都是来自word文档,一般会执行下面的流程:使用word打开文档,Ctrl+A,进入发布文章页面,Ctrl ...
- OpenOffice Word文档转换成Html格式
为什么会想起来将上传的word文档转换成html格式呢?设想,如果一个系统需要发布在页面的文章都是来自word文档,一般会执行下面的流程:使用word打开文档,Ctrl+A,进入发布文章页面,Ctrl ...
- POI 生成 word 文档 简单版(包括文字、表格、图片、字体样式设置等)
POI 生成word 文档 一般有两种方法: ① word模板 生成word 文档 : ② 写代码直接生成 word 文档: 我这里演示的是第二种方法,即写代码生成 word文档,不多说废话,直接 ...
- Python将word文档转换成PDF文件
如题. 代码: ''' #將word文档转换为pdf文件 #用到的库是pywin32 #思路上是调用了windows和office功能 ''' #导入所需库 from win32com.client ...
- Java利用aspose-words将word文档转换成pdf(破解 无水印)
首先下载aspose-words-15.8.0-jdk16.jar包 http://pan.baidu.com/s/1nvbJwnv 引入jar包,编写Java代码 package doc; impo ...
- ASP.NET将word文档转换成pdf的代码
一.添加引用 using Microsoft.Office.Interop.Word; 二.转换方法 1.方法 C# 代码 /// <summary> /// 把Word文件转换成pdf文 ...
- Java实现批量将word文档转换成PDF
先导入words的jar包 需要jar包的私聊我发你 代码如下:import com.aspose.words.Document;import java.io.File; public class W ...
- C# word文档转换成PDF格式文档
最近用到一个功能word转pdf,有个方法不错,挺方便的,直接调用即可,记录下 方法:ConvertWordToPdf(string sourcePath, string targetPath) so ...
- word ppt excel文档转换成pdf
1.把word文档转换成pdf (1).添加引用 using Microsoft.Office.Interop.Word; 添加引用 (2).转换方法 /// <summary> /// ...
随机推荐
- SpringBoot-SpringCloud-版本对应关系
刚开始研究SpringCloud和SpringBoot的时候,困惑点比较多,SpringBoot是从1.X.X到目前的2.X.X版本,但是SpringCloud基于SpringBoot,版本之间肯定是 ...
- 数据库并发及锁机制及Hibernate锁实现
数据库事务的定义 数据库事务(Database Transaction),是指作为单个逻辑工作单元执行的一系列操作.一个逻辑工作单元要成为事务,必须满足所谓的ACID(原子性.一致性.隔离性和持久性) ...
- NHibernet Unable to locate persister for the entity
第一 xml文件必须为 *.hbm.xml 第二 设置xml文件为嵌入的资源,用鼠标点击右键 然后生成操作里 选择嵌入的资源即可解决. https://www.cnblogs.com/lyj/
- 【记录】STS设置maven远程仓库,加快下载jar包
在STS中windows->preferences->maven->user settings 修改user settings里setting.xml配置文件 <?xml ve ...
- IO复用: select 和poll 到epoll
linux 提供了select.poll和epoll三种接口来实现多路IO复用.下面总结下这三种接口. select 该函数允许进程指示内核等待多个事件中的任何一个发生,并只在有一个或多个事件发生或经 ...
- python5数据存储
1 txt文件存储 正常调用文件python文件操作 https://www.cnblogs.com/x2x3/p/9979919.html 2 json文件存储 在JavaScript语言中,一切都 ...
- 【Flutter学习】之DateTime日期转换
概述: 表示一个时间点 通过构造函数或解析格式化的字符串创建DateTime对象,并且符合ISO 8601标准的子集,小时是24小时制,范围在0-23之间 DateTime对象创建之后,将是固定不变的 ...
- 【leetcode】931. Minimum Falling Path Sum
题目如下: Given a square array of integers A, we want the minimum sum of a falling path through A. A fal ...
- 二进制&八进制&十六进制之间的快速转换------ 心算&笔算方法总结
二进制数 0&1两种元素: 8进制数 0-7 八种元素: 十六进制数 0-9,a,b,c,d,e, ...
- jmeter之-聚合报告&分析结果
Label:请求的名称 Sample:表示这次测试中一共发了多少个请求 Average:平均响应时间 median:中位数,也就是表示在所有请求响应时间中排在中间的那个响应的时间点,50%line 实 ...