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

转载前序:
document4j需要使用office或是LibreOffice(未试)
——————————————————————

前言

使用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)
    ...
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

出现这个错误是因为,如果我们把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.

  • 1
  • 2
  • 3
  • 4

当应用在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官方文档
文章知识点与官方知识档案匹配,可进一步学习相关知识
Java技能树首页概览 149896 人正在系统学习中
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值