After checking a lot of information, I almost flipped through the Java source code. Finally, I combined an article (I forgot the source), and thought that the output stream would block the process execution. The Java process executes one input stream and two output streams (relative to external programs). When the two output streams have content output and the Java executor does not clear the output stream in time, the process will be blocked.
Now I have posted the code, hoping to help peers in need:
Copy the code as follows:/**
* pdf to swf function
* @param path Input and output file path
* @param inputFileName Enter file name
* @param outputFileName output file name
* @return File generated swf file
*/
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("The output result is: " + buf);
}
}).start(); // Start a separate thread to clear the buffer of process.getInputStream()
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("Error output is: " + buf);
try {
process.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
long endTime = System.nanoTime();
System.out.println("to swf time takes: " + (endTime - beginTime) / 1000000000 + "seconds" + sourceFile);
return new File(destFile);
}