利用swftools將pdf轉換為swf小例
package com.iori.webapp.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class PDF2SWFUtil {
/**
* 利用SWFTools工具將pdf轉換成swf,轉換完后的swf文件與pdf同名
* @author iori
* @param fileDir PDF文件存放路徑(包括文件名)
* @param exePath 轉換器安裝路徑
* @throws IOException
*/
public static synchronized void pdf2swf(String fileDir, String exePath) throws IOException {
//文件路徑
String filePath = fileDir.substring(0, fileDir.lastIndexOf("/"));
//文件名,不帶后綴
String fileName = fileDir.substring((filePath.length() + 1), fileDir.lastIndexOf("."));
Process pro = null;
if (isWindowsSystem()) {
//如果是windows系統
//命令行命令
String cmd = exePath + " \"" + fileDir + "\" -o \"" + filePath + "/" + fileName + ".swf\"";
//Runtime執行后返回創建的進程對象
pro = Runtime.getRuntime().exec(cmd);
} else {
//如果是linux系統,路徑不能有空格,而且一定不能用雙引號,否則無法創建進程
String[] cmd = new String[3];
cmd[0] = exePath;
cmd[1] = fileDir;
cmd[2] = filePath + "/" + fileName + ".swf";
//Runtime執行后返回創建的進程對象
pro = Runtime.getRuntime().exec(cmd);
}
//非要讀取一遍cmd的輸出,要不不會flush生成文件(多線程)
new DoOutput(pro.getInputStream()).start();
new DoOutput(pro.getErrorStream()).start();
try {
//調用waitFor方法,是為了阻塞當前進程,直到cmd執行完
pro.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 判斷是否是windows操作系統
* @author iori
* @return
*/
private static boolean isWindowsSystem() {
String p = System.getProperty("os.name");
return p.toLowerCase().indexOf("windows") >= 0 ? true : false;
}
/**
* 多線程內部類
* 讀取轉換時cmd進程的標準輸出流和錯誤輸出流,這樣做是因為如果不讀取流,進程將死鎖
* @author iori
*/
private static class DoOutput extends Thread {
public InputStream is;
//構造方法
public DoOutput(InputStream is) {
this.is = is;
}
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 = "c:/SWFTools/pdf2swf.exe";
try {
PDF2SWFUtil.pdf2swf("c:/test.pdf", exePath);
} catch (IOException e) {
System.err.println("轉換出錯!");
e.printStackTrace();
}
}
}
/**
* 利用SWFTools工具將pdf轉換成swf,轉換完后的swf文件與pdf同名
* @author iori
* @param fileDir PDF文件存放路徑(包括文件名)
* @param exePath 轉換器安裝路徑
* @throws IOException
*/
public static synchronized void pdf2swf(String fileDir, String exePath) throws IOException {
//文件路徑
String filePath = fileDir.substring(0, fileDir.lastIndexOf("/"));
//文件名,不帶后綴
String fileName = fileDir.substring((filePath.length() + 1), fileDir.lastIndexOf("."));
Process pro = null;
if (isWindowsSystem()) {
//如果是windows系統
//命令行命令
String cmd = exePath + " \"" + fileDir + "\" -o \"" + filePath + "/" + fileName + ".swf\"";
//Runtime執行后返回創建的進程對象
pro = Runtime.getRuntime().exec(cmd);
} else {
//如果是linux系統,路徑不能有空格,而且一定不能用雙引號,否則無法創建進程
String[] cmd = new String[3];
cmd[0] = exePath;
cmd[1] = fileDir;
cmd[2] = filePath + "/" + fileName + ".swf";
//Runtime執行后返回創建的進程對象
pro = Runtime.getRuntime().exec(cmd);
}
//非要讀取一遍cmd的輸出,要不不會flush生成文件(多線程)
new DoOutput(pro.getInputStream()).start();
new DoOutput(pro.getErrorStream()).start();
try {
//調用waitFor方法,是為了阻塞當前進程,直到cmd執行完
pro.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 判斷是否是windows操作系統
* @author iori
* @return
*/
private static boolean isWindowsSystem() {
String p = System.getProperty("os.name");
return p.toLowerCase().indexOf("windows") >= 0 ? true : false;
}
/**
* 多線程內部類
* 讀取轉換時cmd進程的標準輸出流和錯誤輸出流,這樣做是因為如果不讀取流,進程將死鎖
* @author iori
*/
private static class DoOutput extends Thread {
public InputStream is;
//構造方法
public DoOutput(InputStream is) {
this.is = is;
}
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 = "c:/SWFTools/pdf2swf.exe";
try {
PDF2SWFUtil.pdf2swf("c:/test.pdf", exePath);
} catch (IOException e) {
System.err.println("轉換出錯!");
e.printStackTrace();
}
}
}
本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/FG2006/archive/2010/08/19/5823420.aspx
本文由用戶 seailove 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!