




已阅读5页,还剩11页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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:programfiles),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. bodymargin-top:100px;background:#fff;font-family:verdana,tahoma;10. acolor:#ce4614;11. #msg-boxcolor:#ce4614;font-size:0.9em;text-align:center;12. #msg-box.logoborder-bottom:5pxsolid#ece5d9;margin-bottom:20px;padding-bottom:10px;13. #msg-box.titlefont-size:1.4em;font-weight:bold;margin:0030px0;14. #msg-box.navmargin-top:20px;15. 16. 17. 18. 19. 20. 21. 22. 请上传要处理的文件,过程可能需要几分钟,请稍候片刻。23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. (4)创建文档转换类docconverter.javajavaview plaincopy1. packagecom.cectsims.util;2. importjava.io.bufferedinputstream;3. importjava.io.file;4. importjava.io.ioexception;5. importjava.io.inputstream;6. 7. importcom.artofsolving.jodconverter.documentconverter;8. importcom.artofsolving.jodconverter.openoffice.connection.openofficeconnection;9. importcom.artofsolving.jodconverter.openoffice.connection.socketopenofficeconnection;10. importcom.artofsolving.jodconverter.openoffice.converter.openofficedocumentconverter;11. 12. /*13. *docdocx格式转换14. */15. publicclassdocconverter16. privatestaticfinalintenvironment=1;/环境1:windows2:linux17. privatestringfilestring;/(只涉及pdf2swf路径问题)18. privatestringoutputpath=;/输入路径,如果不设置就输出在默认的位置19. privatestringfilename;20. privatefilepdffile;21. privatefileswffile;22. privatefiledocfile;23. 24. publicdocconverter(stringfilestring)25. ini(filestring);26. 27. 28. /*29. *重新设置file30. *31. *paramfilestring32. */33. publicvoidsetfile(stringfilestring)34. ini(filestring);35. 36. 37. /*38. *初始化39. *40. *paramfilestring41. */42. privatevoidini(stringfilestring)43. this.filestring=filestring;44. filename=filestring.substring(0,filestring.lastindexof(.);45. docfile=newfile(filestring);46. pdffile=newfile(filename+.pdf);47. swffile=newfile(filename+.swf);48. 49. 50. /*51. *转为pdf52. *53. *paramfile54. */55. privatevoiddoc2pdf()throwsexception56. if(docfile.exists()57. if(!pdffile.exists()58. openofficeconnectionconnection=newsocketopenofficeconnection(8100);59. try60. connection.connect();61. documentconverterconverter=newopenofficedocumentconverter(connection);62. converter.convert(docfile,pdffile);63. /closetheconnection64. connection.disconnect();65. system.out.println(*pdf转换成功,pdf输出:+pdffile.getpath()+*);66. catch(.connectexceptione)67. e.printstacktrace();68. system.out.println(*swf转换器异常,openoffice服务未启动!*);69. throwe;70. catch(com.artofsolving.jodconverter.openoffice.connection.openofficeexceptione)71. e.printstacktrace();72. system.out.println(*swf转换器异常,读取转换文件失败*);73. throwe;74. catch(exceptione)75. e.printstacktrace();76. throwe;77. 78. else79. system.out.println(*已经转换为pdf,不需要再进行转化*);80. 81. else82. system.out.println(*swf转换器异常,需要转换的文档不存在,无法转换*);83. 84. 85. 86. /*87. *转换成swf88. */89. suppresswarnings(unused)90. privatevoidpdf2swf()throwsexception91. runtimer=runtime.getruntime();92. if(!swffile.exists()93. if(pdffile.exists()94. if(environment=1)/windows环境处理95. try96. processp=r.exec(d:/programfiles/swftools/pdf2swf.exe+pdffile.getpath()+-o+swffile.getpath()+-t9);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(ioexceptione)107. e.printstacktrace();108. throwe;109. 110. elseif(environment=2)/linux环境处理111. try112. processp=r.exec(pdf2swf+pdffile.getpath()113. +-o+swffile.getpath()+-t9);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(exceptione)122. e.printstacktrace();123. throwe;124. 125. 126. else127. system.out.println(*pdf不存在,无法转换*);128. 129. else130. system.out.println(*swf已经存在不需要转换*);131. 132. 133. 134. staticstringloadstream(inputstreamin)throwsioexception135. 136. intptr=0;137. in=newbufferedinputstream(in);138. stringbufferbuffer=newstringbuffer();139. 140. while(ptr=in.read()!=-1)141. buffer.append(char)ptr);142. 143. 144. returnbuffer.tostring();145. 146. /*147. *转换主方法148. */149. suppresswarnings(unused)150. publicbooleanconver()151. 152. if(swffile.exists()153. system.out.println(*swf转换器开始工作,该文件已经转换为swf*);154. returntrue;155. 156. 157. if(environment=1)158. system.out.println(*swf转换器开始工作,当前设置运行环境windows*);159. else160. system.out.println(*swf转换器开始工作,当前设置运行环境linux*);161. 162. try163. doc2pdf();164. pdf2swf();165. catch(exceptione)166. e.printstacktrace();167. returnfalse;168. 169. 170. if(swffile.exists()171. returntrue;172. else173. returnfalse;174. 175. 176. 177. /*178. *返回文件路径179. *180. *params181. */182. publicstringgetswfpath()183. if(swffile.exists()184. stringtempstring=swffile.getpath();185. tempstring=tempstring.replaceall(,/);186. returntempstring;187. else188. return;189. 190. 191. 192. /*193. *设置输出路径194. */195. publicvoidsetoutputpath(stringoutputpath)196. this.outputpath=outputpath;197. if(!outputpath.equals()198. stringrealname=filename.substring(filename.lastindexof(/),199. filename.lastindexof(.);200. if(outputpath.charat(outputpath.length()=/)201. swffile=newfile(outputpath+realname+.swf);202. else203. swffile=newfile(outputpath+realname+.swf);204. 205. 206. 207. 208. (5)创建文档上传转换处理文件docuploadconvertaction.jsp文件htmlview plaincopy1. 3. 4. 5. 6. 7. 8. 9. %10. /文件上传采用cos组件上传,可更换为commons-fileupload上传,文件上传后,保存在upload文件夹11. /获取文件上传路径12. stringsavedirectory=application.getrealpath(/)+upload;13. /打印上传路径信息14. system.out.println(savedirectory);15. /每个文件最大50m16. intmaxpostsize=50*1024*1024;17. /采用cos缺省的命名策略,重名后加1,2,3.如果不加dfp重名将覆盖18. defaultfilerenamepolicydfp=newdefaultfilerenamepolicy();19. /response的编码为utf-8,同时采用缺省的文件名冲突解决策略,实现上传,如果不加dfp重名将覆盖20. multipartrequestmulti=newmultipartrequest(request,savedirectory,maxpostsize,utf-8,dfp);21. /multipartrequestmulti=newmultipartrequest(request,savedirectory,maxpostsize,utf-8);22. /输出反馈信息23. enumerationfiles=multi.getfilenames();24. while(files.hasmoreelements()25. system.err.println(ccc);26. stringname=(string)files.nextelement();27. filef=multi.getfile(name);28. if(f!=null)29. stringfilename=multi.getfilesystemname(name);30. /获取上传文件的扩展名31. stringextname=filename.substring(filename.lastindexof(.)+1);32. /文件全路径33. stringlastfilename=savedirectory+filename;34. /获取需要转换的文件名,将路径名中的替换为/35. stringconverfilename=savedirectory.replaceall(,/)+/+filename;36. system.out.println(converfilename);37. /调用转换类docconverter,并将需要转换的文件传递给该类的构造方法38. docconverterd=newdocconverter(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. stringswfpath=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();51. 52. 53. 54. %55. 56. 57. 58. 59. inserttitlehere60. 61. bodymargin-top:100px;background:#fff;font-family:verdana,tahoma;62. acolor:#ce4614;63. #msg-boxcolor:#ce4614;font-size:0.9em;text-align:center;64. #msg-box.logoborder-bottom:5pxsolid#ece5d9;margin-bottom:20px;padding-bottom:10px;65. #msg-box.titlefont-size:1.4em;font-weight:bold;margin:0030px0;66. #msg-box.navmargin-top:20px;67. 68. 69. 70. 71. 72. 73. 74. 75. 76. (6)创建文档预览文件documentview.jsphtmlview plaincopy1. 3. 6. 7. 8. 9. 10. 11. 12. 13. 14. html,bodyheight:100%;15. bodymargin:0;padding:0;overflow:auto;16. #flashcontentdisplay:none;17. 18. 19. 文档在线预览系统20. 21. 22. 23. 24. 25. 26. var
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 二年级品德与生活上册 找长处教学设计 泰山版
- (重庆二诊)重庆市高2025届高三学业质量调研抽测 (第二次)历史试卷(含答案)
- 人的感知与反应(教学设计)-2024-2025学年科学五年级下册人教鄂教版
- 反洗钱工作保密事项培训
- 2024北京资产管理有限公司招聘4人笔试参考题库附带答案详解
- 耳鼻喉科护理指南
- 表情管理培训方案
- 2024中铝铁矿西芒杜项目公开招聘13人笔试参考题库附带答案详解
- 工程施工员培训
- 班主任心理健康知识培训
- 垃圾中转站污水处理方案
- 河北石家庄旅游PPT介绍石家庄幻灯片模板
- 宴席设计与菜品开发第二版劳动版宴席菜肴与菜单设计课件
- 2024届高考语文复习-新高考卷文学类阅读真题《建水记》《大师》讲评
- 轴向拉压杆的强度计算
- 中考冠词专项训练100题 (带答案)
- 电力现货市场基础知识
- 公司收支明细表
- GB/T 18323-2001滑动轴承烧结轴套的尺寸和公差
- 2ttk7d6.0gd空调装置使用维护说明书法补充
- 2022年中盐东兴盐化股份有限公司招聘笔试试题及答案解析
评论
0/150
提交评论