document4j框架使用问题-java.util.concurrent.ExecutionException: Could not complete conversion

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/achenyuan/article/details/82424293

document4j框架使用问题-java.util.concurrent.ExecutionException: Could not complete conversion

前言

使用document4j调用office将其它格式的文件转换成PDF时,有使用报错比如

java.util.concurrent.ExecutionException: Could not complete conversion
    at com.documents4j.job.FailedConversionFuture.get(FailedConversionFuture.java:35)
    ...
Caused by: com.documents4j.throwables.ConversionInputException: The input file seems to be corrupt
    at com.documents4j.util.Reaction$ConversionInputExceptionBuilder.make(Reaction.java:159)
    ...

出现这个错误是因为,如果我们把java程序当作window服务使用时,由于office不提供windows service的上下文环境,导致调用时输入流会中断。

解决办法

从document官方文档上得知

documents4j might malfunction when run as a Windows service together with MS Office conversion. Note that MS Office does not officially support execution in a service context. When run as a service, MS Office is always started with MS Window's local service account which does not configure a desktop. However, MS Office expects a desktop to exist in order to run properly. Without such a desktop configuration, MS Office will start up correctly but fail to read any input file. In order to allow MS Office to run in a service context, there are two possible approaches of which the first approach is more recommended:

On a 32-bit system, create the folder C:\Windows\System32\config\systemprofile\Desktop. On a 64-bit system, create the folder C:\Windows\SysWOW64\config\systemprofile\Desktop. Further information can be found on MSDN.
You can manipulate MS Window's registry such that MS Office applications are run with another account than the local service account. This approach is documented on MSDN. Note that this breaks MS Window's sandbox model and imposes additional security threats to the machine that runs MS Office.

当应用在window service上下文环境中运行,同时使用document4j的转换功能可能出现问题,MS Office不官方支持在上下文环境中使用,所以当我们的应用由服务的方式运行时,若使用document4j会报错。
一个典型的盒子,jenkins自动代部署时,执行java -jar test.jar启动test应用,应用就是在winows上下文环境中运行的。
有2种解决办法,第二种我没试过,尝试第一种有效,只介绍第一种方式。

  • win32位系统中,在C:\Windows\System32\config\systemprofile\位置下创建Desktop文件夹
  • win64位系统中,在C:\Windows\SysWOW64\config\systemprofile\位置下创建Desktop文件夹
    创建完成后重启电脑即可。
    真实有效。
    document4j官方文档

猜你喜欢

转载自blog.csdn.net/achenyuan/article/details/82424293