行云间 2013-03-28
转载于:http://blog.csdn.net/hil2000/article/details/8459940,作者:hil2000
笔者最近在给客户开发文档管理系统时,客户要求上传到管理系统的文档(包括ppt,word,excel,txt)只能预览不允许下载。笔者想到了百度文库和豆丁网,百度文库和豆丁网的在线预览都是利用flash来播放文档的,在网上查阅了大量资料,终于实现了该项功能,现将自己的设计和实现整理如下。
一、如何将文档转成flash支持的swf文件实现在线播放?
1.先用openOffice把ppt、word、excel、txt类型的文档转换成pdf
2.用swftools将pdf转换成swf,然后利用FlexPaper插件实现在线播放预览。
二、具体实现
1.安装必备工具组件
(1)安装openoffice,openoffice是开源免费的文字处理软件,它可以将office文档转成pdf文件(笔者安装到D:\ProgramFiles),openOffice下载地址http://www.openoffice.org/download/index.html
(2)安装完openoffice后必须启动其server,以命令行方式启动openoffice server。进入cmd命令行提示符D:\Program Files\OpenOffice.org 3\program\
键入如下命令:
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" –nofirststartwizard
进入windows任务管理器查看有个进程soffice.bin,说明openoffice启动成功!
(3)安装swfTools(安装到 D:\Program Files)swftools作用是将pdf转换为swf文件以便flexpaper播放。下载地址:http://www.swftools.org/download.html
(4)下载flexpaper,下载地址:http://flexpaper.devaldi.com/download/
笔者下载的是FlexPaper_1.5.1,下载后将其解压备用。
(5)下载OpenDocument文档转换器 JODConverter,JODConverter是一个java的OpenDucument文件转换器,可以进行许多文件格式的转换,它利用OpenOffice来进行转换工作,它能进行以下的转换工作:
a.Microsoft Office格式转换为OpenDucument,以及OpenDucument转换为Microsoft Office
b.OpenDucument转换为PDF,Word、Excel、PowerPoint转换为PDF,RTF转换为PDF等。
下载地址:http://sourceforge.net/projects/jodconverter/files/
我们后面开发主要用它的jodconverter-2.2.2.jar包
2.软件开发过程
(1)启动Eclipse,新建web项目名称为ctcesims
(2)将上面第4步解压的flexpaper文件中的js文件夹(包含了flexpaper_flash_debug.js,flexpaper_flash.js,jquery.js,这三个js文件主要是预览swf文件的插件)拷贝至网站根目录;将FlexPaperViewer.swf拷贝至网站根目录下(该文件主要是用在网页中播放swf文件的播放器),目录结构如下图
(3)创建documentUpload.jsp文件
packagecom.cectsims.util;
importjava.io.BufferedInputStream;
importjava.io.File;
importjava.io.IOException;
importjava.io.InputStream;
importcom.artofsolving.jodconverter.DocumentConverter;
importcom.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
importcom.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
importcom.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
/**
*docdocx格式转换
*/
publicclassDocConverter{
privatestaticfinalintenvironment=1;//环境1:windows2:linux
privateStringfileString;//(只涉及pdf2swf路径问题)
privateStringoutputPath="";//输入路径,如果不设置就输出在默认的位置
privateStringfileName;
privateFilepdfFile;
privateFileswfFile;
privateFiledocFile;
publicDocConverter(StringfileString){
ini(fileString);
}
/**
*重新设置file
*
*@paramfileString
*/
publicvoidsetFile(StringfileString){
ini(fileString);
}
/**
*初始化
*
*@paramfileString
*/
privatevoidini(StringfileString){
this.fileString=fileString;
fileName=fileString.substring(0,fileString.lastIndexOf("."));
docFile=newFile(fileString);
pdfFile=newFile(fileName+".pdf");
swfFile=newFile(fileName+".swf");
}
/**
*转为PDF
*
*@paramfile
*/
privatevoiddoc2pdf()throwsException{
if(docFile.exists()){
if(!pdfFile.exists()){
OpenOfficeConnectionconnection=newSocketOpenOfficeConnection(8100);
try{
connection.connect();
DocumentConverterconverter=newOpenOfficeDocumentConverter(connection);
converter.convert(docFile,pdfFile);
//closetheconnection
connection.disconnect();
System.out.println("****pdf转换成功,PDF输出:"+pdfFile.getPath()+"****");
}catch(java.net.ConnectExceptione){
e.printStackTrace();
System.out.println("****swf转换器异常,openoffice服务未启动!****");
throwe;
}catch(com.artofsolving.jodconverter.openoffice.connection.OpenOfficeExceptione){
e.printStackTrace();
System.out.println("****swf转换器异常,读取转换文件失败****");
throwe;
}catch(Exceptione){
e.printStackTrace();
throwe;
}
}else{
System.out.println("****已经转换为pdf,不需要再进行转化****");
}
}else{
System.out.println("****swf转换器异常,需要转换的文档不存在,无法转换****");
}
}
/**
*转换成swf
*/
@SuppressWarnings("unused")
privatevoidpdf2swf()throwsException{
Runtimer=Runtime.getRuntime();
if(!swfFile.exists()){
if(pdfFile.exists()){
if(environment==1){//windows环境处理
try{
Processp=r.exec("D:/ProgramFiles/SWFTools/pdf2swf.exe"+pdfFile.getPath()+"-o"+swfFile.getPath()+"-T9");
System.out.print(loadStream(p.getInputStream()));
System.err.print(loadStream(p.getErrorStream()));
System.out.print(loadStream(p.getInputStream()));
System.err.println("****swf转换成功,文件输出:"
+swfFile.getPath()+"****");
if(pdfFile.exists()){
pdfFile.delete();
}
}catch(IOExceptione){
e.printStackTrace();
throwe;
}
}elseif(environment==2){//linux环境处理
try{
Processp=r.exec("pdf2swf"+pdfFile.getPath()
+"-o"+swfFile.getPath()+"-T9");
System.out.print(loadStream(p.getInputStream()));
System.err.print(loadStream(p.getErrorStream()));
System.err.println("****swf转换成功,文件输出:"
+swfFile.getPath()+"****");
if(pdfFile.exists()){
pdfFile.delete();
}
}catch(Exceptione){
e.printStackTrace();
throwe;
}
}
}else{
System.out.println("****pdf不存在,无法转换****");
}
}else{
System.out.println("****swf已经存在不需要转换****");
}
}
staticStringloadStream(InputStreamin)throwsIOException{
intptr=0;
in=newBufferedInputStream(in);
StringBufferbuffer=newStringBuffer();
while((ptr=in.read())!=-1){
buffer.append((char)ptr);
}
returnbuffer.toString();
}
/**
*转换主方法
*/
@SuppressWarnings("unused")
publicbooleanconver(){
if(swfFile.exists()){
System.out.println("****swf转换器开始工作,该文件已经转换为swf****");
returntrue;
}
if(environment==1){
System.out.println("****swf转换器开始工作,当前设置运行环境windows****");
}else{
System.out.println("****swf转换器开始工作,当前设置运行环境linux****");
}
try{
doc2pdf();
pdf2swf();
}catch(Exceptione){
e.printStackTrace();
returnfalse;
}
if(swfFile.exists()){
returntrue;
}else{
returnfalse;
}
}
/**
*返回文件路径
*
*@params
*/
publicStringgetswfPath(){
if(swfFile.exists()){
StringtempString=swfFile.getPath();
tempString=tempString.replaceAll("\\\\","/");
returntempString;
}else{
return"";
}
}
/**
*设置输出路径
*/
publicvoidsetOutputPath(StringoutputPath){
this.outputPath=outputPath;
if(!outputPath.equals("")){
StringrealName=fileName.substring(fileName.lastIndexOf("/"),
fileName.lastIndexOf("."));
if(outputPath.charAt(outputPath.length())=='/'){
swfFile=newFile(outputPath+realName+".swf");
}else{
swfFile=newFile(outputPath+realName+".swf");
}
}
}
}(5)创建文档上传转换处理文件docUploadConvertAction.jsp文件
(6)创建文档预览文件documentView.jsp
7.启动tomcat,部署当前web应用
8.地址栏输入http://localhost:8080/ctcesims/documentUpload.jsp 如下图:

单击选择文件,选择您要上传的文档,笔者选择的是lunwen.doc文件,然后单击上传,处理完成后,打印如下信息,如下图所示:
单击预览按钮,就会生成预览界面,如下图:
9.若出现swf无法预览,请访问
http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04a.html#119065将生成swf的文件夹设置为信任文件位置。
由于时间仓促,以上只是简单示例,很多情况还需要进一步考虑!
参考资料:
http://blog.csdn.net/iamduoluo/article/details/6727031
http://www.cnblogs.com/star-studio/archive/2011/12/09/2282411.html#2497889