前言
自己找了很多相关博客对doc文档的预览都没有好的解决办法,在前端Vue使用Vue-office组件也只能预览Word2003之后的文件类型docx。又去找后台doc转docx的方法结果也是一塌糊涂。然后自己根据前辈们的博客找到一种投机取巧的办法。
一、引入poi包
<!--新版本的poi已经将读取doc文件的类剔除掉了,需要单独引包-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>4.1.2</version>
</dependency>
<!-- poi工具 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
二、读取doc文件转成html返回给前端
1.转html字符串方法
代码如下(示例):
public String docToHtml(HWPFDocument wordDocument) {
String content = null;
ByteArrayOutputStream baos = null;
try {
WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
wordToHtmlConverter.processDocument(wordDocument);
Document htmlDocument = wordToHtmlConverter.getDocument();
DOMSource domSource = new DOMSource(htmlDocument);
baos = new ByteArrayOutputStream();
StreamResult streamResult = new StreamResult(baos);
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);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (baos != null) {
content = new String(baos.toByteArray(), "utf-8");
baos.close();
}
} catch (Exception e) {
System.out.println("出错啦");
e.printStackTrace();
}
}
return docxBytes;
return content;
}
2.读取数据
代码如下(示例):
public void downloadFile(HttpServletResponse response, String filePath) {
try {
// 读取.doc文件流
FileInputStream fileInputStream = new FileInputStream(filePath);
HWPFDocument document = new HWPFDocument(fileInputStream);
//转成html字符串
String htmlContent = docToHtml(document);
byte[] bytes = htmlContent.getBytes();
//response的输出流
ServletOutputStream outputStream = response.getOutputStream();
//将html字符串的字节流写入到response中
outputStream.write(bytes);
//关闭流
outputStream.close();
fileInputStream.close();
} catch (IOException e) {
log.error("下载文件失败", e);
}
}
3 前端
//api
export function docPreview(filePath) {
return request({
url: "/docPreview",
method: 'post',
responseType: "blob", //设置响应类型
data: filePath
});
}
//前端调用方法 我这里是因为后台返回的是文件流,所以需要读流,也可以返回String类型直接渲染
docPreview(filePath).then((data) => {
const READER = new FileReader();
READER.addEventListener("loadend", function (e) {
window.localStorage.removeItem("callbackHTML");
window.localStorage.setItem("callbackHTML", e.target.result);
//读取本地保存的html数据,使用新窗口打开
var newWin = window.open("", "_blank");
newWin.document.write(localStorage.getItem("callbackHTML"));
// 关闭输出流
newWin.document.close();
});
READER.readAsText(data);
});
总结
记录一下对doc文件预览的方法,前端不是很精通只能想到打开新窗口的方式去预览。