简体   繁体   English

FileSystemInteractionException: 使用文档时无法访问目标文件

[英]FileSystemInteractionException: Could not access target file while using documents4j

I am using documents4j to convert word documents to pdf and some time I am getting below exception我正在使用文档 4j 将 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]

After this exception any further requests are rejected by the documents4j library with below exception在此异常之后,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]

This is how I am doing the documents conversion.这就是我进行文档转换的方式。

I am instantiating an instance of LocalConverter我正在实例化 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

And I am using this instance like我正在使用这个实例

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);
        }
    }

This multipart file is spring multipart file.这个多部分文件是 spring 多部分文件。 I checked the vb script that documents4j uses for the conversion and I came to know that this error occurs when the wordDocument was not closed properly.我检查了文档 4j 用于转换的 vb 脚本,我知道当 wordDocument 没有正确关闭时会发生这个错误。 Below is the snippet from vb script which is the source of this error下面是来自 vb 脚本的片段,这是此错误的来源

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

I am not sure why I am getting FileSystemInteractionException .我不确定为什么会收到FileSystemInteractionException

There are two assumptions that I can think of我能想到的有两个假设

  1. I am sending multiple simultaneous requests and the file is deleted by some other thread我同时发送多个请求,文件被其他线程删除
  2. I am getting the inputstream from MultipartFile object and the multipart file is a temporary and as per the documentation the user is responsible to copy the content to a persistent storage.我从 MultipartFile 对象获取输入流,多部分文件是临时文件,根据文档,用户负责将内容复制到持久存储。

spring official docs 春季官方文档

How can I resolve this error and what is the root cause of this error.如何解决此错误以及此错误的根本原因是什么。

There can be multiple reasons for this error:此错误可能有多种原因:

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

Exception documentation here 异常文档在这里

Have you tried saving the uploaded multi-part file to a temporary file, then passing this temporary file to the converter?您是否尝试将上传的多部分文件保存到临时文件,然后将此临时文件传递给转换器? I am aware this is an unnecessary overhead.我知道这是不必要的开销。 However, if this works, then we can safely assume that the input "docstream" isn't populated completely when the IConverter instance tries to access it, and hence the error.但是,如果这有效,那么我们可以安全地假设当 IConverter 实例尝试访问它时输入“docstream”没有完全填充,因此出现错误。 In this case, you should ensure that the inputstream is populated before attempting conversion and that should resolve your issue.在这种情况下,您应该确保在尝试转换之前填充输入流,这应该可以解决您的问题。

If you get this error even for "file-based" conversion scenarios, try the following steps:如果即使在“基于文件”的转换场景中也出现此错误,请尝试以下步骤:

  1. Ensure that MS Office applications aren't running (because you opened a word document externally)确保 MS Office 应用程序未运行(因为您在外部打开了 Word 文档)
  2. Ensure that there is one and only one instance of IConverter running across the physical machine (and not just the JVM)确保有一个且只有一个 IConverter 实例在物理机上运行(而不仅仅是 JVM)
  3. If you are running Tomcat as a service (I'm assuming you're deploying this on Tomcat), you are running tomcat not as a SYSTEM account service but a local user account.如果您将 Tomcat 作为服务运行(我假设您正在 Tomcat 上部署它),那么您运行的 tomcat 不是作为 SYSTEM 帐户服务,而是作为本地用户帐户。
  4. In a web application, you should have the IConverter instance being created once (like in a singleton class), and it should return the same instance whenever requested by one of your business methods.在 Web 应用程序中,您应该创建一次 IConverter 实例(就像在单例类中一样),并且只要您的业务方法之一请求它,它就应该返回相同的实例。 Also, do not shut down the converter if you anticipate simultaneous document conversion requests.此外,如果您预计同时出现文档转换请求,请不要关闭转换器。

Ideally one of these steps should solve your issue at hand, let me know in the comments if you still face this issue.理想情况下,这些步骤之一应该可以解决您手头的问题,如果您仍然遇到此问题,请在评论中告诉我。

I am also facing the same error.我也面临同样的错误。 I used this PDF conversion inside a Spring Boot application and deployed it, in a Windows Server.我在 Spring Boot 应用程序中使用了这个 PDF 转换并将其部署在 Windows Server 中。 When I run this application manually (using java -jar), it's working perfectly fine.当我手动运行这个应用程序(使用 java -jar)时,它运行得非常好。 But, when I start this as a Windows Service(using winsw.exe), it is giving me the error:但是,当我将此作为 Windows 服务(使用 winsw.exe)启动时,它给了我错误:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM