最近项目中遇到一个需求,需要在手机端实现对pc端上传的附件进行在线预览,整理了一下实现方案,仅供参考

首先是最常见的我word在线预览,这里使用的是com.aspose.words这个jar包(其他格式的也可以用这种方式,需要引用对应格式的jar包,没有找到免费的,所以换了别的方式实现)

实现方式

1.引入jar包
  <dependency><groupId>com.aspose</groupId><artifactId>words</artifactId><version>14.9.0</version><classifier>jdk16</classifier></dependency>
2.后台实现
//获取文件实体
FileVO fileVO = this.facade.getFileService().loadObject(objectId, FileVO.class);
//判断是否为word文档if (fileVO.getFileName().indexOf(".doc") > -1 || fileVO.getFileName().indexOf(".docx") > -1) {//自定义文件夹String floder = LawConfig.officeHtml + subFolder(objectId);//自定义html文件路径String htmlPath = floder + objectId + "_" + fileVO.getVersion() + ".html";//判断是否已经生成过html文件if (new File(htmlPath).exists()) {System.out.println("文件已存在");} else {System.out.println("新文件");//本项目附件有单独的服务器,这里获取的是附件服务器的连接URL url = new URL(Global.imgServer + "?file=" + fileVO.getFilePath() + "&name="+ URLEncoder.encode(fileVO.getFileName(), "utf-8"));HttpURLConnection conn = (HttpURLConnection) url.openConnection();if (!new File(floder).exists()) {new File(floder).mkdirs();}DataInputStream input = new DataInputStream(conn.getInputStream());String filename = fileVO.getFileName();Boolean flag = request.getHeader("User-Agent").indexOf("like Gecko") > 0;if (request.getHeader("User-Agent").toLowerCase().indexOf("msie") > 0 || flag) {filename = URLEncoder.encode(filename, "UTF-8");// IE浏览器} else {// 先去掉文件名称中的空格,然后转换编码格式为utf-8,保证不出现乱码,// 这个文件名称用于浏览器的下载框中自动显示的文件名filename = new String(filename.replaceAll(" ", "").getBytes("UTF-8"), "ISO8859-1");}filename=filename.replaceAll("\\+","%20");response.setContentType(fileVO.getFileType());//response.addHeader("Content-Disposition", "attachment;fileName=" + filename);// 设置文件名byte[] buffer = new byte[1024];BufferedInputStream bis = null;//由于文件可能是加密的,这里选择先下载文件到服务器然后判断是否需要解密File file=new  File(LawConfig.officeHtml+fileVO.getFileName());FileOutputStream out=new FileOutputStream(file,true);try {bis = new BufferedInputStream(input);//OutputStream os = response.getOutputStream();int i = bis.read(buffer);while (i != -1) {out.write(buffer, 0, i);i = bis.read(buffer);}} catch (Exception e) {e.printStackTrace();} finally {if (bis != null) {try {bis.close();} catch (IOException e) {e.printStackTrace();}}if (input != null) {try {input.close();} catch (IOException e) {e.printStackTrace();}}out.close();}//本项目中上传的附件可能会加密,这里判断是否需要解密System.setProperty("user.dir", "加密接口对应路径");InteKey mInteKey = new InteKey();System.out.println("开始验证,文件是:"+LawConfig.officeHtml + fileVO.getFileName());int ia = mInteKey.Ia(LawConfig.officeHtml + fileVO.getFileName());System.out.println("验证结果:" + ia);if (ia == 0) {     // 加密文件,需要做解密处理System.out.println("开始解密");int da = mInteKey.Da(LawConfig.officeHtml + fileVO.getFileName(), LawConfig.officeHtml + fileVO.getFileName());System.out.println("解密结果:" + da);}//这一步是关键,指定保存成HTML类型文件,这里的文件路径可以是真实路径,也可以是流new Document(LawConfig.officeHtml + fileVO.getFileName()).save(htmlPath, SaveFormat.HTML);}//将生成的html文件以流的形式写回浏览器FileInputStream fis = null;OutputStream os = null;fis = new FileInputStream(htmlPath);os = response.getOutputStream();int count = 0;byte[] buffer = new byte[1024 * 8];while ((count = fis.read(buffer)) != -1) {os.write(buffer, 0, count);os.flush();}fis.close();os.close();
3.前台引用

