JAVA+FlexPaper+OpenOffice+SWFTools文档预览

Java基于springmvc实现文档预览(openoffice+swftools+flexpaper) Java基于springmvc使用openoffice+swftools+flexpaper实现在线预览 源码下载 1.概述 主要原理 1.通过第三方工具openoffice,将word、excel、ppt、txt等文件转换为pdf文件 2.通过swfTools将pdf文件转换成swf格式的文件 3.通过FlexPaper文档组件在页面上进行展示 阅读详情

1、软件环境:
  • openoffice:启动openoffice服务:soffice.exe -headless -nologo -norestore -accept=socket,host=localhost,port=8100;urp;StarOffice.ServiceManager
  • swftools
2、所需组件:
  • flexpaper : flexpaper_flash_debug.js,flexpaper_flash.js,jquery.js , FlexPaperViewer.swf    
  •  OpenDocument文档转换器 JODConverter, 主要用它的jodconverter-2.2.2.jar包
3、程序源码:
  • java转换器的程序代码:
    package correy.erp.util.converter;
    
    import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    
    import com.artofsolving.jodconverter.DocumentConverter;
    import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
    import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
    import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
    
    
    public class DocConverter {
    
         private static final int environment = 1;// 环境 1:windows 2:linux
         private String fileString;// (只涉及pdf2swf路径问题)
         private String outputPath = "";// 输入路径 ,如果不设置就输出在默认的位置
         private String fileName;
         private File pdfFile;
         private File swfFile;
         private File docFile;
        
         public DocConverter(String fileString) {
              ini(fileString);
         }
    
         /**
         * 重新设置file
         *
         * @param fileString
         */
         public void setFile(String fileString) {
              ini(fileString);
         }
    
         /**
         * 初始化
         *
         * @param fileString
         */
         private void ini(String fileString) {
              this.fileString = fileString;
              fileName = fileString.substring(0, fileString.lastIndexOf("."));
              docFile = new File(fileString);
              pdfFile = new File(fileName + ".pdf");
              swfFile = new File(fileName + ".swf");
         }
        
         /**
         * 转为PDF
         *
         * @param file
         */
         private void doc2pdf() throws Exception {
              if (docFile.exists()) {
                   if (!pdfFile.exists()) {
                        OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
                        try {
                             connection.connect();
                             DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
                             converter.convert(docFile, pdfFile);
                             // close the connection
                             connection.disconnect();
                             System.out.println("****pdf转换成功,PDF输出:" + pdfFile.getPath()+ "****");
                        } catch (java.net.ConnectException e) {
                             e.printStackTrace();
                             System.out.println("****swf转换器异常,openoffice服务未启动!****");
                             throw e;
                        } catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {
                             e.printStackTrace();
                             System.out.println("****swf转换器异常,读取转换文件失败****");
                             throw e;
                        } catch (Exception e) {
                             e.printStackTrace();
                             throw e;
                        }
                   } else {
                        System.out.println("****已经转换为pdf,不需要再进行转化****");
                   }
              } else {
                   System.out.println("****swf转换器异常,需要转换的文档不存在,无法转换****");
              }
         }
        
         /**
         * 转换成 swf
         */
         @SuppressWarnings("unused")
         private void pdf2swf() throws Exception {
              Runtime r = Runtime.getRuntime();
              if (!swfFile.exists()) {
                   if (pdfFile.exists()) {
                        if (environment == 1) {// windows环境处理
                             try {
                                  Process p = r.exec("E:/Program Files/SWFTools/pdf2swf.exe "+ pdfFile.getPath() + " -o "+ swfFile.getPath() + " -T 9");
                                  System.out.print(loadStream(p.getInputStream()));
                                  System.err.print(loadStream(p.getErrorStream()));
                                  System.out.print(loadStream(p.getInputStream()));
                                  System.err.println("****swf转换成功,文件输出:"
                                            + swfFile.getPath() + "****");
                                  if (pdfFile.exists()) {
                                       pdfFile.delete();
                                  }
    
                             } catch (IOException e) {
                                  e.printStackTrace();
                                  throw e;
                             }
                        } else if (environment == 2) {// linux环境处理
                             try {
                                  Process p = r.exec("pdf2swf " + pdfFile.getPath()
                                            + " -o " + swfFile.getPath() + " -T 9");
                                  System.out.print(loadStream(p.getInputStream()));
                                  System.err.print(loadStream(p.getErrorStream()));
                                  System.err.println("****swf转换成功,文件输出:"
                                            + swfFile.getPath() + "****");
                                  if (pdfFile.exists()) {
                                       pdfFile.delete();
                                  }
                             } catch (Exception e) {
                                  e.printStackTrace();
                                  throw e;
                             }
                        }
                   } else {
                        System.out.println("****pdf不存在,无法转换****");
                   }
              } else {
                   System.out.println("****swf已经存在不需要转换****");
              }
         }
    
         static String loadStream(InputStream in) throws IOException {
    
              int ptr = 0;
              in = new BufferedInputStream(in);
              StringBuffer buffer = new StringBuffer();
    
              while ((ptr = in.read()) != -1) {
                   buffer.append((char) ptr);
              }
    
              return buffer.toString();
         }
         /**
         * 转换主方法
         */
         @SuppressWarnings("unused")
         public boolean conver() {
    
              if (swfFile.exists()) {
                   System.out.println("****swf转换器开始工作,该文件已经转换为swf****");
                   return true;
              }
    
              if (environment == 1) {
                   System.out.println("****swf转换器开始工作,当前设置运行环境windows****");
              } else {
                   System.out.println("****swf转换器开始工作,当前设置运行环境linux****");
              }
              try {
                   doc2pdf();
                   pdf2swf();
              } catch (Exception e) {
                   e.printStackTrace();
                   return false;
              }
    
              if (swfFile.exists()) {
                   return true;
              } else {
                   return false;
              }
         }
    
         /**
         * 返回文件路径
         *
         * @param s
         */
         public String getswfPath() {
              if (swfFile.exists()) {
                   String tempString = swfFile.getPath();
                   tempString = tempString.replaceAll("\\\\", "/");
                   return tempString;
              } else {
                   return "";
              }
    
         }
         /**
         * 设置输出路径
         */
         public void setOutputPath(String outputPath) {
              this.outputPath = outputPath;
              if (!outputPath.equals("")) {
                   String realName = fileName.substring(fileName.lastIndexOf("/"),
                             fileName.lastIndexOf("."));
                   if (outputPath.charAt(outputPath.length()) == '/') {
                        swfFile = new File(outputPath + realName + ".swf");
                   } else {
                        swfFile = new File(outputPath + realName + ".swf");
                   }
              }
         }
    }

  • java文件上传并调用转换器对文件进行转换:
    String newFileName = null;
                       // 得到当前时间自1970年1月1日0时0秒开始流失的毫秒数,将这个毫秒数作为上传文件的文件名
                       long now = new Date().getTime();
                      
                       // 得到保存上传文件的目录的真实路径
                       String path = ServletActionContext.getServletContext().getRealPath(uploadDir);
                      path=path.replace( '\\', '/' );
                      newFileName=now+agreement.getAgreeNum()+fileName.substring(fileName.lastIndexOf( "."));
                      
                      File dir = new File(path);
                      dir.mkdirs();
                      
                       //删除旧文件
                       if(!Stringer.isNullOrEmpty(agreement.getPath()))
                      {
                            File oldFile= new File(dir,agreement.getPath().substring(agreement.getPath().lastIndexOf("/")+1));
                             if(oldFile.exists())
                            {
                                  oldFile.delete();
                            }
                      }
                      
                      
                      agreement.setPath(uploadDir+ "/"+newFileName);
                       agreementDao.saveAgreement(agreement);
                      
                      BufferedOutputStream bos = null;
                      BufferedInputStream bis = null;
                       // 将上传的文件保存在本地目录
                      File newFile = null;
                       try {
                            FileInputStream fis = new FileInputStream(file);
                            bis = new BufferedInputStream(fis);
    
                            newFile = new File(dir, newFileName);
                            FileOutputStream fos = new FileOutputStream(newFile);
                            bos = new BufferedOutputStream(fos);
    
                             byte[] buf = new byte[1024];
                             int len = -1;
                             while ((len = bis.read(buf)) != -1) {
                                  bos.write(buf, 0, len);
                            }
                            
                      } catch (Exception e) {
                            e.printStackTrace();
                             throw new Exception();
                      } finally {
                             try {
                                   if (null != bis)
                                        bis.close();
                                   if (null != bos)
                                        bos.close();
                            } catch (Exception e) {
                                  e.printStackTrace();
                                   throw new Exception();
                            }
                      }
                      DocConverter d = new DocConverter(converfilename); 
                d.conver();
    

  • JSP页面预览文档
    <%@ page language= "java" contentType ="text/html; charset=UTF-8"
        pageEncoding="UTF-8" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript" src=" ${ctx}/js/flexpaper/jquery.js"></ script>
    <script type="text/javascript" src=" ${ctx}/js/flexpaper/flexpaper_flash.js"></script >
    <script type="text/javascript" src=" ${ctx}/js/flexpaper/flexpaper_flash_debug.js"></script >
    <style type="text/css" media="screen">
                       html, body   { height:100%; }
                       body { margin :0; padding:0; overflow:auto ; }  
                       #flashContent { display :none; }
            </style>
    
    <title> 文档在线预览系统 </title>
    </head>
    <body>
            <div style="text-align : center;width: 100%;" id= "c">
                  <a id= "viewerPlaceHolder" style="width :820px;height:650px;" ></a>
                 
                  <script type="text/javascript" >
                       var width=$("#c" ).width();
                       var height=$(document).height();
                      $( "#viewerPlaceHolder").css("width" ,width*0.94+"px");
                      $( "#viewerPlaceHolder").css("height" ,height+"px");
                        $("#viewerPlaceHolder" ).css("padding-left",width*0.03+ "px");
                      $( "#viewerPlaceHolder").css("display" ,"block" );
                       var name="${param.name}" ;
                      name=name.substring(0, name.lastIndexOf( "."))+".swf" ;
                             var fp = new FlexPaperViewer( 
                                         '${ctx}/swf/FlexPaperViewer',
                                         'viewerPlaceHolder', { config : {
                                         SwfFile : escape( '${ctx}/'+name),
                                         Scale : 1.0,
                                         ZoomTransition : 'easeOut',
                                         ZoomTime : 0.5,
                                         ZoomInterval : 0.2,
                                         FitPageOnLoad : false,
                                         FitWidthOnLoad : false,
                                         FullScreenAsMaxWindow : false,
                                         ProgressiveLoading : false,
                                         MinZoomSize : 0.2,
                                         MaxZoomSize : 5,
                                         SearchMatchAll : false,
                                         InitViewMode : 'SinglePage',
                                        
                                         ViewModeToolsVisible : true,
                                         ZoomToolsVisible : true,
                                         NavToolsVisible : true,
                                         CursorToolsVisible : true,
                                         SearchToolsVisible : true,
                                        
                                         localeChain: 'zh_CN'
                                         }});
                  </script>            
            </div>
    </body>
    </html>
    

