将office文档及图片转换为flash

import java.io.File;

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;

/**
 * @File: FileConvert.java
 * @Date: Dec 23, 2011
 * @Descrip: 调用openoffice服务,利用JODConverter.jar(转换器-v2.2.2)将office文档转换为pdf;然后调用swftools工具将pdf转换为swf
 */
public class FileConvert {
       /**
        * 涉及pdf2swf路径问题 1-windows 2-linux
        */
       private final String CONVERTFILETYPE = "pdf,jpg,jpeg,font,gif,png,wav";
       private final String swftoolsPath = "C:/Program Files/SWFTools/";         // swftools工具的安装路径
       private static final int environment = 1;
       private String filePath;
       private String fileName;
       private String fileType;
       private File pdfFile;
       private File swfFile;
       private File srcFile;

       public FileConvert(String fileString) {
              ini(fileString);
       }

       /*
        * 重新设置file
        */
       public void setFile(String fileString) {
              ini(fileString);
       }

       /*
        * 初始化文件 fileString
        */
       private void ini(String fileString) {
              fileString = fileString.replace("/", "
\\").replace("\\", "\\\\");
              filePath = fileString.substring(0, fileString.lastIndexOf("
\\srcFile") + 1);
              fileName = fileString.substring(fileString.lastIndexOf("\\") + 1, fileString.lastIndexOf("."));
              fileType = fileString.substring(fileString.lastIndexOf(".") + 1);
              srcFile = new File(fileString);
              pdfFile = new File(getRealPath(filePath + "pdfFile\\") + fileName + ".pdf");
              swfFile = new File(getRealPath(filePath + "swfFile\\") + fileName + ".swf");
              // 设置jpg图片格式的转换命令
              if (fileType.toLowerCase().equals("jpg")) {
                     fileType = "jpeg";
              }
       }

       /*
        * 转为PDF @param file
        */
       private void doc2pdf() throws Exception {
              if (srcFile.exists()) {
                     if (!pdfFile.exists()) {
                            OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
                            try {
                                   // 获取连接之前,必须先启动OpenOffice服务(OpenOffice.org 3.3(zh-CN))
                                   // OpenOffice的安装路径如下
                                   // cd C:\Program Files\OpenOffice.org 3\program
                                   // soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
                                   connection.connect();
                                   // 获取转换器
                                   DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
                                   // 开始转换
                                   converter.convert(srcFile, pdfFile);
                                   // 转换成功,关闭连接
                                   connection.disconnect();
                            } catch (java.net.ConnectException e) {
                                   System.out.println("****swf转换异常,openoffice服务未启动!****");
                                   throw e;
                            } catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {
                                   System.out.println("****swf转换器异常,读取转换文件失败****");
                                   throw e;
                            }
                     } else {
                            System.out.println("****已经转换为pdf,不需要再进行转化****");
                     }
              } else {
                     System.out.println("****swf转换器异常,需要转换的文档不存在,无法转换****");
              }
       }

       /*
        * 转换成swf
        */
       private void pdf2swf() throws Exception {
              try {
                     Runtime runtime = Runtime.getRuntime();
                     if (!swfFile.exists()) {
                            if (environment == 1) { // windows环境处理
                                   if (pdfFile.exists()) {
                                          runtime.exec(swftoolsPath + "pdf2swf.exe " + pdfFile.getPath() + " -o " + swfFile.getPath() + " -T 9");
                                   } else if (CONVERTFILETYPE.indexOf(fileType.toLowerCase()) != -1) {
                                          runtime.exec(swftoolsPath + fileType + "2swf.exe " + srcFile.getPath() + " -o " + swfFile.getPath() + " -T 9");
                                   }
                            } else if (environment == 2) { // linux环境处理
                                   if (pdfFile.exists()) {
                                          runtime.exec("pdf2swf " + pdfFile.getPath() + " -o " + swfFile.getPath() + " -T 9");
                                   } else if (CONVERTFILETYPE.indexOf(fileType.toLowerCase()) != -1) {
                                          runtime.exec(fileType + "2swf " + srcFile.getPath() + " -o " + swfFile.getPath() + " -T 9");
                                   }
                            }
                     } else {
                            System.out.println("****swf已存在不需要转换****");
                     }
              } catch (Exception e) {
                     throw e;
              }

       }

       /*
        * 转换主方法
        */
       public boolean convert() {
              if (swfFile.exists()) {
                     return true;
              }

              try {
                     if (CONVERTFILETYPE.indexOf(fileType.toLowerCase()) == -1)
                            doc2pdf();
                     pdf2swf();
                     return true;
              } catch (Exception e) {
                     return false;
              }
       }

       /*
        * 返回文件路径 @param s
        */
       public String getswfPath() {
              if (swfFile.exists()) {
                     String tempString = swfFile.getPath();
                     tempString = tempString.replaceAll("
\\\\", "/");
                     return tempString;
              } else {
                     return "";
              }
       }

       /**
        * @Descrip:如果给定目录不存在,则创建
        * @param path
        * @return 真实目录
        */
       private String getRealPath(String path) {
              File realPath = new File(path);
              if (!realPath.exists())
                     realPath.mkdirs();
              return path;
       }

       /**
        * @Descrip: 程序调用(文件的绝对路径)
        * @throws Exception
        */
       public static void main(String s[]) throws Exception {
              FileConvert d = new FileConvert("E:\\IDE6.5\\cmci\\WebRoot\\upfile\\srcFile\\1328075846843.DOCX");
              d.convert();
       }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值