Java poi 配合 js 预览doc文件

前言

自己找了很多相关博客对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文件预览的方法,前端不是很精通只能想到打开新窗口的方式去预览。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值