在线阅读实现(文档-->swf的转换)
来源:互联网 发布:用vb进行分类汇总 编辑:程序博客网 时间:2024/04/29 17:47
在线阅读技术整理(各类office文档-->swf的转换)
一:项目前资料准备(下载所需要的工具),这里我选择的实现方式是:
Txt/Word/Excel/PPT=>PDF(OpenOffice+JodConverter)=>SWF(pdf2swf)=>FlexPaper浏览
二.功能所需工具
下载工具
OpenOffice
http://zh.openoffice.org/new/zh_cn/downloads.html
JodConverter
http://download.csdn.net/detail/winy_lm/8103089
Swftools(pdf2swf)
http://download.csdn.net/detail/winy_lm/8103121
xpdf-chinese-simplified.tar.gz (pdf2swf的中文语言包)
http://download.csdn.net/detail/winy_lm/8103361
FlexPaper
http://download.csdn.net/detail/winy_lm/8103151
三.环境搭建与实现
第一步:安装OpenOffice。从上述下载地址得到可执行安装文件,直接双击执行,安装过程请选择系统默认安装,即安装在(c:\Program Files (x86)下),注意:当安装在其他目录时,调用windows命令窗口启动服务时,会出问题!。此处注意下安装路径,文件转换之前需在Windows命令行窗口打开安装根目录,然后执行开启服务命令。
服务启动命令:
Cmd命令窗口:第一步:cd c:\Program Files (x86)\OpenOffice 4\program
第二步:soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
其中(-nofirststartwizard)的作用是cmd命令启动服务的时候,跳过在OpenOffice手动双击启动时的注册画面!
Linux中(host:服务器ip):
/opt/OpenOffice 4/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &
第二步:解压JodConverter。将lib文件夹中的jar包复制到Web工程的WebRoot/WEB-INF/lib下。(工程中已有的不需要复制),然后build path。
第三步:安装Swftools。从下载的压缩包中解压得到可执行安装文件,直接双击执行。特别注意:SWFTools的安装路径不能出现空格,正确格式如(d:\SWFTools)。该转换工具用来将pdf文件转换成swf文件。转换命令将在下面代码中特别指明。
第四步:使用Flexpaper。Flexpaper就是一个播放swf文件的播放器(重点是几个js文件)。解压后把js文件copy到项目所需的路径下即可。在JSP中引用。(本项目中是点击明细行显示在线阅读,所以实现方式是通过流的形式返回结果,原理同图片的显示)
@RequestMapping("readSwfFile")
public void readSwfFile(HttpServletRequest re, HttpServletResponse response, String file_addr){
// 从缓存中换取到上传到服务器的文件存放地址 (D:/UPLOAD)
String folderName = cacheService.queryParam("ENOCP_SAVE_FILE");
//file_addr = "1415689819017.doc";
// 拼接文件存放路径
String fullPath = folderName + File.separator + file_addr;
// 最终显示在前台的转换之后的SWF文件全路径
String finalFilePath = "";
// 是否可预览标志 0普通文档,MP3,SWF,可以预览 , 1 不支持的文件类型,不能预览 2,MP4播放
String previewFlag="";
// 可以预览的文件
if(fullPath.indexOf(".xls") > 0 || fullPath.indexOf(".xlsx") > 0 || fullPath.indexOf(".ppt") > 0 || fullPath.indexOf(".pptx") > 0 || fullPath.indexOf(".doc") > 0 || fullPath.indexOf(".docx") > 0 || fullPath.indexOf(".pdf") > 0 || fullPath.indexOf(".txt") > 0 || fullPath.indexOf(".mp3") > 0 || fullPath.indexOf(".swf") > 0){
previewFlag = "0";
}else if(fullPath.indexOf(".mp4") > 0){
previewFlag = "2";
}
/**************************************以下为文件转换*****************************************************/
if("0".equals(previewFlag)){
// 服务器信息
Properties props=System.getProperties();
// 不同系统分隔符
String separator=props.getProperty("file.separator");
// 系统版本Linux,Window XP,Window 7
String os_name=props.getProperty("os.name");
String os_flag="";
if(os_name.indexOf("Linux")==0){
os_flag="linux";
}else if(os_name.indexOf("Windows")==0){
os_flag="window";
}
// 去除后缀的文件名
String sysFileName_temp = file_addr.substring(0,file_addr.lastIndexOf("."));
File sourceFile; //转换源文件
File pdfFile; //PDF媒介文件
File swfFile; //SWF目标文件
File createPath; //创建文件存放目录
Runtime rt = Runtime.getRuntime(); //转换命令执行类
String swfToolsPath =""; //swfTools 安装路径,
if(os_flag.equals("linux")){
swfToolsPath=TrainConstants.SWFTOOLS_INSTALLPATH;//Linux下安装路径(根据实际安装目录更改)
}else{
//"D:\\SWFTools\\"
swfToolsPath =TrainConstants.SWFTOOLS_INSTALLPATH;
}
// swf
if(file_addr.indexOf(".swf") > 0){
// 文件原路径
finalFilePath = fullPath ;
}else if(file_addr.indexOf(".pdf") > 0){
// PDF格式文件处理方式
sourceFile = new File(fullPath);
// 创建SWF文件存放目录
createPath = new File(folderName + separator + "swfFiles" + separator);
if(!createPath.isDirectory()){
createPath.mkdir();
}
swfFile = new File(folderName+separator+"swfFiles"+separator+sysFileName_temp+".swf");
// 为了避免版本问题,不用7,8版本,用9
Process p= null;
try{
if(os_flag.equals("window")){
p =rt.exec(swfToolsPath+"pdf2swf.exe" + sourceFile.getPath() +" -o " +swfFile.getPath() + " -T 9");
}else{
// /usr/local/xpdf-chinese-simplified 中文语言包
p =rt.exec("pdf2swf -slanguagedir=/usr/local/xpdf-chinese-simplified -T 9 -s poly2bitmap -s zoom=150-s flashversion=9 "+ sourceFile.getPath()+" -o "+swfFile.getPath());
}
} catch(IOException e){
e.printStackTrace();
}
finalFilePath = folderName + separator + "swfFiles" + separator + sysFileName_temp + ".swf";
// 非pdf,非swf
}else{
// 非PDF格式文件处理方式
if(isLegal(file_addr.substring(file_addr.lastIndexOf(".") + 1).toUpperCase())){
sourceFile = new File(fullPath);
pdfFile = new File(folderName + separator + "swfFiles" + separator + sysFileName_temp + ".pdf");
swfFile = new File(folderName + separator + "swfFiles"+ separator + sysFileName_temp + ".swf");
if(!pdfFile.exists()){
// 获取连接对象SocketOpenOfficeConnection(8100)
OpenOfficeConnection connection = new SocketOpenOfficeConnection(re.getServerName(), 8100);
// 取得连接
try {
connection.connect();
// 创建文件格式转换对象
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
// 实现文件格式转换
converter.convert(sourceFile, pdfFile);
// 生成已转换的PDF文件
pdfFile.createNewFile();
if(!swfFile.exists()){
// 执行PDF文件转换成SWF文件命令
Process p= null;
if(os_flag.equals("window")){
p = rt.exec(swfToolsPath + "pdf2swf.exe " + pdfFile.getPath() + " -o " + swfFile.getPath() + " -T 9");
}else{
// /usr/local/xpdf-chinese-simplified 中文语言包
p = rt.exec("pdf2swf -s languagedir=/usr/local/xpdf-chinese-simplified -T 9 -spoly2bitmap -s zoom=150 -s flashversion=9 " + pdfFile.getPath() + " -o " + swfFile.getPath());
}
}
} catch (Exception e) {
e.printStackTrace();
} finally{
// 释放连接
connection.disconnect();
}
}else{
// 如果已经存在了pdf,但是swf由于某种原因不存在
if(!swfFile.exists()){
// 执行PDF文件转换成SWF文件命令
Process p= null;
try {
if(os_flag.equals("window")){
p= rt.exec(swfToolsPath + "pdf2swf.exe " + pdfFile.getPath() + " -o " + swfFile.getPath() + " -T 9");
}else{
p= rt.exec("pdf2swf -s languagedir=/usr/local/xpdf-chinese-simplified-T 9 -s poly2bitmap -s zoom=150 -s flashversion=9 " + pdfFile.getPath() + " -o " + swfFile.getPath());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
finalFilePath = folderName + separator + "swfFiles" + separator + sysFileName_temp + ".swf";
}
}
}
/** 通过流读取文件返回到页面----------------------*/
//设置文件类型
response.setContentType("application/octet-stream");
FileInputStream fis = null;
OutputStream os = null;
try {
//读取文件并输出
fis = new FileInputStream(new File(finalFilePath));
os = response.getOutputStream();
int count = 0;
byte[] buffer = new byte[1024];
while ( (count = fis.read(buffer)) != -1 ){
os.write(buffer, 0, count);
os.flush();
}
}catch(Exception e){
e.printStackTrace();
}finally {
try {//关闭流
if(null != fis){
fis.close();
}
if(null != os){
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 判断所转换文件类型是否合法
* @paramgetFileType //文件格式
* @param fileLegalFlag //是否合法标志 false:非法 true:合法
*/
public boolean isLegal(String getFileType){
boolean fileLegalFlag =false;
if(getFileType.equals("TXT")){
fileLegalFlag = true;
}else if(getFileType.equals("DOC")||getFileType.equals("DOCX")){
fileLegalFlag = true;
}else if(getFileType.equals("PPT")||getFileType.equals("PPTX")){
fileLegalFlag = true;
}else if(getFileType.equals("XLS")||getFileType.equals("XLSX")){
fileLegalFlag = true;
}
return fileLegalFlag;
}
代码还有很多需要完善的地方,后续会慢慢完善。仅供参考!
- 在线阅读实现(文档-->swf的转换)
- (五)java实现文档的在线浏览-转换pdf的同时转为swf
- (四)java实现文档的在线浏览-使用swftools将pdf转换为swf
- Java平台要实现类似豆丁百度文科的文档在线阅读,总体思路是讲doc docx等文件格式利用jcom转换成pdf再用swftools转为swf。再用flexpaper组件显示swf。
- Java平台要实现类似豆丁百度文科的文档在线阅读,总体思路是讲doc docx等文件格式利用jcom转换成pdf再用swftools转为swf。再用flexpaper组件显示swf。
- java实现文档的在线阅读功能
- (六)java实现文档的在线浏览-flexpaper在线浏览swf文档
- 实现Word、Excel、Txt文档在线转换为Swf(或PDF)
- Java实现文档在线阅读
- 实现类似百度文库的文档在线阅读功能
- 文档在线阅读(类似百度文库)的实现
- 文档在线阅读(类似百度文库)的实现
- 文档在线阅读的实现(类百度文库)
- PDF 转 SWF 在线阅读
- flexPaper +swftools实现文档在线阅读
- Java 实现文档在线阅读功能
- 文档在线预览(三)- 使用SWFTools转换文档为SWF格式
- java将office文档pdf文档转换成swf文件在线预览
- 关于centos6.5下的saltstack安装
- easyUI treegrid的使用
- 最全的VIM快捷键
- jmail邮件发送
- 关于rtmp封包中数据类型为0x16的封包
- 在线阅读实现(文档-->swf的转换)
- Android系统架构剖析(二)之应用框架演变
- android电源锁机制
- MyEclipse破解
- hdu 5074 Hatsune Miku(dp)
- Flex客户端与Red5通信示例
- 正则表达式30分钟入门教程
- jsf 文件下载
- 【1】Cocos2dx 3.0 与2.0的不同