1、Java+FlexPaper+swfTools 仿百度文库文档在线预览系统设计与实现笔者最近在给客户开发文档管理系统时,客户要求上传到管理系统的文档(包括ppt,word ,excel,txt)只能预览不允许下载。笔者想到了 百度文库和豆丁网,百度文库和豆丁网的在线预览都是利用 flash 来播放文档的,在网上查阅了大量资料,终于实现了该项功能,现将自己的设计和实现整理如 下。一、如何将文档转成 flash 支持的 swf 文件实现在线播放?1.先用 openOffice 把 ppt、word、excel、txt 类型的文档转换成 pdf2.用 swftools 将 pdf 转换成 swf,然
2、后利用 FlexPaper 插件实现在线播放预览。二、具体实现1.安装必备工具组件(1)安装 openoffice,openoffice 是开源免费的文字处理软件,它可以将 office 文档转成 pdf 文件(笔者安装到 D:Program Files),openOffice 下载地址http:/www.openoffice.org/download/index.html(2)安装完 openoffice 后必须启动其 server,以命令行方式启动 openoffice server。进入 cmd命令行提示符 D:Program FilesOpenOffice.org 3program键入
3、如下命令:soffice -headless -accept=“socket,host=127.0.0.1,port=8100;urp;“ nofirststartwizard进入 windows 任务管理器查看有个进程 soffice.bin,说明 openoffice 启动成功!(3)安装 swfTools(安装到 D:Program Files)swftools 作用是将 pdf 转换为 swf 文件以便flexpaper 播放。下载地址:http:/www.swftools.org/download.html(4)下载 flexpaper,下载地址: http:/ FlexPaper_
4、1.5.1,下载后将其解压备用。(5)下载 OpenDocument 文档转换器 JODConverter,JODConverter 是一个 java 的OpenDucument 文件转换器,可以进行许多文件格式的转换,它利用 OpenOffice 来进行转换工作,它能进行以下的转换工作:a.Microsoft Office 格式转换为 OpenDucument,以及 OpenDucument 转换为 Microsoft Officeb.OpenDucument 转换为 PDF,Word、Excel、PowerPoint 转换为 PDF,RTF 转换为 PDF 等。下载地址:http:/ jo
5、dconverter-2.2.2.jar 包2.软件开发过程(1)启动 Eclipse,新建 web 项目名称为 ctcesims(2)将上面第4步解压的 flexpaper 文件中的 js 文件夹(包含了 flexpaper_flash_debug.js,flexpaper_flash.js,jquery.js,这三个 js 文件主要是预览 swf 文件的 插件)拷贝至网站根目录;将 FlexPaperViewer.swf 拷贝至网站根目录下 (该文件主要是用在网页中播放 swf 文件的播放器),目录结构如下 图(3)创建 documentUpload.jsp 文件htmlview plai
6、ncopy 文档在线预览系统 body margin-top:100px;background:#fff;font-family: Verdana, Tahoma; a color:#CE4614; #msg-box color: #CE4614; font-size:0.9em;text-align:center; #msg-box .logo border-bottom:5px solid #ECE5D9;margin-bottom:20px;padding-bottom:10px; #msg-box .title font-size:1.4em;font-weight:bold;marg
7、in:0 0 30px 0; #msg-box .nav margin-top:20px; 请上传要处理的文件,过程可能需要几分钟,请稍候片刻。 (4)创建文档转换类 DocConverter.javajavaview plaincopy package com.cectsims.util; import java.io.BufferedInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import com.artofsolving.jodconverter.Doc
8、umentConverter; import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection; import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection; import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter; /* * doc docx 格式转换 */ public
9、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
10、 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 = ne
11、w 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(8100); try connection.co
12、nnect(); DocumentConverter converter = new OpenOfficeDocumentConverter(connection); converter.convert(docFile, pdfFile); / close the connection connection.disconnect(); System.out.println(“*pdf 转换成功,PDF 输出:“ + pdfFile.getPath()+ “*“); catch (.ConnectException e) e.printStackTrace(); System.out.print
13、ln(“*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,不需要再进行转化*“);
14、 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(“D:/Program Files/SWFToo
15、ls/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.exi
16、sts() 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.getErrorStre
17、am(); 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 IOExc
18、eption 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 转换器开始工作,该文件
19、已经转换为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; /* * 返回文件路
20、径 * * 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(“) St
21、ring 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“); (5)创建文档上传转换处理文件 docUploadConvertAction.jsp 文件htmlview p
22、laincopy “); % Insert title here body margin-top:100px;background:#fff;font-family: Verdana, Tahoma; a color:#CE4614; #msg-box color: #CE4614; font-size:0.9em;text-align:center; #msg-box .logo border-bottom:5px solid #ECE5D9;margin-bottom:20px;padding-bottom:10px; #msg-box .title font-size:1.4em;fon
23、t-weight:bold;margin:0 0 30px 0; #msg-box .nav margin-top:20px; (6)创建文档预览文件 documentView.jsphtmlview plaincopy html, body height:100%; body margin:0; padding:0; overflow:auto; #flashContent display:none; 文档在线预览系统 var fp = new FlexPaperViewer( FlexPaperViewer, viewerPlaceHolder, config : SwfFile : es
24、cape(), Scale : 0.6, ZoomTransition : easeOut, ZoomTime : 0.5, ZoomInterval : 0.2, FitPageOnLoad : true, FitWidthOnLoad : false, FullScreenAsMaxWindow : false, ProgressiveLoading : false, MinZoomSize : 0.2, MaxZoomSize : 5, SearchMatchAll : false, InitViewMode : SinglePage, ViewModeToolsVisible : tr
25、ue, ZoomToolsVisible : true, NavToolsVisible : true, CursorToolsVisible : true, SearchToolsVisible : true, localeChain: en_US ); 7.启动 tomcat,部署当前 web 应用8.地址栏输入 http:/localhost:8080/ctcesims/documentUpload.jsp 如下图: 单击选择文件,选择您要上传的文档,笔者选择的是 lunwen.doc 文件,然后单击上传,处理完成后,打印如下信息,如下图所示:单击预览按钮,就会生成预览界面,如下图:9.若出现 swf 无法预览,请访问http:/ swf 的文件夹设置为信任文件位置。由于时间仓促,以上只是简单示例,很多情况还需要进一步考虑!参考资料:http:/