java使用swfTools将pdf文件转换swf文件

转载请表明出处 https://blog.csdn.net/Amor_Leo/article/details/89388851 谢谢

java使用swfTools将pdf文件转换swf文件

swfTools下载

swfTools下载

工具类

public class PDF2SWFUtils {

    private static Logger logger = LoggerFactory.getLogger(PDF2SWFUtils.class);

    /**
     * 利用SWFTools工具将pdf转换成swf,转换完后的swf文件与pdf同名
     * @param intputPath PDF文件存放路径(包括文件名)
     * @param exePath 转换器安装路径
     */
    public static synchronized boolean pdf2swf(String intputPath , String outputPath, String exePath)  {
        // 源文件不存在则返回
        File source = new File(intputPath);
        if (!source.exists()) {
            logger.debug("原始文件不存在!!");
            return false;
        }
        // 目标路径存在则删除
        File out = new File(outputPath);
        if (out.exists()) {
            logger.debug("目标文件存在,删除!!");
            out.delete();
        }

        Process pro = null;
        if (isWindowsSystem()) {
            //如果是windows系统
            //命令行命令
            String cmd = exePath + " \"" + intputPath + "\" -o \"" + outputPath + "\"" + " -f -T 9";
            //Runtime执行后返回创建的进程对象
            try {
                pro = Runtime.getRuntime().exec(cmd);
                logger.debug("window下开始转换!! "+ pro);
            } catch (IOException e) {
                e.printStackTrace();
                logger.debug("转换失败!! 异常: "+ e );
                return false;
            }
        } else {
            //如果是linux系统,路径不能有空格,而且一定不能用双引号,否则无法创建进程
            String[] cmd = new String[5];
            cmd[0] = exePath;
            cmd[1] = intputPath;
            // cmd[2] = filePath + "/" + fileName + ".swf";
            cmd[3] = outputPath;
            //Runtime执行后返回创建的进程对象
            try {
                pro = Runtime.getRuntime().exec(cmd);
                logger.debug("linux下开始转换!! "+ pro);
            } catch (IOException e) {
                e.printStackTrace();
                logger.debug("转换失败!! 异常: "+ e );
                return false;
            }
        }
        //非要读取一遍cmd的输出,要不不会flush生成文件(多线程)
        new DoOutput(pro.getInputStream()).start();
        new DoOutput(pro.getErrorStream()).start();
        try {
            //调用waitFor方法,是为了阻塞当前进程,直到cmd执行完
            pro.waitFor();
        } catch (InterruptedException e) {
            e.printStackTrace();
            logger.debug("转换失败!! 异常: "+ e );
            return false;
        }finally {
            pro.destroy();
        }
        return true;
    }

    /**
     * 判断是否是windows操作系统
     * @return
     */
    private static boolean isWindowsSystem() {
        String p = System.getProperty("os.name");
        return p.toLowerCase().contains("windows");
    }

    /**
     * 多线程内部类
     * 读取转换时cmd进程的标准输出流和错误输出流,这样做是因为如果不读取流,进程将死锁
     */
    private static class DoOutput extends Thread {
        public InputStream is;
        //构造方法
        public DoOutput(InputStream is) {
            this.is = is;
        }
        @Override
        public void run() {
            BufferedReader br = new BufferedReader(new InputStreamReader(this.is));
            String str = null;
            try {
                //这里并没有对流的内容进行处理,只是读了一遍
                while ((str = br.readLine()) != null);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    /**
     * 测试main方法
     * @param args
     */
    public static void main(String[] args) {
        //转换器安装路径
        String exePath = "D:/SoftWare/SWFTools/pdf2swf.exe";
        boolean b = PDF2SWFUtils.pdf2swf("D:/DOC/aaa.pdf", "D:/DOC/111.swf", exePath);
        System.out.println(b);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值