0、安装环境。可以参考http://www.cnblogs.com/star-studio/archive/2011/12/09/2281807.html     百度关键字 仿百度文库方案

1、借用开源工具flexpaper

2、使用工具类,这个类是前人都已经做好的。在此借用一下!

  

  1. import java.io.BufferedInputStream;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.net.ConnectException;
  6.  
  7. import com.artofsolving.jodconverter.DocumentConverter;
  8. import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
  9. import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException;
  10. import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
  11. import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
  12. import com.zkhy.fw.core.util.FWLogger;
  13. import com.zkhy.web.core.constants.Constants;
  14. import com.zkhy.web.core.property.ApplicationConfig;
  15.  
  16. /**
  17. * doc docx格式转换
  18. *
  19. * @see <a
  20. * href="http://www.cnblogs.com/star-studio/archive/2011/12/12/2284417.html">http://www.cnblogs.com/star-studio/archive/2011/12/12/2284417.html</a>
  21. */
  22. public class DocConverter {
  23. private static FWLogger logger = new FWLogger();
  24.  
  25. // 环境1:windows 2:linux(涉及pdf2swf路径问题)
  26. private static final int SYSTEM_OS = Integer.valueOf(ApplicationConfig.Settings.SystemOs.getValue());
  27. private String fileName;
  28. private File pdfFile;
  29. private File swfFile;
  30. private File docFile;
  31.  
  32. public DocConverter(String fileString) {
  33. ini(fileString);
  34. }
  35.  
  36. /* 重新设置 file
  37. *
  38. * @param fileString */
  39. public void setFile(String fileString) {
  40. ini(fileString);
  41. }
  42.  
  43. /* 初始化
  44. *
  45. * @param fileString */
  46. private void ini(String fileString) {
  47. fileName = fileString.substring(0, fileString.lastIndexOf("."));
  48. docFile = new File(fileString);
  49. pdfFile = new File(fileName + ".pdf");
  50. swfFile = new File(fileName + ".swf");
  51. }
  52.  
  53. /* 转为PDF
  54. *
  55. * @param file */
  56. private void doc2pdf() throws Exception {
  57. if (docFile.exists()) {
  58. if (!pdfFile.exists()) {
  59. OpenOfficeConnection connection =
  60. new SocketOpenOfficeConnection(ApplicationConfig.Settings.OpenOfficeIp.getValue(),
  61. Integer.valueOf(ApplicationConfig.Settings.OpenOfficePort.getValue()));
  62. try {
  63. connection.connect();
  64. DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
  65. converter.convert(docFile, pdfFile);
  66. // close the connection
  67. connection.disconnect();
  68. logger.info("pdf转换成功,PDF输出:" + pdfFile.getPath());
  69. } catch (ConnectException e) {
  70. logger.errorText("swf转换异常,openoffice服务未启动!");
  71. throw e;
  72. } catch (OpenOfficeException e) {
  73. logger.errorText("swf转换器异常,读取转换文件失败");
  74. throw e;
  75. } catch (Exception e) {
  76. logger.error(e);
  77. throw e;
  78. }
  79. } else {
  80. logger.info("已经转换为pdf,不需要再进行转化");
  81. }
  82. } else {
  83. logger.errorText("swf转换器异常,需要转换的文档不存在,无法转换");
  84. }
  85. }
  86.  
  87. /* 转换成swf */
  88. private void pdf2swf() throws Exception {
  89. Runtime r = Runtime.getRuntime();
  90. if (!swfFile.exists()) {
  91. if (pdfFile.exists()) {
  92. if (SYSTEM_OS == Constants.SYSTEM_OS_WINDOWS)// windows环境处理
  93. {
  94. try {
  95. System.out.println(ApplicationConfig.Settings.Pdf2SwfPath.getValue());
  96. Process p =
  97. r.exec(ApplicationConfig.Settings.Pdf2SwfPath.getValue() + " "
  98. + pdfFile.getPath() + " -o "
  99. + swfFile.getPath() + " -T 9");
  100. logger.info(loadStream(p.getInputStream()));
  101. logger.errorText(loadStream(p.getErrorStream()));
  102. logger.info(loadStream(p.getInputStream()));
  103. if (pdfFile.exists()) {
  104. pdfFile.delete();
  105. }
  106. if(swfFile.exists())//add by gjf
  107. {
  108. File contextFile = new File(Constants.swffilesPath.substring(0, Constants.swffilesPath.lastIndexOf(Constants.WEB_SEPARATOR)));
  109. if (!contextFile.exists()) {
  110. // 创建目录
  111. contextFile.mkdirs();
  112. }
  113. File fnew = new File(Constants.swffilesPath);
  114. swfFile.renameTo(fnew);//移到项目目录下
  115. swfFile.delete();
  116. logger.info("swf转换成功,文件输出:" + fnew.getPath());
  117.  
  118. } //end add
  119. } catch (Exception e) {
  120. logger.error(e);
  121. throw e;
  122. }
  123. } else if (SYSTEM_OS == Constants.SYSTEM_OS_LINUX)// linux环境处理
  124. {
  125. try {
  126. Process p = r.exec("pdf2swf " + pdfFile.getPath() + " -o " + swfFile.getPath() + " -T 9");
  127. logger.info(loadStream(p.getInputStream()));
  128. logger.errorText(loadStream(p.getErrorStream()));
  129. logger.info("swf转换成功,文件输出:" + swfFile.getPath());
  130. if (pdfFile.exists()) {
  131. pdfFile.delete();
  132. }
  133. if(swfFile.exists())//add by gjf 。。。这些是特殊处理,在转换成SWF文件时,文件名和路径中不能有空格,故需要将建好的SWF文件移到TOMCAT目录下。
  134. {
  135. File contextFile = new File(Constants.swffilesPath.substring(0, Constants.swffilesPath.lastIndexOf(Constants.WEB_SEPARATOR)));
  136. if (!contextFile.exists()) {
  137. // 创建目录
  138. contextFile.mkdirs();
  139. }
  140. File fnew = new File(Constants.swffilesPath);
  141. swfFile.renameTo(fnew);//移到项目目录下
  142. swfFile.delete();
  143. logger.info("swf转换成功,文件输出:" + fnew.getPath());
  144.  
  145. }
  146. } catch (Exception e) {
  147. logger.error(e);
  148. throw e;
  149. }
  150. }
  151. } else {
  152. logger.errorText("pdf不存在,无法转换");
  153. }
  154. } else {
  155. logger.info("swf已存在不需要转换");
  156. }
  157. }
  158.  
  159. static String loadStream(InputStream in) throws IOException {
  160. int ptr = 0;
  161. in = new BufferedInputStream(in);
  162. StringBuffer buffer = new StringBuffer();
  163.  
  164. while ((ptr = in.read()) != -1) {
  165. buffer.append((char) ptr);
  166. }
  167. return buffer.toString();
  168. }
  169.  
  170. /* 转换主方法 */
  171. public boolean conver() throws Exception {
  172. if (swfFile.exists()) {
  173. logger.info("swf转换器开始工作,该文件已经转换为swf");
  174. return true;
  175. }
  176.  
  177. if (SYSTEM_OS == Constants.SYSTEM_OS_WINDOWS) {
  178. logger.debug("swf转换器开始工作,当前设置运行环境windows");
  179. } else if (SYSTEM_OS == Constants.SYSTEM_OS_LINUX) {
  180. logger.debug("swf转换器开始工作,当前设置运行环境linux");
  181. }
  182.  
  183. try {
  184. doc2pdf();
  185. pdf2swf();
  186. } catch (Exception e) {
  187. logger.error(e);
  188. throw e;
  189. }
  190. if (swfFile.exists()) {
  191. return true;
  192. } else {
  193. return false;
  194. }
  195. }
  196.  
  197. /* 返回文件路径
  198. *
  199. * @param s */
  200. public String getSwfPath() {
  201. if (swfFile.exists()) {
  202. String tempString = swfFile.getPath();
  203. tempString = tempString.replaceAll("\\\\", "/");
  204. return tempString;
  205. } else {
  206. return "";
  207. }
  208. }
  209.  
  210. /* 设置输出路径 */
  211. public void setOutputPath(String outputPath) {
  212. if (!outputPath.equals("")) {
  213. String realName = fileName.substring(fileName.lastIndexOf("/"), fileName.lastIndexOf("."));
  214. if (outputPath.charAt(outputPath.length()) == '/') {
  215. swfFile = new File(outputPath + realName + ".swf");
  216. } else {
  217. swfFile = new File(outputPath + realName + ".swf");
  218. }
  219. }
  220. }
  221. }

