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等文档的更多相关文章

  1. java通过url在线预览Word、excel、ppt、pdf、txt文档

    java通过url在线预览Word.excel.ppt.pdf.txt文档中的内容[只获得其中的文字] 在页面上显示各种文档中的内容.在servlet中的逻辑 word: BufferedInputS ...

  2. 使用Lucene对doc、docx、pdf、txt文档进行全文检索功能的实现

    转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/76273859 本文出自[我是干勾鱼的博客] 这里讲一下使用Lucene对doc. ...

  3. 判断pdf、word文档、图片等文件类型(格式)、大小的简便方法

    判断pdf.word文档.图片等文件类型(格式).大小的简便方法 很久没发文了,今天有时间就写一下吧. 关于上传文件,通常我们都需要对其进行判断,限制上传的类型,如果是上传图片,我们甚至会把图片转化成 ...

  4. Python处理PDF和Word文档常用的方法

    Python处理PDF和Word文档的模块是PyPDF2,使用之前需要先导入. 打开一个PDF文档的操作顺序是:用open()函数打开文件并用一个变量来接收,然后把变量给传递给PdfFileReade ...

  5. flexpaper上传带中文名字的文档,在页面显示若出现404错误时,请在server.xml文件中进行编码utf-8

    flexpaper上传带中文名字的文档,在页面显示若出现404错误时,请在server.xml文件中进行编码utf-8

  6. 集成 Spring Doc 接口文档和 knife4j-SpringBoot 2.7.2 实战基础

    优雅哥 SpringBoot 2.7.2 实战基础 - 04 -集成 Spring Doc 接口文档和 knife4j 前面已经集成 MyBatis Plus.Druid 数据源,开发了 5 个接口. ...

  7. JAVA实现在线查看PDF和office文档

    一个项目中要做一个在线预览附件(和百度文库差不多)的小功能点,楼主在开发过程中踩了很多坑的同时也总结了一些方法,仅供广大猿友参考,那么要实现这个小功能,目前主要是有如下3种可行的实现方式,下面先说实现 ...

  8. iOS-打开word、ppt、pdf、execl文档方式

    这里面包括下载和打开文档的操作:需要先导入<AFNetworking>的框架 第一步:创建一个显示文档的view:ReadViewController (1).h的代码如下: @inter ...

  9. 使用OpenOffice实现各种文档转pdf或者html文档

    ---恢复内容开始--- 最近在做项目时需要写一个功能,将doc,ppt,xsl等文档做在线预览.网上查了很多资料,开始适用poi将文档转成pdf没成功,后来使用了OpenOffice4 + jodc ...

随机推荐

  1. CodeForces 710E Generate a String (DP)

    题意:给定 n,x,y,表示你要建立一个长度为 n的字符串,如果你加一个字符要花费 x时间,如果你复制前面的字符要花费y时间,问你最小时间. 析:这个题,很明显的DP,dp[i]表示长度为 i 的字符 ...

  2. GC: 垃圾回收算法

    标记-清除算法标记-清除(Mark-Sweep)算法是最基础的算法,就如它的名字一样,算法分为“标记”和“清除”两个阶段:首先标记出所有需要回收的对象,在标记完成后统一回收掉所有被标记的对象.之所以说 ...

  3. UserControl和CustomControl基础【PluraSight】

    UserControl UserControl实际上就是ContentControl,xaml里<UserControl></UserControl>tag之间包含的实际就是后 ...

  4. text输入框中按下enter键时阻止刷新页面

    1.在js中加入一个方法: function judge_enter(){ if(window.event.keyCode==13){ return false;//阻止页面刷新的作用 } } 2.然 ...

  5. Animated Scroll to Top

    Due to a number of requests, I'm writing a detail tutorial on how to create an animated scroll to to ...

  6. js查看浏览器类型和版本

    var Sys = {}; var ua = navigator.userAgent.toLowerCase(); var s; var scan; (s = ua.match(/msie ([\d. ...

  7. 让网站变灰的CSS代码(支持IE、FIREFOX和CHROME)

    方法1:支持IE <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xht ...

  8. SCCM2007

    Active Directory系统组发现:此方法按照上次运行发现方法时 Active Directory 中的响应返回对象,可发现活动目录OU.全局组.通用组.嵌套组.非安全组. Active Di ...

  9. windows下安装,配置gcc编译器

    在Windows下使用gcc编译器: 1.首先介绍下MinGW MinGW是指仅仅用自由软件来生成纯粹的Win32可运行文件的编译环境,它是Minimalist GNU on Windows的略称. ...

  10. C#封装、多态、抽象、接口、匿名方法等学习

    1:封装 将对象进行封装,并不等于将整个对象完全包裹起来,而是根据实际需要,设置一定的访问权限,用户根据不同的权限调用对象提供的功能,在C#语言中,可以使用修饰符public.internal.pro ...