使用jodconverter连接openoffice / libreoffice失败出现Connection failed >> Could not establish connection异常

问题背景描述

最近在使用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个依赖(我好懒,哈哈)。

感谢您的提问!根据您的问题描述,您希望在 Linux 环境中使用 Java 语言和 jodconverter 插件,检查是否存在 libreoffice 进程。如果存在,则需要退出该进程;如果不存在,则需要开启 Excel 转 PDF 转换服务。下面是一段示例代码,供您参考: ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration; import org.artofsolving.jodconverter.office.OfficeManager; public class JodconverterDemo { public static void main(String[] args) throws Exception { // 检查是否存在 libreoffice 进程 boolean libreOfficeProcessExists = false; try { Process process = Runtime.getRuntime().exec("pgrep libreoffice"); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = reader.readLine()) != null) { libreOfficeProcessExists = true; System.out.println("LibreOffice process exists, stopping it..."); Runtime.getRuntime().exec("kill " + line); break; } reader.close(); process.waitFor(); } catch (IOException | InterruptedException e) { // do nothing } // 开启 Excel 转 PDF 转换服务 System.out.println("Starting OfficeManager..."); DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration(); configuration.setOfficeHome("/usr/bin/libreoffice"); configuration.setTaskExecutionTimeout(60000L); configuration.setTaskQueueTimeout(60000L); OfficeManager officeManager = configuration.buildOfficeManager(); officeManager.start(); System.out.println("OfficeManager started."); } } ``` 此示例代码使用 `pgrep` 命令检查是否存在 libreoffice 进程,并使用 `kill` 命令终止进程。如果您的环境中没有安装 `pgrep` 和 `kill` 命令,您可以使用其他方法检查并结束进程。 请注意,此示例代码假设您已经安装了 libreoffice,并且其路径为 `/usr/bin/libreoffice`。如果您使用的是其他 Office 软件,请将路径更改为相应的路径。 最后,请确保在代码中正确关闭 officeManager 线程,以确保代码的稳定性和安全性。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值