gpt4 book ai didi

java - 文件系统交互异常: Could not access target file while using documents4j

转载 作者:太空宇宙 更新时间:2023-11-04 12:46:26 24 4
gpt4 key购买 nike

我正在使用documents4j将word文档转换为pdf,有时我遇到了异常

2016-03-28 09:29:16.982  INFO 3660 --- [pool-1-thread-2] c.d.c.msoffice.MicrosoftWordBridge       : Requested conversion from C:\conversion-temp\2b33637b-b74a-4aaa-ac65-a5ebc1eb3efc\temp3 (application/msword) to C:\conversion-temp\2b33637b-b74a-4aaa-ac65-a5ebc1eb3efc\temp4 (application/pdf)
2016-03-28 09:29:17.372 ERROR 3660 --- [http-nio-8080-exec-9] c.s.c.e.mappers.ExceptionMapper : Exception while handling request

com.documents4j.throwables.FileSystemInteractionException: Could not access target file
at com.documents4j.util.Reaction$FileSystemInteractionExceptionBuilder.make(Reaction.java:180) ~[documents4j-util-all-1.0.2.jar:na]
at com.documents4j.util.Reaction$ExceptionalReaction.apply(Reaction.java:75) ~[documents4j-util-all-1.0.2.jar:na]
at com.documents4j.conversion.ExternalConverterScriptResult.resolve(ExternalConverterScriptResult.java:70) ~[documents4j-transformer-api-1.0.2.jar:na]
at com.documents4j.conversion.ProcessFutureWrapper.evaluateExitValue(ProcessFutureWrapper.java:48) ~[documents4j-util-transformer-process-1.0.2.jar:na]
at com.documents4j.conversion.ProcessFutureWrapper.get(ProcessFutureWrapper.java:36) ~[documents4j-util-transformer-process-1.0.2.jar:na]
at com.documents4j.conversion.ProcessFutureWrapper.get(ProcessFutureWrapper.java:11) ~[documents4j-util-transformer-process-1.0.2.jar:na]
at com.documents4j.job.AbstractFutureWrappingPriorityFuture.run(AbstractFutureWrappingPriorityFuture.java:78) ~[documents4j-util-conversion-1.0.2.jar:na]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) ~[na:1.8.0_74]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) ~[na:1.8.0_74]
at java.lang.Thread.run(Thread.java:745) ~[na:1.8.0_74]

在此异常之后,documents4j 库将拒绝任何进一步的请求,并出现以下异常

com.documents4j.throwables.ConverterAccessException: The converter seems to be shut down
at com.documents4j.util.Reaction$ConverterAccessExceptionBuilder.make(Reaction.java:117) ~[documents4j-util-all-1.0.2.jar:na]
at com.documents4j.util.Reaction$ExceptionalReaction.apply(Reaction.java:75) ~[documents4j-util-all-1.0.2.jar:na]
at com.documents4j.conversion.ExternalConverterScriptResult.resolve(ExternalConverterScriptResult.java:70) ~[documents4j-transformer-api-1.0.2.jar:na]
at com.documents4j.conversion.ProcessFutureWrapper.evaluateExitValue(ProcessFutureWrapper.java:48) ~[documents4j-util-transformer-process-1.0.2.jar:na]
at com.documents4j.conversion.ProcessFutureWrapper.get(ProcessFutureWrapper.java:36) ~[documents4j-util-transformer-process-1.0.2.jar:na]
at com.documents4j.conversion.ProcessFutureWrapper.get(ProcessFutureWrapper.java:11) ~[documents4j-util-transformer-process-1.0.2.jar:na]
at com.documents4j.job.AbstractFutureWrappingPriorityFuture.run(AbstractFutureWrappingPriorityFuture.java:78) ~[documents4j-util-conversion-1.0.2.jar:na]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) ~[na:1.8.0_74]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) ~[na:1.8.0_74]
at java.lang.Thread.run(Thread.java:745) ~[na:1.8.0_74]

这就是我进行文档转换的方式。

我正在实例化 LocalConverter 的实例

LocalConverter.builder().workerPool(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.MINUTES).baseFolder(baseFolder).processTimeout(processTimeout, TimeUnit.SECONDS).build();

corePoolSize is 5
maximumPoolSize is 10
keepAliveTime is 3 minutes
processTimeout is 20 minutes

我正在使用这个实例

public File convertFile(MultipartFile file) throws ConversionException {
try(InputStream docStream = file.getInputStream(); ByteArrayOutputStream pdfStream = new ByteArrayOutputStream()) {
boolean status = iConverter.convert(docStream, false).as(DocumentType.DOC).to(pdfStream, false).as(DocumentType.PDF).execute();
if(status) {
// conversion is success, send the response
File response = new File();
//InputStream responseStream = new ByteArrayInputStream(pdfStream.toByteArray());
response.setContentLength(pdfStream.size());
//response.setInputStream(responseStream);
response.setOutputStream(pdfStream);
return response;
} else {
LOGGER.error("Failed to convert word to pdf, conversion status is {}", status);
throw new ConversionException("failed to convert word to pdf");
}
} catch (FileSystemInteractionException fsie) {
LOGGER.error("documents4j file system interaction exception", fsie);
throw new ConversionException("File system exception", fsie);
} catch(IOException ioe) {
throw new ConversionException("Cannot read the input stream of file", ioe);
}
}

此多部分文件是 spring 多部分文件。我检查了documents4j用于转换的vb脚本,发现当wordDocument未正确关闭时会出现此错误。以下是 vb 脚本的片段,它是此错误的来源

' Close the source document.
wordDocument.Close WdDoNotSaveChanges
If Err <> 0 Then
WScript.Quit -3
End If
On Error GoTo 0

我不确定为什么会收到 FileSystemInteractionException

我能想到两个假设

  1. 我同时发送多个请求,文件被其他线程删除
  2. 我从 MultipartFile 对象获取输入流,并且多部分文件是临时文件,根据文档,用户负责将内容复制到持久存储。

spring official docs

如何解决此错误以及此错误的根本原因是什么。

最佳答案

导致此错误的原因可能有多种:

com.documents4j.throwables.FileSystemInteractionException: Could not access target file

Exception documentation here

您是否尝试将上传的多部分文件保存到临时文件,然后将此临时文件传递给转换器?我知道这是不必要的开销。但是,如果这有效,那么我们可以放心地假设当 IConverter 实例尝试访问输入“docstream”时,该输入并未完全填充,因此会出现错误。在这种情况下,您应该确保在尝试转换之前填充输入流,这应该可以解决您的问题。

如果即使在“基于文件”的转换场景中您也遇到此错误,请尝试以下步骤:

  1. 确保 MS Office 应用程序未运行(因为您在外部打开了 Word 文档)
  2. 确保物理机(而不仅仅是 JVM)上运行唯一一个 IConverter 实例
  3. 如果您将 Tomcat 作为服务运行(我假设您将其部署在 Tomcat 上),则您不是将 Tomcat 作为系统帐户服务运行,而是作为本地用户帐户运行。
  4. 在 Web 应用程序中,您应该创建一次 IConverter 实例(就像在单例类中一样),并且只要您的业务方法之一请求,它就应该返回相同的实例。另外,如果您预计会同时出现文档转换请求,请不要关闭转换器。

理想情况下,这些步骤之一应该可以解决您手头的问题,如果您仍然遇到此问题,请在评论中告诉我。

关于java - 文件系统交互异常: Could not access target file while using documents4j,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36281270/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com