doc转swf,主流文档在线查看解决方案--类似百度文档功能
引言:
最近项目中对上传的文件需要在线查看功能(就是不用下载到本地,可以直接在网页里打开的查看),通过几周的研究终于搞定,在此总结下供有同样需求的同仁查询和使用。
原理:
通常的在线查看功能都是使用文档转换工具,把原始文档转换成swf文档,然后通过网页直接展示文档内容。
解决方案:
在前期技术研究的过程中,发现有三种解决方案,他们分别是:
1、使用 FlexPaper + Pdf2swf 组合。
缺点是只能提供pdf转换成swf然后在线查看。要支持其他格式的话,需要先转成pdf,这样的话效率有点低。不过网上这种方式的资料挺多的。
相关资料: www.cnblogs.com/luckyxiaoxuan/archive/2012/06/13/2548510.html
2、使用FlashPaper 把文档转成swf直接显示
这种方式的好处在于支持多种格式的文档转换,支持的文档格式包括(doc、docx、xls、xlsx、pdf、txt, ppt、pptx), ppt、pptx的支持效果不怎么好,转换有些慢,要支持这两种方式的话,有专有的转换工具。
原来准备使用FlashPaper转换成swf文档,然后使用FlexPaper显示的,但是发现使用FlexPaper显示FlashPaper转成的swf存在问题,不能显示swf内容,而且不停的闪。后来想起浏览器可以直接支持swf显示的。使用<object><embed>标签即可。
还有一个好处就是这个工具免费。
缺点就是: 最后版本2.2,发布于2008年5月,此后不再支持;支持winxp, server2003等,不支持win7系统;
3、使用Print2Flash把文档转换成swf直接显示
这个东西比FlashPaper更强大,功能更全面,而且提供各种系统的支持。是一个非常不错的工具。
缺点就是:需要money,有需求的公司可以使用。
相关资料: http://www.blue-pacific.com/print2flash/samples/default.htm
我的方案:
我选用的第二种方案,免费,而且对各种当前流行的文档都支持,同时服务器是window server 2003。下面说下具体的程序吧。
1、FlashPaper的安装
可以在网上下载 FlashPaper2.2绿色版,地址: http://download.csdn.net/detail/walkerjong/4420486
下载安装程序后,可以点击install.bat安装FlashPaper,若出现下面的错误:
flashpaper AddPrinterDriver stage 13: error 126 - 找不到指定的模块
错误原因: 安装操作系统的时候没有开启系统还原功能,FlashPaper需要使用该功能。
解决方案: 把安装文件包里的srclient.dll文件拷贝到c:/windows/system32/目录下。
当然也可以使用java程序自动安装,下面是我的工具类代码:FlashPaperUtil.java
- packageorg.study.isap.common.util;
- importjava.io.BufferedReader;
- importjava.io.File;
- importjava.io.FileNotFoundException;
- importjava.io.IOException;
- importjava.io.InputStream;
- importjava.io.InputStreamReader;
- importorg.apache.log4j.Logger;
- publicclassFlashPaperUtil {
- privatestaticfinalLogger logger = Logger.getLogger(FlashPaperUtil.class);
- // flashPaper的存放位置
- publicstaticfinalString FLASH_PAPER_DIR ="bin/flashPaper2_2";
- // 转换各种格式的文档为swf的命令
- publicstaticfinalString FLASH_PAPER_CMD ="FlashPrinter.exe";
- // 检查swf是否转换完成的最大等待次数
- privatestaticfinalintmaxWaitCount =60;
- // 检查swf是否存在的时间间隔, 单位ms。
- privatestaticfinalintcheckInterval =20000;
- privatestaticfinalFile flashPaperDir = initDir();
- privateFlashPaperUtil(){
- }
- privatestaticFile initDir(){
- File dir = newFile(FileUtil.getWebRootPath(), FLASH_PAPER_DIR);
- logger.debug("flashPaperDir["+dir.getAbsolutePath()+"]");
- returndir;
- }
- /**
- * 卸载flashPaper。
- */
- publicstaticvoiduninstall(){
- String uninstallCmd = "uninstall.bat";
- try{
- String cmd =newFile(flashPaperDir, uninstallCmd).getAbsolutePath();
- Runtime runtime = Runtime.getRuntime();
- Process process = runtime.exec(cmd, null, flashPaperDir);
- try{
- // 读取process输出流,放置buffer过小阻塞
- InputStream infoStream = process.getInputStream();
- newReadThread(infoStream).start();
- InputStream errorStream = process.getErrorStream();
- newReadThread(errorStream).start();
- process.waitFor();
- } catch(InterruptedException e) {
- logger.error(e);
- }finally{
- process.destroy();
- }
- runtime = null;
- logger.info("卸载FlashPaper成功,cmd["+cmd+"]");
- } catch(IOException e) {
- logger.error(e);
- logger.error("卸载FlashPaper时出现错误。");
- }
- }
- /**
- * 安装flashPaper。
- */
- publicstaticvoidinstall(){
- String installCmd = "install.bat";
- if(checkInstallEnvironment()){
- try{
- String cmd =newFile(flashPaperDir, installCmd).getAbsolutePath();
- Runtime runtime = Runtime.getRuntime();
- Process process = runtime.exec(cmd, null, flashPaperDir);
- try{
- // 读取process输出流,放置buffer过小阻塞
- InputStream infoStream = process.getInputStream();
- newReadThread(infoStream).start();
- InputStream errorStream = process.getErrorStream();
- newReadThread(errorStream).start();
- process.waitFor();
- } catch(InterruptedException e) {
- logger.error(e);
- }finally{
- process.destroy();
- }
- runtime = null;
- logger.info("安装FlashPaper成功,cmd["+cmd+"]");
- } catch(IOException e) {
- logger.error(e);
- logger.error("安装FlashPaper时出现错误。");
- }
- }
- }
- 便民养生网站长提供:http://www.yangsheng52.com/
- /**
- * 检查安装flashPaper的环境。
- * @return
- */
- privatestaticbooleancheckInstallEnvironment(){
- String systemRoot = System.getenv("SystemRoot");
- File dest = newFile(systemRoot,"system32/srclient.dll");
- logger.debug("操作系统安装路径["+systemRoot+"]");
- if(!dest.exists()){
- logger.info("系统安装目录下不存在该文件["+dest.getAbsolutePath()+"],程序将自动复制该文件。");
- String srcFile = "srclient.dll";
- try{
- File src = newFile(flashPaperDir, srcFile);
- FileUtil.copyFile(src, dest);
- returntrue;
- } catch(FileNotFoundException e) {
- logger.error("找不到要拷贝的源文件["+srcFile+"]", e);
- } catch(IOException e) {
- logger.error("复制文件出错, 源文件["+srcFile+"], 目标文件["+dest.getAbsolutePath()+"]", e);
- }
- logger.error("您的环境不能自动安装FlashPaPer,需要手动安装,安装程序位置/WEB-INF/classes/"+FLASH_PAPER_DIR);
- returnfalse;
- }else{
- logger.info("系统安装目录下存在该文件["+dest.getAbsolutePath()+"],FlashPaper可以开始安装。");
- returntrue;
- }
- }
- /**
- * 使用flashpaper将文档格式转换生产swf文件。
- * 支持格式: office(word、excel),pdf, txt等主流格式。
- * @param path
- * @return
- */
- publicstaticFile fileToSwf(String input, String output){
- File inputFile = newFile(input);
- File outputFile = newFile(output);
- returnfileToSwf(inputFile, outputFile);
- }
- /**
- * 使用flashpaper将文档格式转换生产swf文件。
- * 支持格式: office(word、excel),pdf, txt等主流格式。
- * @param path
- * @return
- */
- publicstaticFile fileToSwf(File input, File output){
- returnfileToSwf(input, output,true);
- }
- publicstaticFile fileToSwf(File input, File output,booleanwaitFinished){
- if(input ==null|| !input.exists()){
- logger.error("要转换为swf的文件["+input.getAbsolutePath()+"]为空或不存在!");
- returnnull;
- }
- if(output.exists()){
- logger.info("swf["+output+"]已经存在。");
- returnoutput;
- }
- String printerCmd = newFile(flashPaperDir, FLASH_PAPER_CMD).getAbsolutePath();
- String cmd = printerCmd+" "+input.getAbsolutePath()+" -o "+output.getAbsolutePath();
- logger.debug("fileToSwf cmd["+cmd+"]");
- intresultCode =1;
- try{
- Process process = Runtime.getRuntime().exec(cmd, null, flashPaperDir);
- try{
- // 读取process输出流,系统提供的buffer过小不读取会阻塞
- newReadThread(process.getInputStream()).start();
- newReadThread(process.getErrorStream()).start();
- resultCode = process.waitFor();
- if(resultCode ==1){
- logger.error("转换文件过程中出现错误,请检查FlashPaper环境是否安装正确!");
- }
- } catch(InterruptedException e) {
- logger.error(e);
- }finally{
- process.destroy();
- }
- if(resultCode !=1&& waitFinished){
- for(intcount=maxWaitCount; count>=0; count--){
- if(output.exists()){
- logger.info("转换文档为swf成功,swf["+output.getAbsolutePath()+"]");
- break;
- }else{
- try{// 休眠checkInterval秒后检查swf文件是否转换完成
- Thread.sleep(checkInterval);
- } catch(InterruptedException e) {
- logger.error(e);
- }
- }
- }
- }
- } catch(IOException e) {
- logger.error("转换文档为swf失败, path["+input.getAbsolutePath()+"]", e);
- }
- returnoutput;
- }
- privatestaticclassReadThreadextendsThread {
- privateInputStream inputStream;
- publicReadThread(InputStream inputStream){
- this.inputStream = inputStream;
- }
- publicvoidrun(){
- BufferedReader input = null;
- String str = null;
- try{
- input = newBufferedReader(newInputStreamReader(this.inputStream,"GBK"));
- while( (str=input.readLine())!=null){
- logger.debug(str);
- }
- } catch(IOException e) {
- logger.error("读取执行线程信息时出错", e);
- }finally{
- try{
- input.close();
- this.inputStream =null;
- } catch(IOException e) {
- logger.error(e);
- }
- }
- }
- }
- }
另外,为了方便程序部署,我们写了一个自动安装程序。
FlashPaperInstallService.java
- packageorg.study.isap.common.service;
- importorg.springframework.stereotype.Service;
- importcom.cosbulk.isap.common.util.FlashPaperUtil;
- @Service
- publicclassFlashPaperInstallService {
- publicFlashPaperInstallService(){
- Thread thread = newInstallThread();
- thread.start();
- }
- privatestaticclassInstallThreadextendsThread {
- publicInstallThread(){
- setName("[Install FlashPaper Thread]");
- setDaemon(true);
- }
- publicvoidrun(){
- FlashPaperUtil.uninstall();
- FlashPaperUtil.install();
- }
- }
- }
FileUtil.java 文件如下:
- packageorg.study.isap.common.util;
- importjava.io.BufferedInputStream;
- importjava.io.BufferedOutputStream;
- importjava.io.File;
- importjava.io.FileInputStream;
- importjava.io.FileOutputStream;
- importjava.io.IOException;
- importjava.io.InputStream;
- importjava.io.OutputStream;
- importjava.io.UnsupportedEncodingException;
- importjava.net.URLDecoder;
- importjavax.servlet.http.HttpServletRequest;
- importorg.apache.log4j.Logger;
- importorg.springframework.util.StringUtils;
- publicclassFileUtil {
- privatestaticfinalLogger logger = Logger.getLogger(FileUtil.class);
- // web工程的文件系统根目录 (/**/isap)
- privatestaticfinalString webRootPath;
- // web工程的URL根目录 (http://***.**/isap)
- privatestaticString webRootUrl;
- static{
- webRootPath = initWebFileBase();
- webRootUrl = null;
- }
- privateFileUtil(){
- }
- privatestaticString initWebFileBase(){
- String fileBase = null;
- String path = FlashPaperUtil.class.getClassLoader().getResource("").getFile();
- // 获取的文件路径为url地址编码,需要处理空格问题
- String classPath = path;
- try{
- classPath = URLDecoder.decode(path, "UTF-8");
- } catch(UnsupportedEncodingException e) {
- logger.error(e);
- }
- intindex = classPath.lastIndexOf("/WEB-INF/classes");
- if(index != -1){
- fileBase = classPath.substring(0, index);
- logger.debug("获取isap文件系统根路径成功, webFileBase["+fileBase+"]");
- }else{
- index = classPath.lastIndexOf("/build/classes");
- if(index != -1){
- fileBase = classPath.substring(0, index)+"/WebRoot";
- }else{
- fileBase="";
- logger.error("获取isap文件系统根路径错误。path["+classPath+"]");
- }
- }
- returnfileBase;
- }
- //------------------ 路径相关 -----------------
- /**
- * 返回web工程的文件系统的绝对路径。
- * @return
- */
- publicstaticString getWebRootPath(){
- returnwebRootPath;
- }
- /**
- * 把相对webFileBase的path转换成url形式的字符串。
- * 例如:<br/>
- * 输入: /uploadFiles/project/file/test.doc <br/>
- * 输出:http://127.0.0.1:8080/isap/uploadFiles/project/file/test.doc <br/>
- * @param request
- * @param path
- * @return
- */
- publicstaticString getDocUrl(HttpServletRequest request, String path){
- if(!StringUtils.hasLength(webRootUrl)){
- webRootUrl = request.getScheme()+"://"+request.getLocalAddr()+":"+request.getLocalPort()+request.getContextPath();
- }
- if(StringUtils.hasLength(path)){
- if(path.startsWith("http://")){
- returnpath;
- }
- path = path.replace('\\', '/');
- }else{
- returnwebRootUrl;
- }
- if(!path.startsWith("/")){
- returnwebRootUrl+"/"+path;
- }
- returnwebRootUrl+path;
- }
- // ----------------------- 文件名和文件类型相关 ----------------
- /**
- * 获取文件名称。
- */
- publicstaticString getFileName(String docUrl){
- intindex = docUrl.lastIndexOf("/");
- if(index != -1){
- returndocUrl.substring(index+1);
- }else{
- returndocUrl;
- }
- }
- /**
- * 获取文件类型, 返回带点号的文件类型。例如“.doc”
- *
- * @param docUrl
- * @return
- */
- publicstaticString getFileType(String docUrl){
- intindex = docUrl.lastIndexOf(".");
- if(index != -1){
- returndocUrl.substring(index);
- }else{
- returndocUrl;
- }
- }
- /**
- * 获得输入路径下,同名文件的swf文件。 <br />
- *
- * @param path
- * @param fileType
- * @return
- */
- publicstaticString getSwfFile(String path){
- String swfPath = null;
- if(StringUtils.hasLength(path)){
- intindex = path.lastIndexOf(".");
- if(index != -1){
- swfPath = path.substring(0, index)+".swf";
- }
- }
- returnswfPath;
- }
- publicstaticFile getSwfFile(File file) {
- String swfpath = getSwfFile(file.getAbsolutePath());
- returnnewFile(swfpath);
- }
- // ---------- 复制文件 ---------
- publicstaticvoidcopyFile(File src, File dest)throwsIOException{
- File dir = dest.getParentFile();
- if(!dir.exists()){
- dir.mkdirs();
- }
- BufferedInputStream input = null;
- BufferedOutputStream output = null;
- try{
- input = newBufferedInputStream(newFileInputStream(src));
- output = newBufferedOutputStream(newFileOutputStream(dest));
- intlength =0;
- byte[] buffer =newbyte[4*1024];
- while((length = input.read(buffer)) >=0){
- output.write(buffer, 0, length);
- }
- }finally{
- FileUtil.closeOutputStream(output);
- FileUtil.closeInputStream(input);
- }
- }
- // ------------- 关闭文件流 --------------------
- /**
- * 关闭输入流。
- * @param input
- */
- publicstaticvoidcloseInputStream(InputStream input){
- if(input !=null){
- try{
- input.close();
- } catch(IOException e) {
- logger.error("关闭输入文件失败!");
- logger.error(e);
- }
- }
- }
- /**
- * 关闭输出流。
- * @param output
- */
- publicstaticvoidcloseOutputStream(OutputStream output){
- if(output !=null){
- try{
- output.close();
- } catch(IOException e) {
- logger.error("关闭输出文件失败!");
- logger.error(e);
- }
- }
- }
- }
说明: 下载下来的flashPaper安装程序解压后放置在 WebContent/bin/目录下。
程序自动的时候自动安装,若检测到没有使用系统还原功能,自动拷贝文件到系统system32目录下。
2、在上面贴出的代码中,使用FlashPaperUtil. fileToSwf()函数即可完成对可支持文档的转换。即转成swf文件。
3、显示转成成的swf文件。
displaySwf.jsp
- <%@ pagelanguage="java"contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"isELIgnored="false"%>
- <%@ taglibprefix="c"uri="/WEB-INF/tld/c.tld"%>
- <%
- String webRoot=request.getContextPath();
- %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <basehref="<%=webRoot%>/view/common/"/>
- <title>在线显示文档</title>
- <metahttp-equiv="Content-Type"content="text/html; charset=UTF-8"/>
- <styletype="text/css"media="screen">
- html,body{
- margin: 0px;
- padding: 0px;
- overflow: hidden;
- height: 100%;
- }
- #content{
- width:100%;
- height:100%;
- MARGIN: 0px auto;
- }
- </style>
- </head>
- <body>
- <c:iftest="${swfUrl!=null}">
- <divid="content">
- <object
- classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
- codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0"
- width="100%"
- height="100%"
- >
- <paramname="movie"value="${swfUrl}"/>
- <paramname="quality"value="high"/>
- <paramname="allowFullScreen"value="true"/>
- <embed
- pluginspage="http://www.macromedia.com/go/getflashplayer"
- type="application/x-shockwave-flash"
- src="${swfUrl}"
- allowfullscreen="true"
- quality="high"
- width="100%"
- height="100%"
- >
- </embed>
- </object>
- </div>
- </c:if>
- <c:iftest="${swfUrl==null}">
- <divclass="box error center"style="width:60%;">
- <h1>没有指定要显示的文档路径!</h1>
- </div>
- </c:if>
- </body>
- </html>
处理请求的controller类中相关方法:
- /**
- * 显示在线文档。
- * @return
- */
- @RequestMapping("show")
- publicString show(String swfUrl, HttpServletRequest request){
- request.setAttribute("swfUrl", swfUrl);
- return"common/displaySwf";
- }
请求显示在线文档的url如下:
- http://localhost:8080/isap/fileManage/show.action?swfUrl=http://192.168.1.100/isap_doc/1341818769265.swf
注: 在FlashPaper转各类文档的时候,看到他们相关程序打开了要转的文件,然后转换的。所以碰到问题的时候注意检查一下几点:
1、服务器上是否安装相关文档的打开程序,例如office 2007 --> docx;
2、检查服务器上的Print Spoller服务是否启动,虚拟打印需要用到该服务;
3、还是失败的话,用cmd运行FlashPaper安装程序下的install.bat, 查看错误原因。
ok,至此为止已经完成各种文档的转换和显示问题,希望能有同样需求的同仁们少走弯路。
doc转swf,主流文档在线查看解决方案--类似百度文档功能相关推荐
- php 文档在线查看器,Office Web Viewer 在线Office文档查看器API
您的网站或博客上是否有希望您的读者查看的 Office 文档(即使他们未安装 Office)?您是否更喜欢在下载文档之前查看文档?若要给您的受众提供更好的体验,请试试 Office Web Viewe ...
- PageOffice+MobOffice 电脑、移动跨平台Office文档在线流转解决方案
一直以来,在线办公系统同时支持领导用手机和电脑对Office文档进行审批.盖章,成为了所有开发者无法逾越的难题.卓正软件开发团队历时三年开发了PageOffice 5.0和MobOffice 5.0产 ...
- html 文档在线查看,在网页中在线查看文档(doc、docx 、xls 、xlsx、 pdf 、swf )
步骤一:(涉及到的工具) 步骤二:(配置工程) 1.在项目中导入下载FlexPaper解压后文件(js.css.swf文件等) 2.编写一个html页面 html, body{ height:100% ...
- java pdf在线阅读插件_JAVA实现在线查看PDF和office文档
一个项目中要做一个在线预览附件(和百度文库差不多)的小功能点,楼主在开发过程中踩了很多坑的同时也总结了一些方法,仅供广大猿友参考,那么要实现这个小功能,目前主要是有如下3种可行的实现方式,下面先说实现 ...
- 文档在线查看功能的实现
js验证手机号格式 function isPhone(phone) {var pattern = /^1[34578]\d{9}$/; return pattern.test(phone); } 提交 ...
- office 文档 在线查看
refs: 1)windows平台 office online server安装 https://docs.microsoft.com/zh-cn/officeonlineserver/office- ...
- 绘制四叶玫瑰线matlab,数学实验_word文档在线阅读与下载_文档网
成都学院二零一三到二零一四学年第二学期半期考试 <数学实验>课程考试题 (120分钟) 考试形式:闭卷 考试日期: 年 月 日 所有答案一律写在答题纸上,写在试卷上无效. 一.单项选择题( ...
- java实现Word文档(doc、docx)在线查看功能(前台+后台)
功能需求要求实现文档上传.下载.查看.删除,查看没弄过,别的就不提了,以下是我记录的实现文档在线查看的方法以及效果图 实现思路 首先把word文件转为pdf,然后在用js查看pdf文件实现在线查看功能 ...
- php实现word文档在线浏览功能,配置安装手记
欢迎加入php架构师之旅 群:410028331(招纳贤人-大师中)方便技术的交流 一般类似oa或者crm等管理系统可能都会遇到需要再线查看word文档的功能,类似百度文库. 记得去年小组中的一个成员 ...
最新文章
- cppunit linux,Linux中使用CppUnit工具
- VMVMware-workstation以及CentOS-7安装
- System.ComponentModel.Component : MarshalByRefObject, IComponent, IDisposable
- 零膨胀负二项回归模型的使用 R语言
- php怎么构造一个验证码,PHP封装一个生成验证码的函数
- 关于如何在Android、Java等非微软平台上建立高信任的SharePoint应用程序
- payload的使 常用xss_跨站脚本XSS Payloads生成器
- 打印Activity任务栈脚本:adb shell dumpsys activity
- sqldf包的使用使用-R
- PHP的.htaccess作用
- Http Simulate
- 用计算机表白教程,抖音短视频vbs表白代码使用教程
- 面试最后一问:你有什么问题想问我吗?
- emwin自定义字库
- python——利用正则表达式爬取豆瓣读书中的图书信息
- 筷子SaaS智能系统源码-短视频私域流量增长解决方案,出【源码-无限授权】
- 《三国群英传2网络版》所有装备掉落查询
- 《Java 2 实用教程》课程学习(17)——《Java 程序设计》实验指导书-校内实验教材
- Codeforces Gym100543L Outer space invaders 区间dp 动态规划
- 关于行为评分卡建模的数据准备
热门文章
- 什么是128陷阱?什么是装箱?什么是拆箱?为什么要有包装类?
- 第1章 游戏之乐——连连看游戏设计
- 生态梯田 “薯”光无限
- Error creating bean with name ‘sqlSessionFactory‘ defined in class path reso...报错的解决方法
- MogaFX—日元兑美元30多年来首次突破150日元
- Unity3D RenderTexture实现3D立绘
- Excel VBA ListBox列表框学习
- 6.汇编语言显示、指令
- 中兴ZXQ10排队机出现问题了...
- CAD中怎么绘制攒尖屋顶?CAD设计攒尖屋顶技巧