4、运行效果:



java实现在线预览doc,txt,ppt,xls等文档,用到了libreOffice,openOffice,SWFTools,JODConverter,flexPaper 涉及到的图片请看附件。 libreOffice转换的代码: public String doDocToFdpLibre() { // File inputFile = new File("d:/1.txt"); // File inputFile = new File("d:/ppt.ppt"); // File inputFile = new File("d:/pptx.pptx" 阅读详情

相关推荐

Java在线文档预览系统仿百度文库实战项目

在本章节,我们将介绍一个令人兴奋的项目——Java在线预览仿百度文库源代码。这个项目的目标是创建一个可以在线预览文档的平台,类似于互联网上广受欢迎的百度文库,这使得用户可以在不下载的情况下预览文档。我们将在后续章节中详细探讨相关的技术实现。swfTools是一套开源的命令行工具,专门用于操作和生成Flash文件(swf文件),它是处理swf文件不可或缺的工具。swfTools的主要功能包括转换多种文档格式到swf格式,提取swf文件中的资源,以及将swf文件分解为源代码。

weixin_36047538的博客 409

java flexpaper_java web word文件 pdf文件在线预览源码(flexpaper)

【实例简介】java web word文件 pdf文件在线预览源码 经过测试【实例截图】【核心代码】BrowsenOnlinehtml, body{ height:100%; }body { margin:0; padding:0; overflow:auto; }var basePath = "http://localhost:8080/";var fpv = new FlexPaperView...

weixin_35160622的博客 251

FlexPaper插件

FlexPaper 用于swf 文件的预览,其中包含js插件和swf播放器。swf播放器经过修改,已经去掉logo,而且支持禁用打印功能。

java flexpaper_Java+FlexPaper+swfTools 文档在线预览demo

1.概述主要原理1.通过第三方工具openoffice,将word、excel、ppt、txt等文件转换为pdf文件2.通过swfTools将pdf文件转换成swf格式的文件3.通过FlexPaper文档组件在页面上进行展示2.安装包下载1.openoffice是Apache下的一个开放免费的文字处理软件2.SWFTools是一组用来处理Flash的swf文件的工具包,我们使用它将pdf文件转成s...

weixin_30216669的博客 296

swfTools使用说明

swfTools使用说明

手把手教你用Matlab实现Copula函数:从理论到代码的完整指南

本文提供了一份在MATLAB中实现Copula函数的完整实战指南。从理解Sklar定理和常见Copula家族(如高斯、t、Clayton)的核心概念出发,详细讲解了数据预处理、边缘分布拟合、概率积分变换等关键步骤。通过具体代码示例,手把手演示了Copula模型的拟合、评估、蒙特卡洛模拟,并进阶应用于金融风险度量(如VaR计算)和条件分布分析,帮助读者掌握这一强大的多变量依赖建模工具。

y9z0a1b的博客 953

java 访问 网络swf_Java实现文档在线预览(openoffice+swfTools+FlexPaper)

文档在线预览在项目中早就完成了,后来又经过了一次优化。但是一直都没时间去记录遇到的问题,文档在线预览的详细步骤可以参考http://blog.youkuaiyun.com/u013614451/article/details/24261503,感谢博主写了这么好的文章帮助我完成了项目中主要的模块。下面是文档转换的工具类DocConvert.java,并标注出我修改的部分。packagecom.he.util;...

weixin_30972131的博客 226

ROS水下仿真神器uuv_simulator:从零搭建水下机器人测试环境(附Gazebo配置全流程)

本文详细介绍了如何使用ROS和uuv_simulator从零搭建水下机器人仿真测试环境,包括Gazebo配置全流程。通过系统环境准备、水下世界构建、机器人模型定制开发等步骤,帮助开发者在虚拟海洋环境中高效验证算法,大幅降低研发成本。特别适合水下机器人研发人员参考。

ujm5678901的博客 263

Java实现文档在线预览(openoffice+swfTools+FlexPaper)

需求:用户上传附件后,点击查看,可以在页面直接查看到附件内容,样式排版需要和附件文档里一致。另外可以查看附件信息,下载附件。  附件格式 为 excle word 文档,pdf 扫描件 分析:一个附件管理的功能 + 在线查看功能。 附件管理的功能好实现,略过。 在线查看,是通过一个播放器查看flash文件,网上例子很多。 flash播放器 搜索

xjh1991xjh的专栏 4228

SAP 深入理解库存

当 某个期间发生货物移动的时候,系统在更新mard数据的之前(这个表是实时更新的),会检查此笔业务过账期间和mard里对应记录的期间是否一致,也就是 看这是不是本期间第一笔移动。其二,我们不能保证这定期的作业的确定性,硬件维护、系统性能都可能使这预定的“时刻”出现偏差,如果某一次没能抓取该时刻的库存,对整个报表来说,是数据的缺失;中,当某一期间(Period)的第一项操作更新了物料数量或价值,则SAP系统会向表MBEWH写入一条记录,以第一项操作之前的余额、价格控制为标准。这对于业务数据量大的。

matinal 當冬夜漸暖 。公众号:matinal 3624

用Matlab搞定Delta并联机器人运动学:从建模到正逆解完整流程(附代码)

本文详细介绍了如何使用Matlab实现Delta并联机器人的运动学分析,包括几何建模、正逆解求解及可视化验证。通过符号计算和数值迭代法,解决非线性方程组问题,并提供完整代码示例,帮助读者快速掌握Delta机器人的运动学计算与应用。

weixin_30955617的博客 177

java flexpaper_JAVA+FlexPaper+OpenOffice+SWFTools文档预览

packagecorrey.erp.util.converter;importjava.io.BufferedInputStream;importjava.io.File;importjava.io.IOException;importjava.io.InputStream;importcom.artofsolving.jodconverter.DocumentConverter;im...

weixin_39988677的博客 123

FOFA奇技淫巧:从搜索语法到实战场景的深度挖掘

本文深入探讨了FOFA搜索语法在网络空间测绘中的高效应用,从基础语法到实战场景的深度挖掘。通过渗透测试、威胁狩猎、资产发现等实际案例,展示了如何利用FOFA精准定位目标系统、识别蜜罐、追踪威胁,并提供了合规使用指南和性能优化技巧,助力安全工作者提升效率。

weixin_32428571的博客 238

java对文档等、附件在线预览的详细开发步骤解析:openOffice+SwfTools+FlexPaper

在线预览实现简介: 过程分为:1、使用OpenOffice(JODConverter)将不同类型的附件转换为.pdf后缀的文件(PDF); 2、使用SwfTools将pdf文件转换为swf文件; 3、使用FlexPaper 在jsp中预览swf文件; 注意:此过程不包含文件的上传下载部分代码说明; 本文的系统环境为 windows 由于本人经验有限,如有纰漏和不足 多多批评; 在此过程中遇到的问题以及解决方法,下

蓝斯、的专栏 1万+

物联网与人工智能项目实战:构建智能农业监控系统

智能农业监控系统通过物联网技术实时监测农业环境中的各项数据(如土壤湿度、温度、光照强度等),并利用人工智能技术分析和预测农业生产的趋势,进而为农民提供决策支持。系统的核心目标是提高农作物的产量和品质,同时减少资源浪费,优化农田管理。环境监测:实时采集农田的温湿度、土壤湿度、光照等环境数据。作物生长监测:通过传感器和图像识别技术监测作物的生长状况。智能灌溉系统:根据土壤湿度和气候条件自动调整灌溉量。病虫害检测与预警:利用人工智能和图像识别技术自动检测作物病虫害,并及时给出预警。数据分析与决策支持。

这里只有干货:从 Modbus 通信到 MES 对接,从 YOLO 训练到工控机部署,带你搞定工业软件开发全流程。 709

11、Windows Autopilot 部署与设备注册全攻略

本文详细介绍了Windows Autopilot的完整部署流程,涵盖注册配置文件设置、注册状态页面(ESP)配置、设备注册方法(包括CSV导入、JSON注入、在线注册等)、自动化脚本实现及故障排除技巧。通过手动与PowerShell自动化两种方式指导用户完成配置,并提供流程图、最佳实践和常见问题解答,帮助IT管理员高效实现设备零接触部署与管理。

ii567的博客 343

Java基于springmvc实现文档预览(openoffice+swftools+flexpaper)(排坑记录)

Java基于springmvc实现文档预览(openoffice+swftools+flexpaper)(排坑记录)本文代码来源已在末尾标注,写本文的目的在于记录自己在实践过程中遇到的问题及解决方案需求实现文档在线预览原理1.通过第三方工具openoffice,将word、excel、ppt、txt等文件转换为pdf文件2.通过swfTools将pdf文件转换成swf格式的文件3.通过FlexPa...

weixin_34259559的博客 252

微信小程序 - 实现人脸识别对比+人脸相似度数据,用户从相册选择或拍照上传两张图片进行人脸对比得到人脸相似度及判断是否是同一个人,微信小程序检测人脸相似度(免费纯前端方案+调用第三方API接口方案)

微信小程序实现人脸识别对比,微信小程序人脸对比 人脸对比识别 人脸对比相似度,实现获取手机摄像头权限,调用相机拍照实现人脸识别相似度对比,小程序人脸相似度对比,快速识别两张照片的相似程度(用百分比)图片相似度对比在线测试,图片相似度对比,微信小程序人脸相似度对比分析,计算两张人脸的相似度,从而判断是否同一个人,并给出相似度评分。小程序如何实现人脸相似度比对,小程序纯前端怎么对比两张图片的相似度,计算两幅图像的相似度,图像相似度对比方法,调用百度AI的人脸识别,微信小程序中实现人脸识别认证,实现人脸检测、人脸

🏆 前端领域优质创作者 4864

java实现附件预览(openoffice+swftools+flexpaper)

先附上本人参考的文章,基于的 flexpaper版本 为 1.5,本人由于使用的是 2.1.9 ,故之后说明: 1.概述 主要原理 1.通过第三方工具openoffice,将word、excel、ppt、txt等文件转换为pdf文件 2.通过swfTools将pdf文件转换成swf格式的文件 3.通过FlexPaper文档组件在页面上进行展示 2.安装包下载

z69183787的专栏 7万+

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

上一篇: java.sql.SQLException: 对只读结果集的无效操作
下一篇: JSP自定义标签(三) 多选控件(3)
carroll0911
博客等级 码龄17年 27粉丝 41原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值