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个下午的时间。

另外需要注意,文件中,不能有空格。当然这个程序可以处理掉

最新文章

  1. 使用KRPano资源分析工具一键下载全景网站切片图
  2. C#开发微信门户及应用(43)--微信各个项目模块的定义和相互关系
  3. vim vundle 安装Base16 Vim主题
  4. maven 仓库地址:
  5. .Net魔法堂:AssemblyInfo.cs文件详解
  6. MFC 构建、消亡 顺序 (一)--单文档 (SDI)
  7. Git CMD - init: Create an empty Git repository or reinitialize an existing one
  8. 读取xml文件信息
  9. OpenCV——Mat,IplImage,CvMat类型转换
  10. node.js系列(实例):原生node.js+formidable模块实现简单的文件上传
  11. bzoj 2243 [SDOI2011]染色(树链剖分+线段树合并)
  12. django xdmin使用
  13. centos查看系统信息命令
  14. [FJOI2018]领导集团问题 mulitset合并
  15. Oracle中查看所有表和字段
  16. 十年百度工作心得(月薪75k)
  17. php初中高阶段
  18. 更新ffmpeg
  19. Kotlin in Action 笔记
  20. Centos 右上角面板里没有wired network图标的问题

热门文章

  1. HDU 4596 Yet another end of the world (数学,扩展欧几里德)
  2. CSS Hack技术(一)
  3. php网站的定时事件
  4. 使用php代码如何筛选出ie6及以下的浏览器。
  5. T4模板语法
  6. Delphi Data Types
  7. C语言中结构体 自引用 和 相互引用
  8. PL/pgSQL学习笔记之九
  9. IIS配置(安装IIS、.Net、更改IIS Log目录位置)
  10. rails中的语法