Java+FlexPaper+swfTools仿百度文库文档在线预览系统设计与实现.doc_第1页
Java+FlexPaper+swfTools仿百度文库文档在线预览系统设计与实现.doc_第2页
Java+FlexPaper+swfTools仿百度文库文档在线预览系统设计与实现.doc_第3页
Java+FlexPaper+swfTools仿百度文库文档在线预览系统设计与实现.doc_第4页
Java+FlexPaper+swfTools仿百度文库文档在线预览系统设计与实现.doc_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Java+FlexPaper+swfTools仿百度文库文档在线预览系统设计与实现笔者最近在给客户开发文档管理系统时,客户要求上传到管理系统的文档(包括ppt,word,excel,txt)只能预览不允许下载。笔者想到了 百度文库和豆丁网,百度文库和豆丁网的在线预览都是利用flash来播放文档的,在网上查阅了大量资料,终于实现了该项功能,现将自己的设计和实现整理如 下。一、如何将文档转成flash支持的swf文件实现在线播放?1.先用openOffice把ppt、word、excel、txt类型的文档转换成pdf2.用swftools将pdf转换成swf,然后利用FlexPaper插件实现在线播放预览。二、具体实现1.安装必备工具组件(1)安装openoffice,openoffice是开源免费的文字处理软件,它可以将office文档转成pdf文件(笔者安装到D:Program Files),openOffice下载地址/download/index.html(2)安装完openoffice后必须启动其server,以命令行方式启动openoffice server。进入cmd命令行提示符D:Program FilesOpenO 3program键入如下命令:soffice -headless -accept=socket,host=,port=8100;urp; nofirststartwizard进入windows任务管理器查看有个进程soffice.bin,说明openoffice启动成功!(3)安装swfTools(安装到 D:Program Files)swftools作用是将pdf转换为swf文件以便flexpaper播放。下载地址:/download.html(4)下载flexpaper,下载地址:/download/笔者下载的是FlexPaper_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等。下载地址:/projects/jodconverter/files/我们后面开发主要用它的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文件htmlview plaincopy1 3 4 5 6 7 文档在线预览系统 8 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 16 17 18 19 20 21 22 请上传要处理的文件,过程可能需要几分钟,请稍候片刻。 23 24 25 26 27 28 29 30 31 32 33 (4)创建文档转换类DocConverter.javajavaview plaincopy34 package com.cectsims.util; 35 import java.io.BufferedInputStream; 36 import java.io.File; 37 import java.io.IOException; 38 import java.io.InputStream; 39 40 import com.artofsolving.jodconverter.DocumentConverter; 41 import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection; 42 import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection; 43 import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter; 44 45 /* 46 * doc docx格式转换 47 */ 48 public class DocConverter 49 private static final int environment = 1;/ 环境 1:windows 2:linux 50 private String fileString;/ (只涉及pdf2swf路径问题) 51 private String outputPath = ;/ 输入路径 ,如果不设置就输出在默认的位置 52 private String fileName; 53 private File pdfFile; 54 private File swfFile; 55 private File docFile; 56 57 public DocConverter(String fileString) 58 ini(fileString); 59 60 61 /* 62 * 重新设置file 63 * 64 * param fileString 65 */ 66 public void setFile(String fileString) 67 ini(fileString); 68 69 70 /* 71 * 初始化 72 * 73 * param fileString 74 */ 75 private void ini(String fileString) 76 this.fileString = fileString; 77 fileName = fileString.substring(0, fileString.lastIndexOf(.); 78 docFile = new File(fileString); 79 pdfFile = new File(fileName + .pdf); 80 swfFile = new File(fileName + .swf); 81 82 83 /* 84 * 转为PDF 85 * 86 * param file 87 */ 88 private void doc2pdf() throws Exception 89 if (docFile.exists() 90 if (!pdfFile.exists() 91 OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100); 92 try 93 connection.connect(); 94 DocumentConverter converter = new OpenOfficeDocumentConverter(connection); 95 converter.convert(docFile, pdfFile); 96 / close the connection 97 connection.disconnect(); 98 System.out.println(*pdf转换成功,PDF输出: + pdfFile.getPath()+ *); 99 catch (.ConnectException e) 100 e.printStackTrace(); 101 System.out.println(*swf转换器异常,openoffice服务未启动!*); 102 throw e; 103 catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) 104 e.printStackTrace(); 105 System.out.println(*swf转换器异常,读取转换文件失败*); 106 throw e; 107 catch (Exception e) 108 e.printStackTrace(); 109 throw e; 110 111 else 112 System.out.println(*已经转换为pdf,不需要再进行转化*); 113 114 else 115 System.out.println(*swf转换器异常,需要转换的文档不存在,无法转换*); 116 117 118 119 /* 120 * 转换成 swf 121 */ 122 SuppressWarnings(unused) 123 private void pdf2swf() throws Exception 124 Runtime r = Runtime.getRuntime(); 125 if (!swfFile.exists() 126 if (pdfFile.exists() 127 if (environment = 1) / windows环境处理 128 try 129 Process p = r.exec(D:/Program Files/SWFTools/pdf2swf.exe + pdfFile.getPath() + -o + swfFile.getPath() + -T 9); 130 System.out.print(loadStream(p.getInputStream(); 131 System.err.print(loadStream(p.getErrorStream(); 132 System.out.print(loadStream(p.getInputStream(); 133 System.err.println(*swf转换成功,文件输出: 134 + swfFile.getPath() + *); 135 if (pdfFile.exists() 136 pdfFile.delete(); 137 138 139 catch (IOException e) 140 e.printStackTrace(); 141 throw e; 142 143 else if (environment = 2) / linux环境处理 144 try 145 Process p = r.exec(pdf2swf + pdfFile.getPath() 146 + -o + swfFile.getPath() + -T 9); 147 System.out.print(loadStream(p.getInputStream(); 148 System.err.print(loadStream(p.getErrorStream(); 149 System.err.println(*swf转换成功,文件输出: 150 + swfFile.getPath() + *); 151 if (pdfFile.exists() 152 pdfFile.delete(); 153 154 catch (Exception e) 155 e.printStackTrace(); 156 throw e; 157 158 159 else 160 System.out.println(*pdf不存在,无法转换*); 161 162 else 163 System.out.println(*swf已经存在不需要转换*); 164 165 166 167 static String loadStream(InputStream in) throws IOException 168 169 int ptr = 0; 170 in = new BufferedInputStream(in); 171 StringBuffer buffer = new StringBuffer(); 172 173 while (ptr = in.read() != -1) 174 buffer.append(char) ptr); 175 176 177 return buffer.toString(); 178 179 /* 180 * 转换主方法 181 */ 182 SuppressWarnings(unused) 183 public boolean conver() 184 185 if (swfFile.exists() 186 System.out.println(*swf转换器开始工作,该文件已经转换为swf*); 187 return true; 188 189 190 if (environment = 1) 191 System.out.println(*swf转换器开始工作,当前设置运行环境windows*); 192 else 193 System.out.println(*swf转换器开始工作,当前设置运行环境linux*); 194 195 try 196 doc2pdf(); 197 pdf2swf(); 198 catch (Exception e) 199 e.printStackTrace(); 200 return false; 201 202 203 if (swfFile.exists() 204 return true; 205 else 206 return false; 207 208 209 210 /* 211 * 返回文件路径 212 * 213 * param s 214 */ 215 public String getswfPath() 216 if (swfFile.exists() 217 String tempString = swfFile.getPath(); 218 tempString = tempString.replaceAll(, /); 219 return tempString; 220 else 221 return ; 222 223 224 225 /* 226 * 设置输出路径 227 */ 228 public void setOutputPath(String outputPath) 229 this.outputPath = outputPath; 230 if (!outputPath.equals() 231 String realName = fileName.substring(fileName.lastIndexOf(/), 232 fileName.lastIndexOf(.); 233 if (outputPath.charAt(outputPath.length() = /) 234 swfFile = new File(outputPath + realName + .swf); 235 else 236 swfFile = new File(outputPath + realName + .swf); 237 238 239 240 241 (5)创建文档上传转换处理文件docUploadConvertAction.jsp文件htmlview plaincopy242 244 245 246 247 248 249 250 % 251 /文件上传采用cos组件上传,可更换为commons-fileupload上传,文件上传后,保存在upload文件夹 252 /获取文件上传路径 253 String saveDirectory =application.getRealPath(/)+upload; 254 /打印上传路径信息 255 System.out.println(saveDirectory); 256 /每个文件最大50m 257 int maxPostSize = 50 * 1024 * 1024 ; 258 /采用cos缺省的命名策略,重名后加1,2,3.如果不加dfp重名将覆盖 259 DefaultFileRenamePolicy dfp = new DefaultFileRenamePolicy(); 260 /response的编码为UTF-8,同时采用缺省的文件名冲突解决策略,实现上传,如果不加dfp重名将覆盖 261 MultipartRequest multi = new MultipartRequest(request, saveDirectory, maxPostSize,UTF-8,dfp); 262 /MultipartRequest multi = new MultipartRequest(request, saveDirectory, maxPostSize,UTF-8); 263 /输出反馈信息 264 Enumeration files = multi.getFileNames(); 265 while (files.hasMoreElements() 266 System.err.println(ccc); 267 String name = (String)files.nextElement(); 268 File f = multi.getFile(name); 269 if(f!=null) 270 String fileName = multi.getFilesystemName(name); 271 /获取上传文件的扩展名 272 String extName=fileName.substring(fileName.lastIndexOf(.)+1); 273 /文件全路径 274 String lastFileName= saveDirectory+ + fileName; 275 /获取需要转换的文件名,将路径名中的替换为/ 276 String converfilename = saveDirectory.replaceAll(, /)+/+fileName; 277 System.out.println(converfilename); 278 /调用转换类DocConverter,并将需要转换的文件传递给该类的构造方法 279 DocConverter d = new DocConverter(converfilename); 280 /调用conver方法开始转换,先执行doc2pdf()将office文件转换为pdf;再执行pdf2swf()将pdf转换为swf; 281 d.conver(); 282 /调用getswfPath()方法,打印转换后的swf文件路径 283 System.out.println(d.getswfPath(); 284 /生成swf相对路径,以便传递给flexpaper播放器 285 String swfpath = upload+d.getswfPath().substring(d.getswfPath().lastIndexOf(/); 286 System.out.println(swfpath); 287 /将相对路径放入sessio中保存 288 session.setAttribute(swfpath, swfpath); 289 out.println(上传的文件:+lastFileName); 290 out.println(文件类型+extName); 291 out.println(); 292 293 294 295 % 296 297 298 299 300 Insert title here 301 302 body margin-top:100px;background:#fff;font-family: Verdana, Tahoma; 303 a color:#CE4614; 304 #msg-box color: #CE4614; font-size:0.9em;text-align:center; 305 #msg-box .logo border-bottom:5px solid #ECE5D9;margin-bottom:20px;padding-bottom:10px; 306 #msg-box .title font-size:1.4em;font-weight:bold;margin:0 0 30px 0; 307 #msg-box .nav margin-top:20px; 308 309 310 311 312 313 314 315 316 317 (6)创建文档预览文件documentView.jsphtmlview plaincopy318 320 323 324 325 326 327 328 329 330 331 html, body height:100%; 332 body margin:0; padding:0; overflow:auto; 333 #flashContent display:none; 334 335 336 文档在线预览系统 337 338 339 340 341 342 343 var fp = new FlexPaperVie

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论