您的位置:首页 > 运维架构 > Linux

用java把MS office(ppt、excel、word、txt) 转换成 flash,并进行播放(linux、windows)

2012-07-25 21:26 766 查看

转自:

用java把MS office(ppt、excel、word、txt) 转换成 flash,并进行播放(linux、windows)

特别感谢本文的作者!!!

博客分类:
javaOfficeWindowsJavaLinuxExcel

现在项目有个需求,要求把ms office和txt在线播放,偶当时就想到了布丁网

然后就问……google……

结果google告诉偶,简单的办法在windows上是可以实现的,非常简单...不过要用到jacob,具体大家去问Google。

由于要调用dll,在linux上偶不清楚能不能用(公司搞服务器的人说不可以),结果就被pass了……

再然后……只有这么办了……

先用openOffice把ppt、word、excel、txt转换成pdf,然后用swftools转换成swf,然后在线播放。

具体说明如下(windows,在linux下偶正准备测试)

1、安装相关软件,这个偶就不说了……

2、下载jodconverter,偶用的2.2.2...然后怎么把lib放在环境变量或者项目环境偶就不说了……

3、以cmd方式启动openoffice server

Java代码  


cd opeonofiice的安装路径/program  

Java代码  


soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard  

4、看看8100被监听没

Java代码  


netstat -an  

5、写代码了……偶没有把代码集成到项目中,所以是单独的例子,不过这样还好点,不然和项目偶和太厉害了

JOD4DocToPDF

Java代码  


/** 
 *  
 */  
package com.born.sys.util.pdf;  
  
import java.io.File;  
import java.net.ConnectException;  
import java.util.Date;  
  
import com.artofsolving.jodconverter.DocumentConverter;  
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;  
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;  
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;  
  
/** 
 * <ul> 
 * <li>文件名称: com.born.sys.util.pdf.JOD4DocToPDF.java</li> 
 * <li>文件描述:</li> 
 * <li>版权所有: 版权所有(C)2001-2006</li> 
 * <li>公 司: born</li> 
 * <li>内容摘要:</li> 
 * <li>其他说明:</li> 
 * <li>完成日期:2010-5-21</li> 
 * <li>修改记录0:无</li> 
 * </ul> 
 *  
 * @version 1.0 
 * @author 许力多 
 */  
public class JOD4DocToPDF extends java.lang.Thread {  
    private File inputFile;// 需要转换的文件  
    private File outputFile;// 输出的文件  
  
    public JOD4DocToPDF(File inputFile, File outputFile) {  
        this.inputFile = inputFile;  
        this.outputFile = outputFile;  
    }  
  
    public void docToPdf() {  
        Date start = new Date();  
        // connect to an OpenOffice.org instance running on port 8100  
        OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);  
        try {  
            connection.connect();  
  
            // convert  
            DocumentConverter converter = new OpenOfficeDocumentConverter(  
                    connection);  
            converter.convert(inputFile, outputFile);  
        } catch (ConnectException cex) {  
            cex.printStackTrace();  
        } finally {  
            // close the connection  
            if (connection != null) {  
                connection.disconnect();  
                connection = null;  
            }  
        }  
        long l = (start.getTime() - new Date().getTime());  
        long day = l / (24 * 60 * 60 * 1000);  
        long hour = (l / (60 * 60 * 1000) - day * 24);  
        long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);  
        long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);  
        System.out.println("生成" + outputFile.getName() + "耗费:" + min + "分" + s  
                + "秒");  
    }  
  
    /** 
     * 由于服务是线程不安全的,所以……需要启动线程 
     */  
    public void run() {  
        this.docToPdf();  
  
    }  
  
    public File getInputFile() {  
        return inputFile;  
    }  
  
    public void setInputFile(File inputFile) {  
        this.inputFile = inputFile;  
    }  
  
    public File getOutputFile() {  
        return outputFile;  
    }  
  
    public void setOutputFile(File outputFile) {  
        this.outputFile = outputFile;  
    }  
  
    /** 
     * @param args 
     */  
    public static void main(String[] args) {  
        JOD4DocToPDF tools = new JOD4DocToPDF(new File("d:/中文的ppt哦.ppt"),  
                new File("d:/被转换的pdf.pdf"));  
        tools.start();  
  
    }  
  
}  