3、后台调用

在考虑到多用户或者当用户并发操作时,可能会尝试系统响应缓慢,因为要多开转换进程。故这里我采用了一个单例模式,即使用一个队列,每次从来个请求放入队列,后天一个线程监视这个队列,有数据时,就转换。这样的好处,虽然速度慢了,但是减小了服务器的并发压力。

代码如下:

  1. public static Queue<SwfForm> taskQueue = null; //单例 ,SwfForm是根据具体业务,我这里存放的是PPT文件的路径和目标SWF的路径
  2.  
  3. ///调用处
  4. if (taskQueue == null) {
  5. taskQueue = new LinkedList<>();
  6. new fileThread().start();
  7. }
  8. if (!taskQueue.offer(swfForm)){// 队列已满
  9. throw new Exception("SWF任务队列已满");// 回滚事务
  10. }
  11.  
  12. //一个内部类,监视线程
  13.  
  14. class fileThread extends Thread {
  15.  
  16. @Override
  17. public void run() {
  18. while (true) {
  19. if (!taskQueue.isEmpty()) {
  20. SwfForm swfForm = taskQueue.poll();
  21. File file = new File(swfForm.getPath());
  22. try {
  23. Constants.swffilesPath =
  24. ContextLoader.getCurrentWebApplicationContext().getServletContext().getRealPath("/")
  25. + swfForm.getSwfPath();//为重命名使用。
  26. new DocConverter(file.getPath()).conver();
  27. } catch (Exception e) {
  28. taskQueue.offer(swfForm);// 失败后,仍将之前的放入队列
  29. e.printStackTrace();
  30. }
  31. }
  32. try {
  33. Thread.sleep(10000);//休眠
  34. } catch (InterruptedException e) {
  35. e.printStackTrace();
  36. }
  37. }
  38. }
  39. }

4、前台代码

  1. showSwf = function(swfFile) {
  2. if (!swfFile) {
  3. return;
  4. }
  5. $('#documentViewer').FlexPaperViewer(//div ID
  6. { config : {
  7. jsDirectory : courseware.swfDirectory,//配置文件路径
  8. SWFFile :swfFile,//要显示的SWF文件
  9. Scale : 0.6,
  10. ZoomTransition : 'easeOut',
  11. ZoomTime : 0.5,
  12. ZoomInterval : 0.2,
  13. FitPageOnLoad : true,
  14. FitWidthOnLoad : false,
  15. FullScreenAsMaxWindow : false,
  16. ProgressiveLoading : false,
  17. MinZoomSize : 0.2,
  18. MaxZoomSize : 5,
  19. SearchMatchAll : false,
  20. InitViewMode : 'Portrait',
  21. RenderingOrder : 'flash',
  22. StartAtPage : '',
  23. ViewModeToolsVisible : false,
  24. ZoomToolsVisible : true,
  25. NavToolsVisible : false,
  26. CursorToolsVisible : false,
  27. SearchToolsVisible : true,
  28. WMode : 'window',
  29. localeChain: 'zh_CN'
  30. }}
  31. );
  32.  
  33. };

5、注意事项

特别注意,这里的文件,只能是PDF ,DOC ,DOCX,PPT,PPTX,xls,xlsx等,千万不能用其它软件更改,我之前就用来WPS改了一个文件,导致我浪费了1个下午的时间。

另外需要注意,文件中,不能有空格。当然这个程序可以处理掉

