使用FlexPaper实现在线预览

准备工作

1.下载并安装安装OpenOffice

2.下载并安装FlexPaper

3.下载jodconverter

5.下载并安装swftools

软件安装具体方式按照 这篇博客 :http://blog.csdn.net/xinxin19881112/article/details/48681637 操作

这篇文章的处理方式大致分为以下几个步骤,

1:上传源文件。

2:根据源文件类型转成pdf。

3:根据pdf转成swf文件

4:使用flexPaper 打开swf文件

根据此操作遇到各种小问题。比如在查看swf文件时 右上角圈圈一直在加载 看下图

这种情况 可能是你上传的文件名称包含中文。 

我处理的方式是把 第2步和第3步 给定义一个根据时间命名的文件。避免生成包含中文的文件名。下面代码

源代码为:源文件是什么名称 pdf文件和swf文件都会启用同样的名称

 

 

修改代码:重新构造方法把一个参数改为两个参数的方式 文件名替换成根据时间长度的格式避免包含文件为中文

 

 

注意:软件安装路径也需要修改为自己本地对应的路径,避免程序找不到指定的exe。

 我处理过的代码文件!

创建 fileUpload.jsp

 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>  

 

 创建 docUploadConvertAction.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  
 2   
 3 <%@page import="java.io.*"%>  
 4 <%@page import="java.util.Enumeration"%>  
 5 <%@page import="com.cectsims.util.DocConverter"%>  
 6 <%@page import="com.oreilly.servlet.MultipartRequest"%>  
 7 <%@page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy"%>  
 8 <%  
 9 //文件上传采用cos组件上传,可更换为commons-fileupload上传,文件上传后,保存在upload文件夹  