function readOnline(){$('.details_centent').on('click','.download',function(){var objectId=$(this).attr('data-objectId');var fName = $(this).text();var fileSuffix = fName.substr(fName.lastIndexOf('.'));function isInformatData(arr, val ) {val = val.toLowerCase();return arr.indexOf(val)!=-1;}; if(isInformatData(['.doc', '.docx','.xls','.xlsx'], fileSuffix)){//doc,docxvar url='/law/common/file/downloadOrDetail.htm?objectId='+objectId;window.open(url);}else if(isInformatData(['.pdf'], fileSuffix)){//pdfwindow.open ("/page/common/mobilePhone/contract/pdf.jsp?fileId="+objectId);}else if(isInformatData(['.txt'], fileSuffix)){//txtwindow.open ("/page/common/mobilePhone/model/txt.jsp?objectId="+objectId);}else if(isInformatData(['.jpg','.png','.bmp','.jpeg','.gif'], fileSuffix)){//图片window.open ("/page/common/mobilePhone/model/img.jsp?objectId="+objectId);}else{//其他alert("该类型不支持预览,请于电脑端查看!");}})}

web项目移动端在线预览(word格式转html)相关推荐

  1. 调用office web 365接口实现在线预览word文档,PDF,PPT

    我项目中是直接用iframe显示: <iframe id="iframe_src"  scrolling="auto"  width="100% ...

  2. pdfh5.js 基于pdf.js和jQuery,web/h5/移动端PDF预览手势缩放插件。

    pdfh5.js 基于pdf.js和jQuery,web/h5/移动端PDF预览手势缩放插件. 注意:本地绝对路径地址不能加载,跨域问题用代理或者服务端解决. svg模式渲染存在缺陷,只能渲染普通pd ...

  3. 手机端与PC端在线预览PDF

    问题场景 公司需要把出具的报告在PC端在线预览,之前一直用embed标签解决,效果很好.产品提出在手机端(微信链接或者手机浏览器)也要在线预览,尴尬的是安卓端提示的是下载,IOS端只能看不能滑动.经一 ...

  4. 在线预览word文档

    在线预览word文档 在线预览word文档 Java 使用wps将word文件转换pdf文件 转成PDF 在线预览word(转成pdf)前端展示 欢迎来提更好的意见 在线预览word文档 昨天下午组长 ...

  5. Springboot 超简单实现在线预览,Word文档 doc、xlsx、pdf、txt等

    前言 PDF.TXT 只要资源可访问,根本就不需要进行任何处理,直接访问查看就完事了. 也是因为这个PDF可以直接查看(现在浏览器基本支持了),那么我们实现Word文档在线预览,其实也是 把WORD文 ...

  6. (开源kkFileView、kkOffice)在线预览word、pdf、ofd、excel、ppt、压缩包、图片等等

    (开源kkFileView.kkOffice)在线预览word.pdf.ofd.excel.ppt.压缩包.图片等 前言 此项目为文件文档在线预览项目解决方案,对标业内付费产品有[永中office][ ...

  7. office在线预览 word在线预览 .net在线预览 文件在线浏览接口

    office在线预览 word在线预览 excel在线预览 文件浏览接口服务 支持移动端浏览,只要能使用浏览器上网都可以使用,不需要安装任何第三方工具. 1.word在线预览 excel在线预览,.n ...

  8. web使用openoffice实现在线预览office文档

    最近搞web项目,使用框架struts+spring+jpa实现,做到项目里面一个在线预览功能,试过无数的方法,最后得到了一个非常使用的方法,这方法也是我看过多篇博客的出来的,仅限参考. 效果图如下: ...

  9. mvc直接在html页面预览pdf,Asp.net MVC 实现在线预览word、excel、ppt、pdf文件

    在线预览word.excel.ppt 原理:主要是引用第三方Dll使本地word.excel.ppt文件转换成Html 需要引用 : Aspose.Cells.dll Aspose.Slides.dl ...

最新文章

  1. 不用写语句的轻量级orm_为什么说sqltoy-orm远比mybatis强大
  2. dp application in paper (c#)
  3. iOS开发之检查更新
  4. android和linux操作系统的区别
  5. 名图1.8智能隐藏功能_7年后再度回归 全新一代名图“大”不同_搜狐汽车
  6. python模拟浏览器请求的库_基于Python模拟浏览器发送http请求
  7. 文本挖掘之 文本相似度判定
  8. 使用xmodmap修改键盘映射
  9. 双系统格式化Ubuntu分区后Windows 开机出现grub rescue问题
  10. 统计学 贾俊平 笔记
  11. 【Linux】之 从源代码安装软件(HelloWorld)
  12. 不卡顿成用户购机第一要素,Mate 9深得人心
  13. 树莓派配置迅雷离线下载器
  14. 腾讯bugly升级注意事项
  15. 操作性条件作用和经典性条件作用中,刺激分化和泛化的区别是?|小白心理-312/347考研答疑
  16. [javascript]替换所有带/的字符串
  17. boost哪些库需要编译
  18. shell之cut命令
  19. Android支付接入(七):Google In-app-Billing
  20. databinding+viewmodel mvvm初学者踩坑

热门文章

  1. moses 编译_傻瓜式MOSES安装
  2. 风控概要和内容安全,反欺诈(营销风控)
  3. python可以做数据库功能吗_python可以用哪些数据库
  4. Java解P2678 [NOIP2015 提高组] 跳石头,有图有注释,通俗易懂
  5. L298N驱动直流电机转动
  6. 程序员到底有没有必要买一台阿里云服务器?
  7. 电脑如何恢复数据?电脑文件清空了如何恢复数据?
  8. 手机连接电脑后,电脑上显示不出来手机的文件夹
  9. python交互式程序设计导论答案-《程序员编程语言经典合集(计算机科学丛书5册套装)》epub+mobi+azw3...
  10. Sign in With Apple (苹果授权登录)