二、代码如下:
package com.converter; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.LineNumberReader; import java.util.Properties; public class PDF2SWF { public void converter(String name){ // 服务器信息 Properties props = System.getProperties(); // 系统版本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"; } //获得PDF文件 File pdfFile = new File("D:/test/" + name); String fname = name.substring(0,name.lastIndexOf(".")); //转换后存放的SWF文件 File swfFile = new File("D:/test/" + fname + ".swf"); Runtime rt = Runtime.getRuntime(); // 执行PDF文件转换成SWF文件命令 // linux命令: pdf2swf // -slanguagedir=/usr/local/xpdf-chinese-simplified-T // 9 -s poly2bitmap -s zoom=150 // -sflashversion=9"/home/s.pdf" -o "/home/%.swf" Process p = null; try { if (os_flag.equals("window")) { p = rt.exec("D:/swftool/" + "pdf2swf.exe "+ pdfFile.getPath() + " -o "+ swfFile.getPath() + " -T 9 -G -s poly2bitmap"); } else { 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()); } // 各系统清理缓存 if (os_flag.equals("window")) { clearCache(p.getInputStream(), p.getErrorStream()); } else if (os_flag.equals("linux")) { InputStreamReader ir = new InputStreamReader(p.getInputStream()); LineNumberReader input = new LineNumberReader(ir); String line; while ((line = input.readLine()) != null) { } } } catch (IOException e) { e.printStackTrace(); } } //清理缓冲区 public void clearCache(InputStream isi, InputStream ise) { try { final InputStream is1 = isi; // 启用单独线程清空InputStream缓冲区 new Thread(new Runnable() { public void run() { BufferedReader br = new BufferedReader(new InputStreamReader(is1)); try { while (br.readLine() != null); } catch (IOException e) { e.printStackTrace(); } } }).start(); // 读入ErrorStream缓冲 BufferedReader br = new BufferedReader(new InputStreamReader(ise)); // 保存缓冲输出结果 StringBuilder buf = new StringBuilder(); String line = null; try { line = br.readLine(); } catch (IOException e) { e.printStackTrace(); } // 循环等待进程结束 while (line != null) buf.append(line); is1.close(); ise.close(); br.close(); } catch (Exception e) { e.printStackTrace(); } } }