我正在尝试使用部署在 Tomcat7 上的应用程序将下载的 DOCX 文件转换为 PDF,但每次尝试都失败了:
18:16:26.286 [pool-5-thread-3] INFO c.d.c.msoffice.MicrosoftWordBridge - Requested conversion from .\docTemp\880943b5-769a-4f30-bbe2-5256997e5be3\temp5 (application/vnd.com.documents4j.any-msword) to .\docTemp\880943b5-769a-4f30-bbe2-5256997e5be3\temp6 (application/pdf)
18:16:26.286 [pool-5-thread-3] DEBUG o.z.exec.ProcessExecutor - Executing [cmd, /S, /C, ""C:\Program Files\Apache Software Foundation\Tomcat 7.0\.\docTemp\word_convert1683097585.vbs" "C:\Program Files\Apache Software Foundation\Tomcat 7.0\.\docTemp\880943b5-769a-4f30-bbe2-5256997e5be3\temp5" "C:\Program Files\Apache Software Foundation\Tomcat 7.0\.\docTemp\880943b5-769a-4f30-bbe2-5256997e5be3\temp6" "17""] in .\docTemp.
18:16:26.286 [pool-5-thread-3] DEBUG o.z.exec.ProcessExecutor - Started java.lang.ProcessImpl@63ef6686
18:16:26.333 [WaitForProcess-java.lang.ProcessImpl@63ef6686] DEBUG o.zeroturnaround.exec.WaitForProcess - java.lang.ProcessImpl@63ef6686 stopped with exit code -2
com.documents4j.throwables.ConversionInputException: The input file seems to be corrupt
at com.documents4j.util.Reaction$ConversionInputExceptionBuilder.make(Reaction.java:159)
at com.documents4j.util.Reaction$ExceptionalReaction.apply(Reaction.java:75)
at com.documents4j.conversion.ExternalConverterScriptResult.resolve(ExternalConverterScriptResult.java:70)
at com.documents4j.conversion.ProcessFutureWrapper.evaluateExitValue(ProcessFutureWrapper.java:48)
at com.documents4j.conversion.ProcessFutureWrapper.get(ProcessFutureWrapper.java:36)
at com.documents4j.conversion.ProcessFutureWrapper.get(ProcessFutureWrapper.java:11)
at com.documents4j.job.AbstractFutureWrappingPriorityFuture.run(AbstractFutureWrappingPriorityFuture.java:78)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
当我尝试使用 Web 应用程序中使用的相同库/类手动转换文件(在控制台中)时,一切正常,但直接在 webapp 中转换失败。我是否尝试直接转换输入流或先在本地保存文件然后将其转换为文件都没有关系(这意味着输入流/文档很好)。我正在使用以下单例(删除了不必要的代码):
public static DocumentHelper getInstance() throws IOException{
if (INSTANCE == null)
INSTANCE = new DocumentHelper();
return INSTANCE;
}
public DocumentHelper() throws IOException{
this.appProps = new Properties();
this.appProps.load(this.getClass().getClassLoader().getResourceAsStream("layer.properties"));
new File(this.appProps.getProperty(CONVERTIOR_TEMP_DIR)).mkdir();
this.converter = LocalConverter.builder()
.baseFolder(new File(this.appProps.getProperty(CONVERTIOR_TEMP_DIR)))
.workerPool(20, 25, 5, TimeUnit.SECONDS)
.processTimeout(15, TimeUnit.SECONDS)
.build();
}
public byte[] convertDocument(InputStream wordFile){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
converter.convert(wordFile).as(DocumentType.MS_WORD)
.to(baos).as(DocumentType.PDF)
.execute();
return baos.toByteArray();
}