ps:其实还有很多属性的,不过偶图简单,直接用的官方例子,人懒无敌……阿门

通过Runtime来调用cmd,然后生成相关的播放文件,生成好的flash效果见点击看效果

PdfToSwf

Java代码  


package com.born.sys.util.pdf;  
  
import java.io.BufferedReader;  
import java.io.File;  
import java.io.IOException;  
import java.io.InputStreamReader;  
  
/** 
 * <ul> 
 * <li>文件名称: com.born.sys.util.pdf.PdfToSwf.java</li> 
 * <li>文件描述: pdf生成swf</li> 
 * <li>版权所有: 版权所有(C)2001-2006</li> 
 * <li>公 司: born</li> 
 * <li>内容摘要:</li> 
 * <li>其他说明:</li> 
 * <li>完成日期:2010-5-21</li> 
 * <li>修改记录0:无</li> 
 * </ul> 
 *  
 * @version 1.0 
 * @author 许力多 
 */  
public class PdfToSwf {  
    public int convertPDF2SWF(String sourcePath, String destPath,  
            String fileName) throws IOException {  
        // 目标路径不存在则建立目标路径  
        File dest = new File(destPath);  
        if (!dest.exists()) {  
            dest.mkdirs();  
        }  
  
        // 源文件不存在则返回  
        File source = new File(sourcePath);  
        if (!source.exists()) {  
            return 0;  
        }  
  
        // 调用pdf2swf命令进行转换  
        // D:\tools\SWFTools>pdf2swf.exe -z -B rfxview.swf -s flashversion=9  
        // d:/人员管理系  
        // 统PersonalManagementSystem简介.pdf -o d:/test.swf  
  
        // 要把D:\\tools\\SWFTools\\放在path里面……不然使用不了播放器  
  
        // 先生成flash  
        String[] envp = new String[1];  
        envp[0] = "PATH=D:\\tools\\SWFTools\\";  
        String command = "pdf2swf -z -s flashversion=9 \"" + sourcePath  
                + "\" -o \"" + destPath + fileName + "\"";  
  
        Process pro = Runtime.getRuntime().exec(command, envp);  
        // System.out.println(command);  
        BufferedReader bufferedReader = new BufferedReader(  
                new InputStreamReader(pro.getInputStream()));  
        while (bufferedReader.readLine() != null) {  
            String text = bufferedReader.readLine();  
            System.out.println(text);  
        }  
        try {  
            pro.waitFor();  
        } catch (InterruptedException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
        // 然后在套播放器  
        /* 
         * swfcombine -z -X 720 -Y 540 "D:\tools\SWFTools\swfs\rfxview.swf" 
         * viewport="d:/人 
         * 员管理系统PersonalManagementSystem简介.swf" -o "d:/人员管理系统PersonalManagemen 
         * tSystem简介.swf" 
         */  
        command = "swfcombine -z -X 720 -Y 540 \"D:/tools/SWFTools/swfs/rfxview.swf\" viewport=\""  
                + destPath + fileName + "\" -o \"" + destPath + fileName + "\"";  
        pro = Runtime.getRuntime().exec(command, envp);  
        System.out.println(command);  
        bufferedReader = new BufferedReader(new InputStreamReader(pro  
                .getInputStream()));  
        while (bufferedReader.readLine() != null) {  
            String text = bufferedReader.readLine();  
            System.out.println(text);  
        }  
        try {  
            pro.waitFor();  
        } catch (InterruptedException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
        return pro.exitValue();  
  
    }  
  
    public static void main(String[] args) {  
        String sourcePath = "d:/PersonalManagementSystem.pdf";  
        String destPath = "d:/";  
        String fileName = "PersonalManagementSystem.swf";  
        try {  
            System.out.println(new PdfToSwf().convertPDF2SWF(sourcePath,  
                    destPath, fileName));  
        } catch (IOException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
    }  
}  

好了,基本就解决了,丢网页上就可以了,这个偶就不说了……

……过两天把她们集成到项目里面偶就安心了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息