java实现Word文档(doc、docx)在线查看功能(前台+后台)

本文档介绍了如何使用Java将Word转换为PDF,并利用JavaScript实现在线预览功能。通过Aspose库进行Word到PDF的转换,然后通过响应式HTTP服务将PDF传递给前端,使用PDFObject库在浏览器中嵌入并显示PDF,从而达到在线查看Word文档的目的。同时,文章提及了在Linux环境下可能遇到的中文乱码问题及其解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

功能需求要求实现文档上传、下载、查看、删除,查看没弄过,别的就不提了,以下是我记录的实现文档在线查看的方法以及效果图

实现思路

首先把word文件转为pdf,然后在用js查看pdf文件实现在线查看功能。

主要用到的知识点:

1.word转pdf:

导入包 com.aspose.words.Document;

使用其中的

//pathFile是doc路径

Document document = new Document(pathFile.toString());

  //pathDirs+File.separator+wordDate+"_"+netWork+".pdf"是pdf路径

 File outputFile = new File(pathDirs+File.separator+wordDate+"_"+netWork+".pdf");

 //操作文档保存

 document.save(outputFile.getAbsolutePath(), com.aspose.words.SaveFormat.PDF);

2.pdf通过response传给前台请求:(filePath是pdf的路径)

public  void wrightWord(HttpServletRequest request, HttpServletResponse response,String filePath)

            throws ServletException, IOException {

        try {

            //获取所要下载的文件名称

                String fileName=filePath.substring(filePath.lastIndexOf(File.separator)+1,filePath.length());

            String fileNameOld=fileName;

            //根据文件名获取 MIME 类型

            response.setCharacterEncoding("UTF-8");

            //设置ContentType字段值

            response.setContentType("application/pdf");

            if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0) {

                fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1"); // firefox浏览器

            } else if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) {

                fileName = URLEncoder.encode(fileName, "UTF-8");// IE浏览器

            }else if (request.getHeader("User-Agent").toUpperCase().indexOf("CHROME") > 0) {

                fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");// 谷歌

            }

            //下载文件所在目录

            String folder=filePath.replace(fileName,"");

            

            response.addHeader("Content-type", "application/pdf");

            response.addHeader("Content-Disposition", "attachment;filename="+fileName);

            //通知文件流读取文件

            InputStream in  = new FileInputStream(filePath);

//            InputStream in = getServletContext().getResourceAsStream(folder+filename);

            //获取response对象的输出流

            OutputStream out = response.getOutputStream();

            byte[] buffer = new byte[1024];

            int len;

            //循环取出流中的数据

            while((len = in.read(buffer)) != -1){

                out.write(buffer,0,len);

            }

            out.flush();

            in.close();

            out.close();

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

3.前台显示pdf文件,实现在线查看功能:

其中showPDF是pdf容器,前边那个url是对应的请求获取pdf文件。外层那个div是用来展示pdf的,给用户感觉就是点击打开,返回页面关闭,没有放对应的css,主要就是js实现,这个自己灵活写。

关键使用打开pdf文件的代码: PDFObject.embed("/spdbnb/file/showJys?netWork="+netWork+"&wordDate="+wordDate,"#showPDF");

<div id="pdfDiv" style="display: none;height: 100%;width: 100%;z-index: 999">
    <a   href="javascript:void(0);" onclick="rsrpallJYS.back()"style="color: #FFFFFF;">
        <div class="back-top">返回<br>页面</div>
    </a>
    <div id="showPDF" style="height: 100%;width: 100%;z-index: 9999999">
</div>

rsrpallJYS.showWord=function (){
            rsrpallJYS.closeShowDivForTerByJYS()
            $("#pdfDiv").show();
            PDFObject.embed("/spdbnb/file/showJys?netWork="+netWork+"&wordDate="+wordDate,"#showPDF");
        }

收尾:用到的jar包和前台js脚本(貌似只能通过连接的方式,也可以自己找jar包com.aspose.words.Document;js包pdfobject.min.js)

不喜欢找的,我也上传到这里了,点击csdn下载

还有word转pdf的时候可能会出现中文乱码,如果,你在win环境下没事就是linux没有安装对应的字体:

解决方法如下:使用com.aspose.words将word模板转为PDF乱码解决方案(window下正常)_com.aspose.words.unsupportedfileformatexception: u-CSDN博客(别人的,如果不能打开了,就自己搜【使用com.aspose.words将word模板转为PDF乱码解决方案(window下正常)】)

本内容没有完整傻瓜式使用的全部代码,只放了关键代码。如果用到要转载请标明出处。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值