flexpaper 在线观看 PPT,PDF,DOC等文档
0、安装环境。可以参考http://www.cnblogs.com/star-studio/archive/2011/12/09/2281807.html 百度关键字 仿百度文库方案
1、借用开源工具flexpaper
2、使用工具类,这个类是前人都已经做好的。在此借用一下!
- import java.io.BufferedInputStream;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.ConnectException;
- import com.artofsolving.jodconverter.DocumentConverter;
- import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
- import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException;
- import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
- import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
- import com.zkhy.fw.core.util.FWLogger;
- import com.zkhy.web.core.constants.Constants;
- import com.zkhy.web.core.property.ApplicationConfig;
- /**
- * doc docx格式转换
- *
- * @see <a
- * 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>
- */
- public class DocConverter {
- private static FWLogger logger = new FWLogger();
- // 环境1:windows 2:linux(涉及pdf2swf路径问题)
- private static final int SYSTEM_OS = Integer.valueOf(ApplicationConfig.Settings.SystemOs.getValue());
- 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) {
- 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(ApplicationConfig.Settings.OpenOfficeIp.getValue(),
- Integer.valueOf(ApplicationConfig.Settings.OpenOfficePort.getValue()));
- try {
- connection.connect();
- DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
- converter.convert(docFile, pdfFile);
- // close the connection
- connection.disconnect();
- logger.info("pdf转换成功,PDF输出:" + pdfFile.getPath());
- } catch (ConnectException e) {
- logger.errorText("swf转换异常,openoffice服务未启动!");
- throw e;
- } catch (OpenOfficeException e) {
- logger.errorText("swf转换器异常,读取转换文件失败");
- throw e;
- } catch (Exception e) {
- logger.error(e);
- throw e;
- }
- } else {
- logger.info("已经转换为pdf,不需要再进行转化");
- }
- } else {
- logger.errorText("swf转换器异常,需要转换的文档不存在,无法转换");
- }
- }
- /* 转换成swf */
- private void pdf2swf() throws Exception {
- Runtime r = Runtime.getRuntime();
- if (!swfFile.exists()) {
- if (pdfFile.exists()) {
- if (SYSTEM_OS == Constants.SYSTEM_OS_WINDOWS)// windows环境处理
- {
- try {
- System.out.println(ApplicationConfig.Settings.Pdf2SwfPath.getValue());
- Process p =
- r.exec(ApplicationConfig.Settings.Pdf2SwfPath.getValue() + " "
- + pdfFile.getPath() + " -o "
- + swfFile.getPath() + " -T 9");
- logger.info(loadStream(p.getInputStream()));
- logger.errorText(loadStream(p.getErrorStream()));
- logger.info(loadStream(p.getInputStream()));
- if (pdfFile.exists()) {
- pdfFile.delete();
- }
- if(swfFile.exists())//add by gjf
- {
- File contextFile = new File(Constants.swffilesPath.substring(0, Constants.swffilesPath.lastIndexOf(Constants.WEB_SEPARATOR)));
- if (!contextFile.exists()) {
- // 创建目录
- contextFile.mkdirs();
- }
- File fnew = new File(Constants.swffilesPath);
- swfFile.renameTo(fnew);//移到项目目录下
- swfFile.delete();
- logger.info("swf转换成功,文件输出:" + fnew.getPath());
- } //end add
- } catch (Exception e) {
- logger.error(e);
- throw e;
- }
- } else if (SYSTEM_OS == Constants.SYSTEM_OS_LINUX)// linux环境处理
- {
- try {
- Process p = r.exec("pdf2swf " + pdfFile.getPath() + " -o " + swfFile.getPath() + " -T 9");
- logger.info(loadStream(p.getInputStream()));
- logger.errorText(loadStream(p.getErrorStream()));
- logger.info("swf转换成功,文件输出:" + swfFile.getPath());
- if (pdfFile.exists()) {
- pdfFile.delete();
- }
- if(swfFile.exists())//add by gjf 。。。这些是特殊处理,在转换成SWF文件时,文件名和路径中不能有空格,故需要将建好的SWF文件移到TOMCAT目录下。
- {
- File contextFile = new File(Constants.swffilesPath.substring(0, Constants.swffilesPath.lastIndexOf(Constants.WEB_SEPARATOR)));
- if (!contextFile.exists()) {
- // 创建目录
- contextFile.mkdirs();
- }
- File fnew = new File(Constants.swffilesPath);
- swfFile.renameTo(fnew);//移到项目目录下
- swfFile.delete();
- logger.info("swf转换成功,文件输出:" + fnew.getPath());
- }
- } catch (Exception e) {
- logger.error(e);
- throw e;
- }
- }
- } else {
- logger.errorText("pdf不存在,无法转换");
- }
- } else {
- logger.info("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();
- }
- /* 转换主方法 */
- public boolean conver() throws Exception {
- if (swfFile.exists()) {
- logger.info("swf转换器开始工作,该文件已经转换为swf");
- return true;
- }
- if (SYSTEM_OS == Constants.SYSTEM_OS_WINDOWS) {
- logger.debug("swf转换器开始工作,当前设置运行环境windows");
- } else if (SYSTEM_OS == Constants.SYSTEM_OS_LINUX) {
- logger.debug("swf转换器开始工作,当前设置运行环境linux");
- }
- try {
- doc2pdf();
- pdf2swf();
- } catch (Exception e) {
- logger.error(e);
- throw e;
- }
- 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) {
- 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");
- }
- }
- }
- }
3、后台调用
在考虑到多用户或者当用户并发操作时,可能会尝试系统响应缓慢,因为要多开转换进程。故这里我采用了一个单例模式,即使用一个队列,每次从来个请求放入队列,后天一个线程监视这个队列,有数据时,就转换。这样的好处,虽然速度慢了,但是减小了服务器的并发压力。
代码如下:
- public static Queue<SwfForm> taskQueue = null; //单例 ,SwfForm是根据具体业务,我这里存放的是PPT文件的路径和目标SWF的路径
- ///调用处
- if (taskQueue == null) {
- taskQueue = new LinkedList<>();
- new fileThread().start();
- }
- if (!taskQueue.offer(swfForm)){// 队列已满
- throw new Exception("SWF任务队列已满");// 回滚事务
- }
- //一个内部类,监视线程
- class fileThread extends Thread {
- @Override
- public void run() {
- while (true) {
- if (!taskQueue.isEmpty()) {
- SwfForm swfForm = taskQueue.poll();
- File file = new File(swfForm.getPath());
- try {
- Constants.swffilesPath =
- ContextLoader.getCurrentWebApplicationContext().getServletContext().getRealPath("/")
- + swfForm.getSwfPath();//为重命名使用。
- new DocConverter(file.getPath()).conver();
- } catch (Exception e) {
- taskQueue.offer(swfForm);// 失败后,仍将之前的放入队列
- e.printStackTrace();
- }
- }
- try {
- Thread.sleep(10000);//休眠
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
4、前台代码
- showSwf = function(swfFile) {
- if (!swfFile) {
- return;
- }
- $('#documentViewer').FlexPaperViewer(//div ID
- { config : {
- jsDirectory : courseware.swfDirectory,//配置文件路径
- SWFFile :swfFile,//要显示的SWF文件
- Scale : 0.6,
- ZoomTransition : 'easeOut',
- ZoomTime : 0.5,
- ZoomInterval : 0.2,
- FitPageOnLoad : true,
- FitWidthOnLoad : false,
- FullScreenAsMaxWindow : false,
- ProgressiveLoading : false,
- MinZoomSize : 0.2,
- MaxZoomSize : 5,
- SearchMatchAll : false,
- InitViewMode : 'Portrait',
- RenderingOrder : 'flash',
- StartAtPage : '',
- ViewModeToolsVisible : false,
- ZoomToolsVisible : true,
- NavToolsVisible : false,
- CursorToolsVisible : false,
- SearchToolsVisible : true,
- WMode : 'window',
- localeChain: 'zh_CN'
- }}
- );
- };
5、注意事项
特别注意,这里的文件,只能是PDF ,DOC ,DOCX,PPT,PPTX,xls,xlsx等,千万不能用其它软件更改,我之前就用来WPS改了一个文件,导致我浪费了1个下午的时间。
另外需要注意,文件中,不能有空格。当然这个程序可以处理掉
flexpaper 在线观看 PPT,PDF,DOC等文档的更多相关文章
- java通过url在线预览Word、excel、ppt、pdf、txt文档
java通过url在线预览Word.excel.ppt.pdf.txt文档中的内容[只获得其中的文字] 在页面上显示各种文档中的内容.在servlet中的逻辑 word: BufferedInputS ...
- 使用Lucene对doc、docx、pdf、txt文档进行全文检索功能的实现
转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/76273859 本文出自[我是干勾鱼的博客] 这里讲一下使用Lucene对doc. ...
- 判断pdf、word文档、图片等文件类型(格式)、大小的简便方法
判断pdf.word文档.图片等文件类型(格式).大小的简便方法 很久没发文了,今天有时间就写一下吧. 关于上传文件,通常我们都需要对其进行判断,限制上传的类型,如果是上传图片,我们甚至会把图片转化成 ...
- Python处理PDF和Word文档常用的方法
Python处理PDF和Word文档的模块是PyPDF2,使用之前需要先导入. 打开一个PDF文档的操作顺序是:用open()函数打开文件并用一个变量来接收,然后把变量给传递给PdfFileReade ...
- flexpaper上传带中文名字的文档,在页面显示若出现404错误时,请在server.xml文件中进行编码utf-8
flexpaper上传带中文名字的文档,在页面显示若出现404错误时,请在server.xml文件中进行编码utf-8
- 集成 Spring Doc 接口文档和 knife4j-SpringBoot 2.7.2 实战基础
优雅哥 SpringBoot 2.7.2 实战基础 - 04 -集成 Spring Doc 接口文档和 knife4j 前面已经集成 MyBatis Plus.Druid 数据源,开发了 5 个接口. ...
- JAVA实现在线查看PDF和office文档
一个项目中要做一个在线预览附件(和百度文库差不多)的小功能点,楼主在开发过程中踩了很多坑的同时也总结了一些方法,仅供广大猿友参考,那么要实现这个小功能,目前主要是有如下3种可行的实现方式,下面先说实现 ...
- iOS-打开word、ppt、pdf、execl文档方式
这里面包括下载和打开文档的操作:需要先导入<AFNetworking>的框架 第一步:创建一个显示文档的view:ReadViewController (1).h的代码如下: @inter ...
- 使用OpenOffice实现各种文档转pdf或者html文档
---恢复内容开始--- 最近在做项目时需要写一个功能,将doc,ppt,xsl等文档做在线预览.网上查了很多资料,开始适用poi将文档转成pdf没成功,后来使用了OpenOffice4 + jodc ...
随机推荐
- HDU3466Proud Merchants(贪心&背包)
http://acm.hdu.edu.cn/showproblem.php?pid=3466 题目大意是说n个物品每个物品的花费是p,但是如果你现在的钱少于q就买不了这个物品,每个物品的价值是v,求有 ...
- 解决UITableView头部空白
解决方式1: self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.ta ...
- 用DependanceProperty做Dynamic换Icon
1:做Icon用Canvas还是DrawingBrush? Canvas的例子:
- android AsyncHttpClient 开源框架的使用
AsyncHttpClient 1.在很多时候android都需要进行网络的操作,而android自带的HttpClient可以实现,但要进行很多网络连接的时候(如:下载很多图片),就需要线程池来进行 ...
- 异常:exception和error的区别
Throwable 是所有 Java 程序中错误处理的父类 ,有两种子类: Error 和 Exception . Error :表示由 JVM 所侦测到的无法预期的错误,由于这是属于 JVM ...
- 全代码实现ios-2
全代码开发ios第一个应用程序,完全像是盲人摸象. 首先,要设计这个应用,无论从界面,还是颜色搭配,以及功能,都是一个人完成. 也许,做独立开发人真的相当不容易. 因为,没有人帮忙给意见,而且,也没有 ...
- NISSAN 尼桑 J1962 诊断座
J1962诊断座: J1962/16 ........ 常火线 +BATJ1962/ 8 ........ 点火开关打开信号 +IGNJ1962/ 5 ........ 发动机搭铁线; 逻辑地J1 ...
- C#常用类汇总
一.object常用类 1.获取去类的类型和公共属性 var a= new Class A(); var props = A.GetType().GetProperties();
- BZOJ 2038 [2009国家集训队]小Z的袜子 莫队
2038: [2009国家集训队]小Z的袜子(hose) 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=2038 Descriptionw ...
- 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 ...