【Java精品源码栏目提醒】:以下是网学会员为您推荐的Java精品源码-java实现类似百度文库实现pdf功能 - 其它资料,希望本篇文章对您学习有所帮助。
实现类似百度文库在线观看功能笔记 (.文件-pdf 文件-swf 文件)实现方式调研: ,发现还是蛮多资网上大致看了下实现的方式(借鉴了前人的文档,在此谢过)料的。
大体上有四种转换的方式:1. Txt/Word/Excel/PPTPDFOpenOfficeJodConverterSWFpdf2swfFlexPap er 浏览2. Txt/Word/Excel/PPTPDFMSOfficeJACOBSWFpdf2swfFlexPaper 浏览3. Txt/Word/Excel/PPTSWF FlashPaper FlexPaper 浏览4. Txt/Word/Excel/PPTSWFprint2flash FlexPaper 浏览上面 12 两种实现方式相对比较复杂,需要先将其他类型转换为 PDF 类型,再转成 SWF 类型在容器中显示。
所以先看了下后 2 种,先说说 flashpaper 吧,此款软件确实是转换格式的神器,可惜是收费的。
不过可以试用 30 天,对于这种小型的可试用的软件,我一般是装在虚拟机中,一直试用,到期了再恢复快照即可。
说实话,效果不错,但不适合开发用,因为木有买的冲动,只能忍痛割爱了!再说说 print2flash,这款软件是开源的,也能实现转换 swf 的功能,但是上网找了半天,关于它的代码资料很是匮乏。
Java 的更是凤毛麟角,无从下手,只好先晾着。
希望某位大虾有关于这个的代码资料可以共享,本人感激不尽啊!剩下的只有相对复杂的 2 种,不多说了,鉴于经典案例及网上资料的丰富性,我最终还是“屈服”在了第一种的组合上了。
资料和工具准备: 工具OpenOffice http://zh.openoffice.org/new/zh_cn/downloads.htmlJodConverter http://dldx.csdn.net/fd.phpi992314146801277s08dbee 95a6e2dda1a95aa8cbf4df197bSwftoolspdf2 http://dldx.csdn.net/fd.phpi389s2f7430swf ad3c00cca78ada8b4671a50b24FlexPaper http://flexpaper.googlecode.com/files/FlexPaper_1.4.5_ flash.zip资料方面,大致搜索了百度文库和开源中国上的一些资料,其实都大同小异。
前辈们已经为我们做了很多了,再次谢过各位分享的前辈。
项目目录及
源码:工程目录大致如下:另外,在 lib 下需要加入 JodConverter 压缩包中 lib 目录下的 jar 包,全部复制进去即可。
ConvertServlet 的 servlet 处理类代码如下:package org.gfg.test.servletimport
java.io.Fileimport
java.io.IOExceptionimport javax.servlet.ServletExceptionimport javax.servlet.http.HttpServletimport javax.servlet.http.HttpServletRequestimport javax.servlet.http.HttpServletResponseimport javax.servlet.http.HttpSessionimport com.artofsolving.jodconverter.DocumentConverterimportcom.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnectionimportcom.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnectionimportcom.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverterpublicclassConvertServletextends HttpServlet private File sourceFile //转换源文件 private File pdfFile //PDF目标文件 private File swfFile //SWF目标文件 private Runtime r publicvoid init throws ServletException sourceFile newFileG:tomcat6webappsreadonlineswfFile1.doc pdfFile newFileG:tomcat6webappsreadonlineswfFile1.pdf swfFile newFileG:tomcat6webappsreadonlineswfFilePaper.swf System.out.println第一步:生成文件对象,准备转换 publicvoid doGetHttpServletRequest requestHttpServletResponse response throws ServletException IOException doPostrequest response / The doPost method of the servlet. This method is called when a form has its tag value methodequals to post. param request the request send by the client to theserver param response the response send by the server to theclient throws ServletException if an error occurred throws IOException if an error occurred / publicvoid doPostHttpServletRequest requestHttpServletResponse response throws ServletException IOException response.setContentTypetext/html //转换成pdf文件 ifsourceFile.exists ifpdfFile.exists OpenOfficeConnection connection newSocketOpenOfficeConnection8100 try connection.connect DocumentConverter converter newOpenOfficeDocumentConverterconnection converter.convertsourceFile pdfFile pdfFile.createNewFile connection.disconnect System.out.println第二步:转换为PDF格式 路径 pdfFile.getPath catch
java.net.ConnectException e e.printStackTrace System.out.printlnOpenOffice服务未启动 throw e catchcom.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e e.printStackTrace System.out.println读取文件失败 throw e catch Exception e e.printStackTrace try throw e catch Exception e1 e1.printStackTrace else System.out.println已转换为PDF,无需再次转换 else System.out.println要转换的文件不存在 //转换成swf文件 r Runtime.getRuntime new Thread Override publicvoid run ifswfFile.exists ifpdfFile.exists try System.out.printlnE:/SWFTools/pdf2swf.exe pdfFile.getPath -o swfFile.getPath -T9 Process p r.execE:/SWFTools/pdf2swf.exe pdfFile.getPath -o swfFile.getPath -T 9 p.waitFor swfFile.createNewFile System.out.println第三步:转换为SWF格式 路径: swfFile.getPath System.out.println第si步:转换为SWF格式mingcheng: swfFile.getName// ifpdfFile.exists // pdfFile.delete// catch Exception e e.printStackTrace try throw e catch Exception e1 // TODO Auto-generated catch block e1.printStackTrace else System.out.printlnPDF文件不存在,无法转换 else System.out.println已经转为SWF文件,无需再次转换 .start HttpSession session request.getSession session.setAttributefileName swfFile.getName System.out.println我是测试:session.getAttributefileName response.sendRedirectrequest.getContextPath/readFile.jsp Readfile.Jsp 显示界面代码如下:努力加载中............要特别注意的是:FlexPaperViewer 的路径一定要正确,id 对上。
另外,可以用官方的 Paper.swf 作为测试,只需要将 SwfFile 的地址改掉即可。
大致显示效果如下:除此之外,关于一直加载无法显示的问题和无法打开自有 swf 文件的问题,据说是 adobe 的信任问题,还在解决中!测试了一下,好像生成的速度还行,就是貌似有 servlet 请求超时导致请求中断的情况!反正基本差不多了,就差优化了!就这样吧。
以上架包需要下载!根据每个架包的名字来进行搜索