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 ...
随机推荐
- CCF 201312-4 有趣的数 (数位DP, 状压DP, 组合数学+暴力枚举, 推公式, 矩阵快速幂)
问题描述 我们把一个数称为有趣的,当且仅当: 1. 它的数字只包含0, 1, 2, 3,且这四个数字都出现过至少一次. 2. 所有的0都出现在所有的1之前,而所有的2都出现在所有的3之前. 3. 最高 ...
- iOS多线程总结
1.不要同时开太多的线程(1~3条线程即可,不要超过5条) 2.线程概念 1> 主线程 : UI线程,显示.刷新UI界面,处理UI控件的事件 2> 子线程 : 后台线程,异步线程 3.不要 ...
- Windows 10 Edge浏览器、照片查看程序关闭“平滑滚动”
升级到10后,这两个常用软件的“平滑滚动”功能,个人感觉体验有点不好,特别是图片这个自带程序,看了几十张图后就有点头晕了,所以把它关闭为好: 控制面板\系统和安全\系统\高级系统设置\高级\性能\设置 ...
- Label & TextBlock
I always thought it was odd that WPF has both TextBlock and Label. They both are responsible for di ...
- Centos下忘记mysql的root密码的解决方法
Centos下忘记mysql的root密码的解决方法 一:(停掉正在运行的mysql) [root@NetDakVPS ~]# service mysql stop 二:使用 “--skip-gran ...
- Java中String对象的不可变性
首先看一个程序 package reverse; public class Reverse { public static void main(String[] args) { String c1=n ...
- 130712周赛(CF)
这次练习从第一题开始注定水了,1A的题目wa了3次,第三题走进了错误的思想,wa到死....其他三个题目看都没看...........赛后慢慢搞. A. Free Cash 巨水的一题,直接找出每个时 ...
- c语言操作符 “++”另类行为
正常情况下,我们使用++ int a = 1; a++; printf(“%d”,a); // 2; 很简单没什么好说的. #include <stdio.h> int main() ...
- Eclipse配置PyDev插件
安装python解释器 安装PyDev: 首先需要去Eclipse官网下载:http://www.eclipse.org/,Eclipse需要JDK支持,如果Eclipse无法正常运行,请到Java官 ...
- 广州项目实施步骤II_练习配置HaProxy的重定向负载均衡
CentOS6.4 配置Haproxy Haproxy下载地址:http://pan.baidu.com/share/link?shareid=1787182295&uk=18290183 ...