10 //获取文件上传路径  
11 String saveDirectory =application.getRealPath("/")+"upload";  
12 //String saveDirectory ="E:/6.5workspace/TESTUpdown/WebRoot/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("\\\\", "/");  
36          System.out.println("converfilename:"+converfilename);  
37          //调用转换类DocConverter,并将需要转换的文件传递给该类的构造方法  
38          DocConverter d = new DocConverter(converfilename,fileName);  
39          //调用conver方法开始转换,先执行doc2pdf()将office文件转换为pdf;再执行pdf2swf()将pdf转换为swf;  
40          d.conver();  
41          //调用getswfPath()方法,打印转换后的swf文件路径  
42          System.out.println("swf:"+d.getswfPath());  
43          //生成swf相对路径,以便传递给flexpaper播放器  
44          String swfpath = "upload"+d.getswfPath().substring(d.getswfPath().lastIndexOf("/"));  
45          System.out.println("swfpath:"+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="documentView.jsp" method="POST">  
72             <input type='submit' value='预览' class='BUTTON SUBMIT'/>  
73         </form>  
74     </div>  
75 </body>  
76 </html>  

创建实体类 DocConverter.java

  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 import java.util.Date;
  7 
  8 import com.artofsolving.jodconverter.DocumentConverter;
  9 import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
 10 import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
 11 import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
 12   
 13 /** 
 14  * doc docx格式转换 
 15  */  
 16 public class DocConverter {  
 17     private static final int environment = 1;// 环境 1:windows 2:linux  
 18     private String fileString;// (只涉及pdf2swf路径问题)  
 19     private String outputPath = "";// 输入路径 ,如果不设置就输出在默认的位置  
 20     private String fileName;  
 21     private File pdfFile;  
 22     private File swfFile;  
 23     private File docFile;  
 24       
 25     public DocConverter(String fileString) {  
 26         System.out.println("DocConverter:"+fileString);
 27         ini(fileString);  
 28     }  
 29     public DocConverter(String fileString,String fileNames) {  
 30         System.out.println("DocConverter:"+fileString);
 31         System.out.println("fileName:"+fileNames);
 32         ini(fileString,fileNames);  
 33     }
 34     /** 
 35      * 重新设置file 
 36      *  
 37      * @param fileString 
 38      */  
 39     public void setFile(String fileString) {  
 40         ini(fileString);  
 41     }  
 42   
 43     /** 
 44      * 初始化 
 45      *  
 46      * @param fileString 
 47      */  
 48     private void ini(String fileString) {  
 49         this.fileString = fileString;  
 50         fileName = fileString.substring(0, fileString.lastIndexOf("."));  
 51         System.out.println("fileName:"+fileName); 
 52         pdfFile = new File(fileName + ".pdf");  
 53         swfFile = new File(fileName + ".swf");  
 54     }  
 55     private void ini(String fileString,String fileNames) {  
 56         this.fileString = fileString;  
 57 //        fileName = fileNames.substring(0, fileString.lastIndexOf("."));  
 58         System.out.println("fileString:"+fileString);
 59         System.out.println("fileName:"+fileNames);
 60         String sj=new Date().getTime()+"";
 61         System.out.println("fileNamefileName:"+fileString+"/"+sj);
 62         
 63         docFile = new File(fileString+"/"+fileNames); 
 64         pdfFile = new File(fileString+"/"+sj + ".pdf");  
 65         swfFile = new File(fileString+"/"+sj + ".swf");  
 66     }
 67     /** 
 68      * 转为PDF 
 69      *  
 70      * @param file 
 71      */  
 72     private void doc2pdf() throws Exception {  
 73         if (docFile.exists()) {  
 74             if (!pdfFile.exists()) {  
 75                 OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);  
 76                 try {  
 77                     connection.connect();  
 78                     DocumentConverter converter = new OpenOfficeDocumentConverter(connection);  
 79                     converter.convert(docFile, pdfFile);  
 80                     // close the connection  
 81                     connection.disconnect();  
 82                     System.out.println("****pdf转换成功,PDF输出:" + pdfFile.getPath()+ "****");  
 83                 } catch (java.net.ConnectException e) {  
 84                     e.printStackTrace();  
 85                     System.out.println("****swf转换器异常,openoffice服务未启动!****");  
 86                     throw e;  
 87                 } catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {  
 88                     e.printStackTrace();  
 89                     System.out.println("****swf转换器异常,读取转换文件失败****");  
 90                     throw e;  
 91                 } catch (Exception e) {  
 92                     e.printStackTrace();  
 93                     throw e;  
 94                 }  
 95             } else {  
 96                 System.out.println("****已经转换为pdf,不需要再进行转化****");  
 97             }  
 98         } else {  
 99             System.out.println("****swf转换器异常,需要转换的文档不存在,无法转换****");  
100         }  
101     }  
102       
103     /** 
104      * 转换成 swf 
105      */  
106     private void pdf2swf() throws Exception {  
107         Runtime r = Runtime.getRuntime();  
108         if (!swfFile.exists()) {  
109             if (pdfFile.exists()) {  
110                 if (environment == 1) {// windows环境处理  
111                     try {  
112                         Process p = r.exec("D:/SWFTools/pdf2swf.exe "+ pdfFile.getPath() + " -o "+ swfFile.getPath() + " -T 9");  
113                         System.out.print("1"+loadStream(p.getInputStream()));  
114                         System.err.print(loadStream(p.getErrorStream()));  
115                         System.out.print("2"+loadStream(p.getInputStream()));  
116                         System.err.println("****swf转换成功,文件输出:"  
117                                 + swfFile.getPath() + "****");  
118                         if (pdfFile.exists()) {  
119                             pdfFile.delete();  
120                         }  
121   
122                     } catch (IOException e) {  
123                         e.printStackTrace();  
124                         throw e;  
125                     }  
126                 } else if (environment == 2) {// linux环境处理  
127                     try {  
128                         Process p = r.exec("pdf2swf " + pdfFile.getPath()  
129                                 + " -o " + swfFile.getPath() + " -T 9");  
130                         System.out.print(loadStream(p.getInputStream()));  
131                         System.err.print(loadStream(p.getErrorStream()));  
132                         System.err.println("****swf转换成功,文件输出:"  
133                                 + swfFile.getPath() + "****");  
134                         if (pdfFile.exists()) {  
135                             pdfFile.delete();  
136                         }  
137                     } catch (Exception e) {  
138                         e.printStackTrace();  
139                         throw e;  
140                     }  
141                 }  
142             } else {  
143                 System.out.println("****pdf不存在,无法转换****");  
144             }  
145         } else {  
146             System.out.println("****swf已经存在不需要转换****");  
147         }  
148     }  
149   
150     static String loadStream(InputStream in) throws IOException {  
151   
152         int ptr = 0;  
153         in = new BufferedInputStream(in);  
154         StringBuffer buffer = new StringBuffer();  
155   
156         while ((ptr = in.read()) != -1) {  
157             buffer.append((char) ptr);  
158         }  
159   
160         return buffer.toString();  
161     }  
162     /** 
163      * 转换主方法 
164      */  
165     public boolean conver() {  
166   
167         if (swfFile.exists()) {  
168             System.out.println("****swf转换器开始工作,该文件已经转换为swf****");  
169             return true;  
170         }  
171   
172         if (environment == 1) {  
173             System.out.println("****swf转换器开始工作,当前设置运行环境windows****");  
174         } else {  
175             System.out.println("****swf转换器开始工作,当前设置运行环境linux****");  
176         }  
177         try {  
178             doc2pdf();  
179             pdf2swf();  
180         } catch (Exception e) {  
181             e.printStackTrace();  
182             return false;  
183         }  
184   
185         if (swfFile.exists()) {  
186             return true;  
187         } else {  
188             return false;  
189         }  
190     }  
191   
192     /** 
193      * 返回文件路径 
194      *  
195      * @param s 
196      */  
197     public String getswfPath() {  
198         if (swfFile.exists()) {  
199             String tempString = swfFile.getPath();  
200             tempString = tempString.replaceAll("\\\\", "/");  
201             return tempString;  
202         } else {  
203             return "";  
204         }  
205   
206     }  
207     /** 
208      * 设置输出路径 
209      */  
210     public void setOutputPath(String outputPath) {  
211         this.outputPath = outputPath;  
212         if (!outputPath.equals("")) {  
213             String realName = fileName.substring(fileName.lastIndexOf("/"),  
214                     fileName.lastIndexOf("."));  
215             if (outputPath.charAt(outputPath.length()) == '/') {  
216                 swfFile = new File(outputPath + realName + ".swf");  
217             } else {  
218                 swfFile = new File(outputPath + realName + ".swf");  
219             }  
220         }  
221     }  
222   
223 } 

