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 ...
随机推荐
- How Tomcat Works(九)
本文接下来描述servlet容器是怎样管理其相关组件的生命周期的,首先本人描述一下事件监听模式,也可以称为观察者模式,该模式分为以下角色 即抽象主题角色 具体主题角色 抽象观察者角色及具体观察者角色, ...
- [置顶] 两台一级域名相同二级域名不同的服务器,怎么共享session
比如www.hongchangfirst.com和video.hongchangfirst.com两个域名,一级域名相同,二级域名不同.每个服务器运行着不同的功能模块或者不同的子系统,他们使用不同的二 ...
- JSP文件下载时文件名在ie和firefox下面文件名不一致极其超链接中文乱码的问题的改进
response.setContentType("application/octet-stream;charset=UTF-8"); fileName=java.net.URLEn ...
- python 缩进问题
200 ? "200px" : this.width)!important;} --> 介绍 在python中认为规定4个空格缩进,缩进的代码可以理解成一个块,但是使用缩进也 ...
- preventDefault stopPropagation??
棒棒哒~ event.preventDefault https://developer.mozilla.org/zh-CN/docs/Web/API/Event/preventDefault stop ...
- JavaBean简单及使用
一.JavaBean简介 JavaBean是使用Java语言开发的一个可重用的组件,在JSP的开发中可以使用JavaBean减少重复代码,使整个JSP代码的开发更简洁.JSP搭配JavaBean来使用 ...
- Windows Server Backup 备份Hypver-V VM
在Windows Server 2012中,可以通过Windows Server Backup备份Hypver-V VM.在还原时,将会还原到Hypver-V管理器中. 设置只保存2个备份副本,命令如 ...
- 【转】深入浅出REST
转自:http://www.infoq.com/cn/articles/rest-introduction 不知你是否意识到,围绕着什么才是实现异构的应用到应用通信的“正确”方式,一场争论正进行的如火 ...
- Educational Codeforces Round 7 D. Optimal Number Permutation 构造题
D. Optimal Number Permutation 题目连接: http://www.codeforces.com/contest/622/problem/D Description You ...
- Codeforces Round #311 (Div. 2) A. Ilya and Diplomas 水题
A. Ilya and Diplomas Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/557/ ...