在我的项目中,我必须将docx转换为pdf。要将docx转换为pdf,我使用processbuilder对象运行bat文件。它在我的本地环境中工作,但不在服务器机器上工作,同时它不会抛出任何异常。以下是我的代码。
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try{
String path = "C:\\selva\\";
String arg1 = "C:\\kumar\\a.docm";
String command = "cmd.exe /c start " + path + "runner.bat "+arg1;
String runCommand=path + "runner.bat";
ProcessBuilder pb = null;
pb = new ProcessBuilder(runCommand, arg1);
pb.redirectErrorStream(true);
pb.start();
response.sendRedirect("/GENERATEPDFFROMDOCX/genPDF.jsp");
}catch(Exception ex){
ex.printStackTrace();
}
}
Runner.bat
echo %1
"C:\Program Files\Microsoft Office 15\root\office15\winword.exe" /mExportToPDFext %1>ONE.TXT
Local Machine Log:
C:\Users\Selva\Desktop\eclipse>echo C:\kumar\a.docm
C:\kumar\a.docm
C:\Users\Selva\Desktop\eclipse>"C:\Program Files\Microsoft Office 15\root\office15\winword.exe" /mExportToPDFext C:\kumar\a.docm 1>ONE.TXT
Error Code is 0
Process Finished
Server Log:
C:\Program Files\Apache Software Foundation\Tomcat 8.0>echo C:\kumar\a.docm
C:\kumar\CarLoanAgreement.docm
C:\Program Files\Apache Software Foundation\Tomcat 8.0>"C:\Program Files\Microsoft Office 15\root\office15\winword.exe" /mExportToPDFext C:\kumar\a.docm 1>ONE.TXT
The process cannot access the file because it is being used by another process.
Error Code is 1
Process Finished
上述代码有什么问题?
答案 0 :(得分:0)
在您的代码中,您正在硬编码某些软件的路径。
为什么它不在服务器上工作的最可能的原因是: