java实现文件预览(openoffice+swftools+flexpaper)
来源:互联网 发布:淘宝10000点券多少钱 编辑:程序博客网 时间:2024/06/05 11:01
做一个项目中有一个功能是实现文件预览,查阅文档之后了解到可以通(openoffice+swftools+flexpaper)来实现。
主要原理
1.通过第三方工具openoffice,将word、excel、ppt、txt等文件转换为pdf文件
2.通过swfTools将pdf文件转换成swf格式的文件
3.通过FlexPaper文档组件在页面上进行展示
工具介绍
1.openoffice是Apache下的一个开放免费的文字处理软件(我下载的版本是3.4.1)2.SWFTools是一组用来处理Flash的swf文件的工具包,我们使用它将pdf文件转成swf文件!(2013)
3.FlexPaper是一个开源轻量级的在浏览器上显示各种文档的组件(1.5.1)
4.JODConverter一个Java的OpenDocument 文件转换器,在此我们只用到它的jar包
开启openoffice,我们要使用openoffice的时候,需要将它的服务打开。可以通过命令行的方式,进入openoffice安装盘符,输入以下代码来启动服务soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
开发流程
运行 flexpaper中给的examples,如果官方给的样例可以使用的话,那么继续进行我们的开发。
要实现文件的预览,先创建转换页,一个jsp文件
<%@page import="java.io.File"%><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@page import="com.fk.preview.DocConverter"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><body><%String path = application.getRealPath("/");String converfilename = "upload\\word.doc";String filepath = path + converfilename;String fileName = filepath.replaceAll("\\\\", "/");System.out.println(fileName);DocConverter converter = new DocConverter(fileName);converter.conver();System.out.println(converter.getswfPath());session.setAttribute("swfpath", converter.getswfPath());%><form name="viewForm" id="form_swf" action="documentview.jsp"method="POST"><input type='submit' value='预览' class='BUTTON SUBMIT' /></form></body></html>
然后创建查看页,也是一个jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><% String swfFilePath=session.getAttribute("swfpath").toString();%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><script type="text/javascript" src="js/jquery.js"></script> <script type="text/javascript" src="js/flexpaper_flash.js"></script> <script type="text/javascript" src="js/flexpaper_flash_debug.js"></script> <style type="text/css" media="screen">html,body {height: 100%;}body {margin: 0;padding: 0;overflow: auto;}#flashContent {display: none;}</style><title>文档在线预览系统</title></head><body><div style="position:absolute;left:10px;top:10px;"><a id="viewerPlaceHolder"style="width:680px;height:480px;display:block"></a> <script type="text/javascript">var fp = new FlexPaperViewer('FlexPaperViewer','viewerPlaceHolder', {config : {SwfFile : '<%=swfFilePath%>',Scale : 0.6,ZoomTransition : 'easeOut',ZoomTime : 0.5,ZoomInterval : 0.2,FitPageOnLoad : false,FitWidthOnLoad : false,FullScreenAsMaxWindow : false,ProgressiveLoading : false,MinZoomSize : 0.2,MaxZoomSize : 5,SearchMatchAll : false,InitViewMode : 'Portrait',ViewModeToolsVisible : true,ZoomToolsVisible : true,NavToolsVisible : true,CursorToolsVisible : true,SearchToolsVisible : true,localeChain : 'en_US'}});</script></div></body></html>
在转换页中用到了一个转换类,
package com.fk.preview;import java.io.BufferedInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import com.artofsolving.jodconverter.DocumentConverter; import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection; import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection; import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter; /** * doc docx格式转换 */ public class DocConverter { private static final int environment = 1;// 环境 1:windows 2:linux private String fileString;// (只涉及pdf2swf路径问题) private String outputPath = "";// 输出路径 ,如果不设置就输出在默认的位置 private String fileName; private File pdfFile; private File swfFile; private File docFile; public DocConverter(String fileString) { ini(fileString); } /** * 重新设置file * * @param fileString */ public void setFile(String fileString) { ini(fileString); } /** * 初始化 * * @param fileString */ private void ini(String fileString) { this.fileString = fileString; fileName = fileString.substring(0, fileString.lastIndexOf(".")); docFile = new File(fileString); pdfFile = new File(fileName + ".pdf"); swfFile = new File(fileName + ".swf"); } /** * 转为PDF * * @param file */ private void doc2pdf() throws Exception { if (docFile.exists()) { if (!pdfFile.exists()) { OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100); try { connection.connect(); DocumentConverter converter = new OpenOfficeDocumentConverter(connection); converter.convert(docFile, pdfFile); // close the connection connection.disconnect(); System.out.println("****pdf转换成功,PDF输出:" + pdfFile.getPath()+ "****"); } catch (java.net.ConnectException e) { e.printStackTrace(); System.out.println("****swf转换器异常,openoffice服务未启动!****"); throw e; } catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) { e.printStackTrace(); System.out.println("****swf转换器异常,读取转换文件失败****"); throw e; } catch (Exception e) { e.printStackTrace(); throw e; } } else { System.out.println("****已经转换为pdf,不需要再进行转化****"); } } else { System.out.println("****swf转换器异常,需要转换的文档不存在,无法转换****"); } } /** * 转换成 swf */ @SuppressWarnings("unused") private void pdf2swf() throws Exception { Runtime r = Runtime.getRuntime(); if (!swfFile.exists()) { if (pdfFile.exists()) { if (environment == 1) {// windows环境处理 try { Process p = r.exec("D:/swftools/pdf2swf.exe "+ pdfFile.getPath() + " -o "+ swfFile.getPath() + " -T 9"); System.out.print(loadStream(p.getInputStream())); System.err.print(loadStream(p.getErrorStream())); System.out.print(loadStream(p.getInputStream())); System.err.println("****swf转换成功,文件输出:" + swfFile.getPath() + "****"); if (pdfFile.exists()) { pdfFile.delete(); } } catch (IOException e) { e.printStackTrace(); throw e; } } else if (environment == 2) {// linux环境处理 try { Process p = r.exec("pdf2swf " + pdfFile.getPath() + " -o " + swfFile.getPath() + " -T 9"); System.out.print(loadStream(p.getInputStream())); System.err.print(loadStream(p.getErrorStream())); System.err.println("****swf转换成功,文件输出:" + swfFile.getPath() + "****"); if (pdfFile.exists()) { pdfFile.delete(); } } catch (Exception e) { e.printStackTrace(); throw e; } } } else { System.out.println("****pdf不存在,无法转换****"); } } else { System.out.println("****swf已经存在不需要转换****"); } } static String loadStream(InputStream in) throws IOException { int ptr = 0; in = new BufferedInputStream(in); StringBuffer buffer = new StringBuffer(); while ((ptr = in.read()) != -1) { buffer.append((char) ptr); } return buffer.toString(); } /** * 转换主方法 */ @SuppressWarnings("unused") public boolean conver() { if (swfFile.exists()) { System.out.println("****swf转换器开始工作,该文件已经转换为swf****"); return true; } if (environment == 1) { System.out.println("****swf转换器开始工作,当前设置运行环境windows****"); } else { System.out.println("****swf转换器开始工作,当前设置运行环境linux****"); } try { doc2pdf(); pdf2swf(); } catch (Exception e) { e.printStackTrace(); return false; } if (swfFile.exists()) { return true; } else { return false; } } /** * 返回文件路径 * * @param s */ public String getswfPath() { if (swfFile.exists()) { String tempString = swfFile.getPath(); tempString = tempString.replaceAll("\\\\", "/"); return tempString; } else { return ""; } } /** * 设置输出路径 */ public void setOutputPath(String outputPath) { this.outputPath = outputPath; if (!outputPath.equals("")) { String realName = fileName.substring(fileName.lastIndexOf("/"), fileName.lastIndexOf(".")); if (outputPath.charAt(outputPath.length()) == '/') { swfFile = new File(outputPath + realName + ".swf"); } else { swfFile = new File(outputPath + realName + ".swf"); } } } }只要把转换文件的路径按自己的方式设置正确,就可以成功转换文件。通过转换页把生成的swf文件的路径放到session中传给查看页,就可以在线预览文件了。
0 0
- java实现文件预览(openoffice+swftools+flexpaper)
- java实现文件预览(openoffice+swftools+flexpaper)
- java SpringMvc 实现文件在线预览(openoffice+swftools+flexpaper)
- OpenOffice+SWFTools+ FlexPaper实现文件预览
- java实现附件预览(openoffice+swfTools+FlexPaper)
- java实现附件预览(openoffice+swfTools+FlexPaper)
- java实现附件预览(openoffice+swftools+flexpaper)
- java实现附件预览(openoffice+swftools+flexpaper)
- java实现附件预览(openoffice+swftools+flexpaper)
- java实现附件预览(openoffice+swftools+flexpaper)
- java实现附件预览(openoffice+swftools+flexpaper)
- java实现附件预览(openoffice+swftools+flexpaper)
- Java基于springmvc实现文档预览(openoffice+swftools+flexpaper)
- java实现附件预览(openoffice+swftools+flexpaper)
- java实现附件预览(openoffice+swftools+flexpaper)
- java实现附件预览(openoffice+swftools+flexpaper)
- java实现附件预览(openoffice+swftools+flexpaper)(解决jsp读取全盘文件问题)
- Java实现文档在线预览(openoffice+swfTools+FlexPaper)
- Unity中自带时间函数的执行顺序(全部完整版)
- 好文记载
- 微信用户昵称乱码解决
- 蓝桥杯 - 算法训练 - ALGO-2 最大最小公倍数
- hdoj-1015-Safecracker--dfs--细心(剪枝要准确, 要么不剪枝, 别留下剪枝后遗症~)
- java实现文件预览(openoffice+swftools+flexpaper)
- 114. Flatten Binary Tree to Linked List
- 100万并发连接服务器笔记之测试端就绪
- Java中,jsp文件编译之后部署显示中文乱码
- maven project
- LeetCode minDepth of 2 bin tree
- 在ubuntu上使用交叉编译工具链编译arm64版busybox
- 批量启动\停止ORACLE服务
- 100万并发连接服务器笔记之1M并发连接目标达成