Java 進程執(zhí)行外部程序造成阻塞的一種原因
現(xiàn)貼出代碼,希望能幫助到有需要的同行:
* pdf轉(zhuǎn)swf函數(shù)
* @param path 輸入輸出文件路徑
* @param inputFileName 輸入文件名
* @param outputFileName 輸出文件名
* @return File 生成的swf文件
*/
private static File toSwf(String sourceFile, String destFile, String command) {
long beginTime = System.nanoTime();
Runtime rt = Runtime.getRuntime();
try {
Process process = rt.exec(command);
final InputStream isNormal = process.getInputStream();
new Thread(new Runnable() {
public void run() {
BufferedReader br = new BufferedReader(new InputStreamReader(isNormal));
StringBuilder buf = new StringBuilder();
String line = null;
try {
while((line = br.readLine()) != null){
buf.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("輸出結(jié)果為:" + buf);
}
}).start(); // 啟動單獨的線程來清空process.getInputStream()的緩沖區(qū)
InputStream isError = process.getErrorStream();
BufferedReader br2 = new BufferedReader(new InputStreamReader(isError));
StringBuilder buf = new StringBuilder();
String line = null;
while((line = br2.readLine()) != null){
buf.append(line + "\n");
}
System.out.println("錯誤輸出結(jié)果為:" + buf);
try {
process.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
long endTime = System.nanoTime();
System.out.println("轉(zhuǎn)swf耗時: " + (endTime - beginTime) / 1000000000 + " 秒 " + sourceFile);
return new File(destFile);
}
相關文章
java 重載(overload)與重寫(override)詳解及實例
這篇文章主要介紹了java 重載(overload)與重寫(override)詳解及實例的相關資料,并附實例代碼,需要的朋友可以參考下2016-10-10詳解Springboot之整合JDBCTemplate配置多數(shù)據(jù)源
這篇文章主要介紹了詳解Springboot之整合JDBCTemplate配置多數(shù)據(jù)源,文中有非常詳細的代碼示例,對正在學習java的小伙伴們有很好的幫助,需要的朋友可以參考下2021-04-04Spring?Boot?優(yōu)雅整合多數(shù)據(jù)源
這篇文章主要介紹了Spring?Boot?優(yōu)雅整合多數(shù)據(jù)源,多數(shù)據(jù)源就是在一個單一應用中涉及到了兩個及以上的數(shù)據(jù)庫,更多相關內(nèi)容需要的小伙伴可以參考下面文章介紹2022-05-05在CentOS系統(tǒng)上安裝Java的openjdk的方法
這篇文章主要介紹了在CentOS系統(tǒng)上安裝Java的openjdk的方法,同樣適用于Fedora等其他RedHat系的Linux系統(tǒng),需要的朋友可以參考下2015-06-06