java SpringMvc 实现文件在线预览(openoffice+swftools+flexpaper)
来源:互联网 发布:阿里巴巴淘宝城四期 编辑:程序博客网 时间:2024/06/10 01:17
项目需求:服务器接受的文件当下只能下载之后才能浏览内容,现需要后台能在线浏览到文件内容,避免繁琐无用文件下载操作. 通过几天网上资料搜索,目前免费的在线预览开发技术使用最多还是(openoffice+swftools+flexpaper),经过2天的学习,终于在本地测试成功,后在服务器搭建环境也成功上线. 1.概述 主要原理:
1.通过第三方工具openoffice,将word、excel、ppt、txt等文件转换为pdf文件
2.通过swfTools将pdf文件转换成swf格式的文件
3.通过FlexPaper文档组件在页面上进行展示
2.安装包下载
1.openoffice是Apache下的一个开放免费的文字处理软件
下载地址:Apache oppenoffice 官网下载 版本-3.4.1
2.SWFTools是一组用来处理Flash的swf文件的工具包,我们使用它将pdf文件转成swf文件!
下载地址:SWFTools官网下载 swftools-2013-04-09-1007.exe
3.FlexPaper是一个开源轻量级的在浏览器上显示各种文档的组件
下载地址:FlexPaper官网下载 版本1.4.0(可选择新版)
4.JODConverter一个Java的OpenDocument 文件转换器,在此我们只用到它的jar包
下载地址:JODCConverter下载
openoffice与SWFTools下载完直接安装就好了,openoffice需要开启相应服务,进入安装目录,进入program文件,在此处打开命令行,或者CMD选择cd到这个文件,然后输入命令soffice -headless -accept=”socket,host=127.0.0.1,port=8100;urp;” -nofirststartwizard
4、引入项目
主要用到的就是其中flexpaper_flash.js和FlexPaperViewer.swf。html页面是Demo格式,可以根据格式自定义自己的显示页面,两个swf文件是测试文件,你直接访问FlexPaperViewer.html指向的是Paper.swf.如果显示成功表示你环境搭建好了.
另外我的是maven项目,jar包是统一管理的.其他项目可以直接把下载的JODConverter文件里面的lib包导入项目中即可.
所有的相关jar包依赖如下:
<dependency> <groupId>com.artofsolving</groupId> <artifactId>jodconverter</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>com.artofsolving</groupId> <artifactId>jodconverter-cli</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>commons-cli</groupId> <artifactId>commons-cli</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.3</version> </dependency> <dependency> <groupId>org.openoffice</groupId> <artifactId>juh</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>org.openoffice</groupId> <artifactId>jurt</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>org.openoffice</groupId> <artifactId>ridl</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-jdk14</artifactId> <version>1.5.6</version> </dependency> <dependency> <groupId>org.openoffice</groupId> <artifactId>unoil</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>com.thoughtworks.xstream</groupId> <artifactId>xstream</artifactId> <version>1.3.1</version> </dependency>
下一步我们把相应的工具类引入项目:创建DocConverter.class作为我们将文件转换成PDF,然后把PDF转换成SWF格式文件,此工具类是直接将两个步骤一起执行的.代码:` 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"); //我的路径是从这里指定的,因为此处截取的是全路径+文件名字,去掉的是文件后缀,如果想指定转换到指定指定位置可以如下操作: // fileName=fileString.substring(fileString.lastIndexOf("/"),fileString.lastIndexOf("."));
// String path=”全路径”;
// docFile=new File(fileString);
// pdfFile=new File(path+fileName+”.pdf”);
// swfFile=new File(path+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 { //此处要指定你机器安装的pdf2swf.exe全路径 Process p = r.exec("D:/Program Files/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"); } } }
}
OK!接下来是显示JSP页面previewFile.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <!-- saved from url=(0014)about:internet --> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <style type="text/css" media="screen"> html, body { height:100%; } body { margin:0; padding:0; overflow:auto; } #flashContent { display:none; } </style> <script type="text/javascript" src="${pageContext.request.contextPath}/FlexPaper_1.4.0_flash/js/swfobject/swfobject.js"></script> <script type="text/javascript" src="${pageContext.request.contextPath}/FlexPaper_1.4.0_flash/js/flexpaper_flash.js"></script> <script type="text/javascript"> <!-- For version detection, set to min. required Flash Player version, or 0 (or 0.0.0), for no version detection. --> var swfVersionStr = "10.0.0"; var xiSwfUrlStr = "playerProductInstall.swf"; var flashvars = { //escape中指定的是你swf文件的路径 SwfFile : escape("${pageContext.request.contextPath}/preview${url}"), //SwfFile : escape("${url}"), Scale : 0.6, ZoomTransition : "easeOut", ZoomTime : 0.5, ZoomInterval : 0.1, FitPageOnLoad : false, FitWidthOnLoad : true, PrintEnabled : true, FullScreenAsMaxWindow : false, ProgressiveLoading : true, PrintToolsVisible : false, ViewModeToolsVisible : true, ZoomToolsVisible : true, FullScreenVisible : true, NavToolsVisible : true, CursorToolsVisible : true, SearchToolsVisible : true, localeChain: "en_US" }; var params = { } params.quality = "high"; params.bgcolor = "#ffffff"; params.allowscriptaccess = "sameDomain"; params.allowfullscreen = "true"; var attributes = {}; attributes.id = "FlexPaperViewer"; attributes.name = "FlexPaperViewer"; //此处一定要指定你项目下的FlexPaperViewer.swf路径 swfobject.embedSWF( "${pageContext.request.contextPath}/FlexPaper_1.4.0_flash/FlexPaperViewer.swf", "flashContent", "1000", "600",//指定高和宽 swfVersionStr, xiSwfUrlStr, flashvars, params, attributes); swfobject.createCSS("#flashContent", "display:block;text-align:left;"); </script> </head> <body> <div style="position:absolute;left:10px;top:10px;"> <div id="flashContent"> <p> To view this page ensure that Adobe Flash Player version 10.0.0 or greater is installed. </p> </div> </div> </body> </html>
到这里,基本配置就完成了,紧接着就是调用了`@RequestMapping(“/center/preview.do”)
public String toView(Model model,String fileName,String Url,HttpServletRequest request)
//fileName = “F:/2.docx”;
String ContextPath = Url+fileName;//文件路径+文件名
DocConverter converter = new DocConverter(ContextPath);
converter.conver();
String getswfPath = converter.getswfPath();
System.out.println(getswfPath);
model.addAttribute(“url”, getswfPath);
return “previewFile”;
}
以上在windows测试没有问题.
过程中我遇到过两种问题情况.
第一种就是路径问题,各种路径需要梳理清楚,研究一下整个过程,差不多能解决路径输入与输出问题.整个工作流程无非就是,指定一个文件的路径作为参数给转换器,转换器调用pdfconver()方法把文件转换成pdf格式,然后调用swfconver()方法将swf格式,里面夹杂一些判断,转换成swf格式之后,将swf文件路径在显示页面指定.
第二种是生成的swf文件名称夹带中文,建议用英文或者数字,至于文件名为中文的swf文件,我最后也没测试成功.干脆我生成swf的时候全生成英文和数字名称.
最后本地windows实现后,在linux安装openoffice与swftools,官网有相应的安装包,和教程,根据教程一步步安装就可以了,记住把pdf2swf命令设置为全局,另外把openoffice服务打开.把项目中的工具环境默认改为2,注意一下路径.
遇到问题一定要耐心!!!!
参考博客http://blog.csdn.net/z69183787/article/details/17468039
- java SpringMvc 实现文件在线预览(openoffice+swftools+flexpaper)
- Java实现文档在线预览(openoffice+swfTools+FlexPaper)
- Java实现文档在线预览(openoffice+swfTools+FlexPaper)
- Openoffice+swfTools+flexPaper实现在线预览
- Java基于springmvc实现文档预览(openoffice+swftools+flexpaper)
- JAVA Web项目中用OpenOffice+Swftools+Flexpaper实现在线预览,txt文件出现乱码!
- java实现文件预览(openoffice+swftools+flexpaper)
- java实现文件预览(openoffice+swftools+flexpaper)
- OpenOffice+SWFTools+ FlexPaper实现文件预览
- java实现附件预览(openoffice+swfTools+FlexPaper)
- java实现附件预览(openoffice+swfTools+FlexPaper)
- java实现附件预览(openoffice+swftools+flexpaper)
- 最详细的文件在线预览(openoffice+swftools+flexpaper)
- java实现在线预览功能 swftools+flexPaper
- 在线文档预览方案:openoffice+swftools+flexPaper
- 关于Java实现在线预览的问题总结(openoffice、SWFTools、FlexPaper)
- java实现附件预览(openoffice+swftools+flexpaper)
- java实现附件预览(openoffice+swftools+flexpaper)
- MySQL命令drop table:删除数据表
- 简单工厂模式
- RxJava 学习使用心得
- Sku算法--商城(品种,规格,参数等选择)
- MySQL命令insert into:向表中插入数据(记录)
- java SpringMvc 实现文件在线预览(openoffice+swftools+flexpaper)
- 【NOIP2016普及组】 second 枚举 date
- 2 slice2java 命令选项详解
- 左侧固定宽度,右侧自适应宽度铺满剩余屏幕
- C++:堆与栈区别
- LevelDB详解
- 文件上传
- java之JDK学习
- 实验三 基于数据包捕获的数据报结构分析