问题背景描述
最近在使用springboot结合jodconverter连接libreoffice以实现一个在线文件预览功能(将各种格式文件转成PDF格式)时,出现一直连接不上的情况,在启动项目的时候抛出异常:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'localOfficeManager' defined in class path resource [org/jodconverter/boot/autoconfigure/JodConverterLocalAutoConfiguration.class]: Invocation of init method failed; nested exception is org.jodconverter.office.OfficeException: Could not establish connection
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1771)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:277)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1255)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1175)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:857)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:760)
... 66 common frames omitted
Caused by: org.jodconverter.office.OfficeException: Could not establish connection
at org.jodconverter.office.OfficeProcessManager.doStartProcessAndConnect(OfficeProcessManager.java:110)
at org.jodconverter.office.OfficeProcessManager.access$100(OfficeProcessManager.java:40)
at org.jodconverter.office.OfficeProcessManager$4.call(OfficeProcessManager.java:261)
at org.jodconverter.office.OfficeProcessManager$4.call(OfficeProcessManager.java:256)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.jodconverter.office.RetryTimeoutException: org.jodconverter.office.OfficeConnectionException: Connection failed: 'socket,host=127.0.0.1,port=8100,tcpNoDelay=1'; null
at org.jodconverter.office.AbstractRetryable.execute(AbstractRetryable.java:64)
at org.jodconverter.office.ConnectRetryable.execute(ConnectRetryable.java:26)
at org.jodconverter.office.OfficeProcessManager.doStartProcessAndConnect(OfficeProcessManager.java:104)
... 7 common frames omitted
Caused by: org.jodconverter.office.OfficeConnectionException: Connection failed: 'socket,host=127.0.0.1,port=8100,tcpNoDelay=1'; null
at org.jodconverter.office.OfficeConnection.connect(OfficeConnection.java:170)
at org.jodconverter.office.ConnectRetryable.attempt(ConnectRetryable.java:61)
at org.jodconverter.office.AbstractRetryable.execute(AbstractRetryable.java:57)
... 9 common frames omitted
Caused by: com.sun.star.lang.IllegalArgumentException: null
at com.sun.star.comp.bridgefactory.BridgeFactory.createBridge(BridgeFactory.java:135)
at org.jodconverter.office.OfficeConnection.connect(OfficeConnection.java:118)
... 11 common frames omitted
检查了日志发现jodconverter已经检测到了soffice进程,但就是连接不上,在确定端口没错的情况下开始检查需要的依赖的jar包是否导入。
解决方法
查阅相关资料发现除了jodconverter还需要导入如下依赖:
如果用openoffice:
compile group: 'org.openoffice', name: 'unoil', version: '4.1.2'
compile group: 'org.openoffice', name: 'ridl', version: '4.1.2'
compile group: 'org.openoffice', name: 'jurt', version: '4.1.2'
compile group: 'org.openoffice', name: 'juh', version: '4.1.2'
如果用libreoffice:
compile group: 'org.libreoffice', name: 'officebean', version: '6.3.2'
这些依赖是openoffice和libreoffice官方提供的,如果你要用java去连接,则必须导入。
我这里就只提供gradle的导入,如果你是maven请自行去maven repository搜openoffice,找到上述4个依赖(我好懒,哈哈)。