flexpaper 在线观看 PPT,PDF,DOC等文档的更多相关文章

  1. java通过url在线预览Word、excel、ppt、pdf、txt文档

    java通过url在线预览Word.excel.ppt.pdf.txt文档中的内容[只获得其中的文字] 在页面上显示各种文档中的内容.在servlet中的逻辑 word: BufferedInputS ...

  2. 使用Lucene对doc、docx、pdf、txt文档进行全文检索功能的实现

    转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/76273859 本文出自[我是干勾鱼的博客] 这里讲一下使用Lucene对doc. ...

  3. 判断pdf、word文档、图片等文件类型(格式)、大小的简便方法

    判断pdf.word文档.图片等文件类型(格式).大小的简便方法 很久没发文了,今天有时间就写一下吧. 关于上传文件,通常我们都需要对其进行判断,限制上传的类型,如果是上传图片,我们甚至会把图片转化成 ...

  4. Python处理PDF和Word文档常用的方法

    Python处理PDF和Word文档的模块是PyPDF2,使用之前需要先导入. 打开一个PDF文档的操作顺序是:用open()函数打开文件并用一个变量来接收,然后把变量给传递给PdfFileReade ...

  5. flexpaper上传带中文名字的文档,在页面显示若出现404错误时,请在server.xml文件中进行编码utf-8

    flexpaper上传带中文名字的文档,在页面显示若出现404错误时,请在server.xml文件中进行编码utf-8

  6. 集成 Spring Doc 接口文档和 knife4j-SpringBoot 2.7.2 实战基础

    优雅哥 SpringBoot 2.7.2 实战基础 - 04 -集成 Spring Doc 接口文档和 knife4j 前面已经集成 MyBatis Plus.Druid 数据源,开发了 5 个接口. ...

  7. JAVA实现在线查看PDF和office文档

    一个项目中要做一个在线预览附件(和百度文库差不多)的小功能点,楼主在开发过程中踩了很多坑的同时也总结了一些方法,仅供广大猿友参考,那么要实现这个小功能,目前主要是有如下3种可行的实现方式,下面先说实现 ...

  8. iOS-打开word、ppt、pdf、execl文档方式

    这里面包括下载和打开文档的操作:需要先导入<AFNetworking>的框架 第一步:创建一个显示文档的view:ReadViewController (1).h的代码如下: @inter ...

  9. 使用OpenOffice实现各种文档转pdf或者html文档

    ---恢复内容开始--- 最近在做项目时需要写一个功能,将doc,ppt,xsl等文档做在线预览.网上查了很多资料,开始适用poi将文档转成pdf没成功,后来使用了OpenOffice4 + jodc ...

随机推荐

  1. HDU3466Proud Merchants(贪心&背包)

    http://acm.hdu.edu.cn/showproblem.php?pid=3466 题目大意是说n个物品每个物品的花费是p,但是如果你现在的钱少于q就买不了这个物品,每个物品的价值是v,求有 ...

  2. 解决UITableView头部空白

    解决方式1: self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.ta ...

  3. 用DependanceProperty做Dynamic换Icon

    1:做Icon用Canvas还是DrawingBrush? Canvas的例子:

  4. android AsyncHttpClient 开源框架的使用

    AsyncHttpClient 1.在很多时候android都需要进行网络的操作,而android自带的HttpClient可以实现,但要进行很多网络连接的时候(如:下载很多图片),就需要线程池来进行 ...

  5. 异常:exception和error的区别

    Throwable 是所有 Java 程序中错误处理的父类 ,有两种子类: Error 和 Exception .     Error :表示由 JVM 所侦测到的无法预期的错误,由于这是属于 JVM ...

  6. 全代码实现ios-2

    全代码开发ios第一个应用程序,完全像是盲人摸象. 首先,要设计这个应用,无论从界面,还是颜色搭配,以及功能,都是一个人完成. 也许,做独立开发人真的相当不容易. 因为,没有人帮忙给意见,而且,也没有 ...

  7. NISSAN 尼桑 J1962 诊断座

    J1962诊断座: J1962/16 ........ 常火线 +BATJ1962/  8 ........ 点火开关打开信号 +IGNJ1962/  5 ........ 发动机搭铁线; 逻辑地J1 ...

  8. C#常用类汇总

    一.object常用类 1.获取去类的类型和公共属性 var a= new Class A(); var props = A.GetType().GetProperties();

  9. BZOJ 2038 [2009国家集训队]小Z的袜子 莫队

    2038: [2009国家集训队]小Z的袜子(hose) 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=2038 Descriptionw ...

  10. Fox Roddick interviw Federer before 2013 US Open

    talk about  Mike Jordan , talk about Tiger Woods, their competitor when people discuss you I love wi ...