最后展示页面 documentView.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  
 2 <%  
 3     String swfFilePath=session.getAttribute("swfpath").toString();  
 4     System.out.println("swfFilePath:"+swfFilePath);
 5 %>  
 6 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
 7 <html>  
 8 <head>  
 9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
10 <script type="text/javascript" src="js/jquery.js"></script>  
11 <script type="text/javascript" src="js/flexpaper_flash.js"></script>  
12 <script type="text/javascript" src="js/flexpaper_flash_debug.js"></script>  
13 <style type="text/css" media="screen">   
14             html, body  { height:100%; }  
15             body { margin:0; padding:0; overflow:auto; }     
16             #flashContent { display:none; }  
17         </style>   
18   
19 <title>文档在线预览系统</title>  
20 </head>  
21 <body>   
22         <div style="position:absolute;left:50px;top:10px;">  
23             <a id="viewerPlaceHolder" style="width:820px;height:650px;display:block"></a>  
24               
25             <script type="text/javascript">   
26                 var fp = new FlexPaperViewer(     
27                          'FlexPaperViewer',  
28                          'viewerPlaceHolder', { config : {  
29                          SwfFile : escape('<%=swfFilePath%>'),  
30                          Scale : 0.6,   
31                          ZoomTransition : 'easeOut',  
32                          ZoomTime : 0.5,  
33                          ZoomInterval : 0.2,  
34                          FitPageOnLoad : true,  
35                          FitWidthOnLoad : false,  
36                          FullScreenAsMaxWindow : false,  
37                          ProgressiveLoading : false,  
38                          MinZoomSize : 0.2,  
39                          MaxZoomSize : 5,  
40                          SearchMatchAll : false,  
41                          InitViewMode : 'SinglePage',  
42                            
43                          ViewModeToolsVisible : true,  
44                          ZoomToolsVisible : true,  
45                          NavToolsVisible : true,  
46                          CursorToolsVisible : true,  
47                          SearchToolsVisible : true,  
48                           
49                          localeChain: 'zh_CN'  
50                          }});  
51             </script>              
52         </div>  
53 </body>  
54 </html>  

 

每个测试项目上都会遇到一些小问题!耐心就好!

 

posted @ 2017-12-20 17:30  MissHuoLe  阅读(11164)  评论(0编辑  收藏  举报