收藏 分销(赏)

java-flexpaper-swftools仿百度文库文档在线预览系统设计与实现.doc

上传人:天**** 文档编号:2493065 上传时间:2024-05-30 格式:DOC 页数:19 大小:197.72KB
下载 相关 举报
java-flexpaper-swftools仿百度文库文档在线预览系统设计与实现.doc_第1页
第1页 / 共19页
java-flexpaper-swftools仿百度文库文档在线预览系统设计与实现.doc_第2页
第2页 / 共19页
点击查看更多>>
资源描述
Java FlexPaper swfTools_仿百度文库文档在线预览系统设计与实现 最近在给客户开发文档管理系统时,客户要求上传到管理系统的文档(包括ppt,word,excel,txt)只能预览不允许下载。想到了百度文库和豆丁网,百度文库和豆丁网的在线预览都是利用flash来播放文档的,在网上查阅了大量资料,终于实现了该项功能,现将自己的设计和实现整理如下,自己可以试试。 一、如何将文档转成flash支持的swf文件实现在线播放? 1.先用openOffice把ppt、word、excel、txt类型的文档转换成pdf 2.用swftools将pdf转换成swf,然后利用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 Files\OpenOffice.org 3\program\ 键入如下命令: 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,下载地址: 笔者下载的是FlexPaper_1.5.1,下载后将其解压备用。 (5)下载OpenDocument文档转换器 JODConverter,JODConverter是一个java的OpenDucument文件转换器,可以进行许多文件格式的转换,它利用OpenOffice来进行转换工作,它能进行以下的转换工作: a.Microsoft Office格式转换为OpenDucument,以及OpenDucument转换为Microsoft Office b.OpenDucument转换为PDF,Word、Excel、PowerPoint转换为PDF,RTF转换为PDF等。 下载地址: 我们后面开发主要用它的jodconverter-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文件 [html] view plaincopy 1. <%@ page language="java" contentType="text/html; charset=UTF-8"   2.     pageEncoding="UTF-8"%>   3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">   4. <html>   5. <head>   6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">   7. <title>文档在线预览系统</title>   8. <style>   9.     body {margin-top:100px;background:#fff;font-family: Verdana, Tahoma;}   10.     a {color:#CE4614;}   11.     #msg-box {color: #CE4614; font-size:0.9em;text-align:center;}   12.     #msg-box .logo {border-bottom:5px solid #ECE5D9;margin-bottom:20px;padding-bottom:10px;}   13.     #msg-box .title {font-size:1.4em;font-weight:bold;margin:0 0 30px 0;}   14.     #msg-box .nav {margin-top:20px;}   15. </style>   16.    17. </head>   18. <body>   19. <div id="msg-box">   20.     <form name="form1"  method="post" enctype="multipart/form-data" action="docUploadConvertAction.jsp">   21.         <div class="title">   22.             请上传要处理的文件,过程可能需要几分钟,请稍候片刻。   23.         </div>   24.         <p>   25.             <input name="file1" type="file">   26.         </p>   27.         <p>   28.             <input type="submit" name="Submit" value="上传">   29.         </p>   30.     </form >   31. </div>   32. </body>   33. </html>   (4)创建文档转换类DocConverter.java [java] view plaincopy 1. package com.cectsims.util;   2. import java.io.BufferedInputStream;   3. import java.io.File;   4. import java.io.IOException;   5. import java.io.InputStream;   6.    7. import com.artofsolving.jodconverter.DocumentConverter;   8. import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;   9. import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;   10. import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;   11.    12. /**  13.  * doc docx格式转换  14.  */   15. public class DocConverter {   16.     private static final int environment = 1;// 环境 1:windows 2:linux   17.     private String fileString;// (只涉及pdf2swf路径问题)   18.     private String outputPath = "";// 输入路径 ,如果不设置就输出在默认的位置   19.     private String fileName;   20.     private File pdfFile;   21.     private File swfFile;   22.     private File docFile;   23.        24.     public DocConverter(String fileString) {   25.         ini(fileString);   26.     }   27.    28.     /**  29.      * 重新设置file  30.      *   31.      * @param fileString  32.      */   33.     public void setFile(String fileString) {   34.         ini(fileString);   35.     }   36.    37.     /**  38.      * 初始化  39.      *   40.      * @param fileString  41.      */   42.     private void ini(String fileString) {   43.         this.fileString = fileString;   44.         fileName = fileString.substring(0, fileString.lastIndexOf("."));   45.         docFile = new File(fileString);   46.         pdfFile = new File(fileName + ".pdf");   47.         swfFile = new File(fileName + ".swf");   48.     }   49.        50.     /**  51.      * 转为PDF  52.      *   53.      * @param file  54.      */   55.     private void doc2pdf() throws Exception {   56.         if (docFile.exists()) {   57.             if (!pdfFile.exists()) {   58.                 OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);   59.                 try {   60.                     connection.connect();   61.                     DocumentConverter converter = new OpenOfficeDocumentConverter(connection);   62.                     converter.convert(docFile, pdfFile);   63.                     // close the connection   64.                     connection.disconnect();   65.                     System.out.println("****pdf转换成功,PDF输出:" + pdfFile.getPath()+ "****");   66.                 } catch (.ConnectException e) {   67.                     e.printStackTrace();   68.                     System.out.println("****swf转换器异常,openoffice服务未启动!****");   69.                     throw e;   70.                 } catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {   71.                     e.printStackTrace();   72.                     System.out.println("****swf转换器异常,读取转换文件失败****");   73.                     throw e;   74.                 } catch (Exception e) {   75.                     e.printStackTrace();   76.                     throw e;   77.                 }   78.             } else {   79.                 System.out.println("****已经转换为pdf,不需要再进行转化****");   80.             }   81.         } else {   82.             System.out.println("****swf转换器异常,需要转换的文档不存在,无法转换****");   83.         }   84.     }   85.        86.     /**  87.      * 转换成 swf  88.      */   89.     @SuppressWarnings("unused")   90.     private void pdf2swf() throws Exception {   91.         Runtime r = Runtime.getRuntime();   92.         if (!swfFile.exists()) {   93.             if (pdfFile.exists()) {   94.                 if (environment == 1) {// windows环境处理   95.                     try {   96.                         Process p = r.exec("D:/Program Files/SWFTools/pdf2swf.exe "+ pdfFile.getPath() + " -o "+ swfFile.getPath() + " -T 9");   97.                         System.out.print(loadStream(p.getInputStream()));   98.                         System.err.print(loadStream(p.getErrorStream()));   99.                         System.out.print(loadStream(p.getInputStream()));   100.                         System.err.println("****swf转换成功,文件输出:"   101.                                 + swfFile.getPath() + "****");   102.                         if (pdfFile.exists()) {   103.                             pdfFile.delete();   104.                         }   105.    106.                     } catch (IOException e) {   107.                         e.printStackTrace();   108.                         throw e;   109.                     }   110.                 } else if (environment == 2) {// linux环境处理   111.                     try {   112.                         Process p = r.exec("pdf2swf " + pdfFile.getPath()   113.                                 + " -o " + swfFile.getPath() + " -T 9");   114.                         System.out.print(loadStream(p.getInputStream()));   115.                         System.err.print(loadStream(p.getErrorStream()));   116.                         System.err.println("****swf转换成功,文件输出:"   117.                                 + swfFile.getPath() + "****");   118.                         if (pdfFile.exists()) {   119.                             pdfFile.delete();   120.                         }   121.                     } catch (Exception e) {   122.                         e.printStackTrace();   123.                         throw e;   124.                     }   125.                 }   126.             } else {   127.                 System.out.println("****pdf不存在,无法转换****");   128.             }   129.         } else {   130.             System.out.println("****swf已经存在不需要转换****");   131.         }   132.     }   133.    134.     static String loadStream(InputStream in) throws IOException {   135.    136.         int ptr = 0;   137.         in = new BufferedInputStream(in);   138.         StringBuffer buffer = new StringBuffer();   139.    140.         while ((ptr = in.read()) != -1) {   141.             buffer.append((char) ptr);   142.         }   143.    144.         return buffer.toString();   145.     }   146.     /**  147.      * 转换主方法  148.      */   149.     @SuppressWarnings("unused")   150.     public boolean conver() {   151.    152.         if (swfFile.exists()) {   153.             System.out.println("****swf转换器开始工作,该文件已经转换为swf****");   154.             return true;   155.         }   156.    157.         if (environment == 1) {   158.             System.out.println("****swf转换器开始工作,当前设置运行环境windows****");   159.         } else {   160.             System.out.println("****swf转换器开始工作,当前设置运行环境linux****");   161.         }   162.         try {   163.             doc2pdf();   164.             pdf2swf();   165.         } catch (Exception e) {   166.             e.printStackTrace();   167.             return false;   168.         }   169.    170.         if (swfFile.exists()) {   171.             return true;   172.         } else {   173.             return false;   174.         }   175.     }   176.    177.     /**  178.      * 返回文件路径  179.      *   180.      * @param s  181.      */   182.     public String getswfPath() {   183.         if (swfFile.exists()) {   184.             String tempString = swfFile.getPath();   185.             tempString = tempString.replaceAll("\\\\", "/");   186.             return tempString;   187.         } else {   188.             return "";   189.         }   190.    191.     }   192.     /**  193.      * 设置输出路径  194.      */   195.     public void setOutputPath(String outputPath) {   196.         this.outputPath = outputPath;   197.         if (!outputPath.equals("")) {   198.             String realName = fileName.substring(fileName.lastIndexOf("/"),   199.                     fileName.lastIndexOf("."));   200.             if (outputPath.charAt(outputPath.length()) == '/') {   201.                 swfFile = new File(outputPath + realName + ".swf");   202.             } else {   203.                 swfFile = new File(outputPath + realName + ".swf");   204.             }   205.         }   206.     }   207.    208. }   (5)创建文档上传转换处理文件docUploadConvertAction.jsp文件 [html] view plaincopy 1. <%@ page language="java" contentType="text/html; charset=UTF-8"   2.     pageEncoding="UTF-8"%>   3.    4. <%@page import="java.io.*"%>   5. <%@page import="java.util.Enumeration"%>   6. <%@page import="com.oreilly.servlet.MultipartRequest"%>   7. <%@page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy"%>   8. <%@page import="com.cectsims.util.DocConverter"%>   9. <%   10. //文件上传采用cos组件上传,可更换为commons-fileupload上传,文件上传后,保存在upload文件夹   11. //获取文件上传路径   12. String saveDirectory =application.getRealPath("/")+"upload";   13. //打印上传路径信息   14. System.out.println(saveDirectory);   15. //每个文件最大50m   16. int maxPostSize = 50 * 1024 * 1024 ;   17. //采用cos缺省的命名策略,重名后加1,2,3...如果不加dfp重名将覆盖   18. DefaultFileRenamePolicy dfp = new DefaultFileRenamePolicy();   19. //response的编码为"UTF-8",同时采用缺省的文件名冲突解决策略,实现上传,如果不加dfp重名将覆盖   20. MultipartRequest multi = new MultipartRequest(request, saveDirectory, maxPostSize,"UTF-8",dfp);   21. //MultipartRequest multi = new MultipartRequest(request, saveDirectory, maxPostSize,"UTF-8");   22. //输出反馈信息   23.  Enumeration files = multi.getFileNames();   24.      while (files.hasMoreElements()) {   25.         System.err.println("ccc");   26.        String name = (String)files.nextElement();   27.        File f = multi.getFile(name);   28.        if(f!=null){   29.          String fileName = multi.getFilesystemName(name);   30.          //获取上传文件的扩展名   31.          String extName=fileName.substring(fileName.lastIndexOf(".")+1);   32.          //文件全路径   33.          String lastFileName= saveDirectory+"\\" + fileName;   34.          //获取需要转换的文件名,将路径名中的'\'替换为'/'   35.          String converfilename = saveDirectory.replaceAll("\\\\", "/")+"/"+fileName;   36.          System.out.println(converfilename);   37.          //调用转换类DocConverter,并将需要转换的文件传递给该类的构造方法   38.          DocConverter d = new DocConverter(converfilename);   39.          //调用conver方法开始转换,先执行doc2pdf()将office文件转换为pdf;再执行pdf2swf()将pdf转换为swf;   40.          d.conver();   41.          //调用getswfPath()方法,打印转换后的swf文件路径   42.          System.out.println(d.getswfPath());   43.          //生成swf相对路径,以便传递给flexpaper播放器   44.          String swfpath = "upload"+d.getswfPath().substring(d.getswfPath().lastIndexOf("/"));   45.          System.out.println(swfpath);   46.          //将相对路径放入sessio中保存   47.          session.setAttribute("swfpath", swfpath);   48.          out.println("上传的文件:"+lastFileName);   49.          out.println("文件类型"+extName);   50.          out.println("<hr>");   51.        }   52.      }   53.    54. %>   55. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">   56. <html>   57. <head>   58. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">   59. <title>Insert title here</title>   60. <style>   61.     body {margin-top:100px;background:#fff;font-family: Verdana, Tahoma;}   62.     a {color:#CE4614;}   63.     #msg-box {color: #CE4614; font-size:0.9em;text-align:center;}   64.     #msg-box .logo {border-bottom:5px solid #ECE5D9;margin-bottom:20px;padding-bottom:10px;}   65.     #msg-box .title {font-size:1.4em;font-weight:bold;margin:0 0 30px 0;}   66.     #msg-box .nav {margin-top:20px;}   67. </style>   68. </head>   69. <body>   70.     <div>   71.         <form name="viewForm" id="form_swf" action="documnetView.jsp" method="POST">   72.             <input type='submit' value='预览' class='BUTTON SUBMIT'/>   73.         </form>   74.     </div>   75. </body>   76. </html>   (6)创建文档预览文件documentView.jsp [html] view plaincopy 1. <%@ page language="java" contentType="text/html; charset=UTF-8"   2.     pageEncoding="UTF-8"%>   3. <
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传
相似文档                                   自信AI助手自信AI助手

当前位置:首页 > 通信科技 > 开发语言

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服