我正在制作一个在 Windows 上运行的 Web 应用程序,Apache Tomcat 上的 JSP,它应该将 Office 文件转换为 PDF。为此,我在 Word 上创建了将文件保存为 PDF 格式的宏,并且这些宏已经过测试。我的应用程序在测试机器上正确运行。我所做的类似于以下内容(抱歉,现在无法访问代码):
String arr[3] = {wordPath,macroName,fileToconvertPath};
new ProcessBuilder(arr).start();
我正在检索标准和错误输出。在测试机器中它按预期工作,也在服务器中打印 arr 变量并在命令行中使用它并且它工作。但是,当从应用程序调用时,Word 在任务管理器中作为进程打开,但永远不会按预期结束。
编辑:
这是实际的代码:
public boolean changeFormatMSOffice(String inF, String WORD) {
System.out.println("changeFormatMSOffice(" + inF + "," + WORD + ")");
String macro = "";
ArrayList<String> wordArr = new ArrayList<String>(java.util.Arrays.asList(TO_PDF_WORD.replace(" ", "").split(",")));
ArrayList<String> excelArr = new ArrayList<String>(java.util.Arrays.asList(TO_PDF_EXCEL.replace(" ", "").split(",")));
ArrayList<String> ppArr = new ArrayList<String>(java.util.Arrays.asList(TO_PDF_PP.replace(" ", "").split(",")));
String extension = inF.substring(inF.lastIndexOf(".")).replace(".", "").trim();
BufferedWriter out;
String cmdArr[] = new String[3];
List<String> cmdList = new ArrayList<String>();
cmdList.add(WORD);
cmdArr[0] = WORD;
String saveFile = "";
if (wordArr.contains(extension)) {
macro = "/msaveAsPDF";
cmdList.add(macro);
cmdList.add(inF);
//cmdArr[1] = "/m" + macro;
cmdArr[2] = inF; // aplica conversion sobre el archivo al que apunta inF
} else if (excelArr.contains(extension) || ppArr.contains(extension)) {
if (excelArr.contains(extension)) {
macro = "/mSaveXLSAsPDF";
} else {
macro = "/msavePPTAsPDF";
}
cmdList.add(macro);
int fileNum = 0;
saveFile = "\"" + DOCUMENTS_PATH + (DOCUMENTS_PATH.substring(DOCUMENTS_PATH.length() - 1).equals(File.separator) ? "" : File.separator) + fileNum + ".txt\"";
while (new File(saveFile).exists()) {
fileNum++;
saveFile = "\"" + DOCUMENTS_PATH + (DOCUMENTS_PATH.substring(DOCUMENTS_PATH.length() - 1).equals(File.separator) ? "" : File.separator) + fileNum + ".txt\"";
}
try {
out = new BufferedWriter(new FileWriter(saveFile));
out.write(inF);
out.close();
cmdList.add(saveFile);
cmdArr[2] = saveFile; // aplica converion sobre la ruta que esta escrita en la direccion del saveFile
} catch (Exception e) {
System.err.println(e.toString());
}
}
cmdArr[1] = macro; //macro a usar
String cmd = "\"" + cmdArr[0] + "\" " + cmdArr[1] + " \"" + cmdArr[2] + "\"";
System.out.println(cmd);
try {
ProcessBuilder proc = new ProcessBuilder(cmdList);
System.out.println("PreWaitForList");
Process pro = proc.start();
StreamGobbler g1 = new StreamGobbler("stdin", pro.getInputStream());
StreamGobbler g2 = new StreamGobbler("stderr", pro.getErrorStream());
g1.start();
g2.start();
pro.waitFor();
//Process proc = Runtime.getRuntime().exec(cmdArr);
//proc.waitFor();
System.out.println("AfterWaitFor");
try {
if (!saveFile.equals("")) {
new File(saveFile).delete();
}
} catch (Exception e) {
}
return true;
} catch (Exception e) {
System.err.println(e.toString());
return false;
}
}
这是 StreamGobbler 的实现:
public class StreamGobbler implements Runnable {
String name;
InputStream is;
Thread thread;
public StreamGobbler(String name, InputStream is) {
this.name = name;
this.is = is;
}
public void start() {
thread = new Thread(this);
thread.start();
}
public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
while (true) {
String s = br.readLine();
if (s == null) {
break;
}
System.out.println("[" + name + "] " + s);
}
is.close();
} catch (Exception ex) {
System.out.println("Problem reading stream " + name + "... :" + ex);
ex.printStackTrace();
}
}
}