如何使用远程jodconverter服务?

问题描述 投票:0回答:1

我将libreoffice服务部署到公共服务器上,成功启动并打开端口后,在我的项目上调用该服务,当我使用其execute方法时,报错:

2023-11-09 17:27:58.675 ERROR 15750 --- [ter-poolentry-0] o.j.remote.task.RemoteConversionTask     : Remote conversion failed.

java.lang.NullPointerException: null
    at java.base/java.util.Objects.requireNonNull(Objects.java:221) ~[na:na]
    at org.jodconverter.remote.task.RemoteConversionTask.execute(RemoteConversionTask.java:135) ~[jodconverter-remote-4.3.0.jar:4.3.0]
    at org.jodconverter.remote.office.RemoteOfficeManagerPoolEntry.doExecute(RemoteOfficeManagerPoolEntry.java:306) ~[jodconverter-remote-4.3.0.jar:4.3.0]
    at org.jodconverter.core.office.AbstractOfficeManagerPoolEntry.lambda$execute$0(AbstractOfficeManagerPoolEntry.java:83) ~[jodconverter-core-4.3.0.jar:4.3.0]
    at java.base/java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:264) ~[na:na]
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java) ~[na:na]
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[na:na]
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[na:na]
    at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]

org.jodconverter.core.office.OfficeException: Remote conversion failed
    at org.jodconverter.remote.task.RemoteConversionTask.execute(RemoteConversionTask.java:153)
    at org.jodconverter.remote.office.RemoteOfficeManagerPoolEntry.doExecute(RemoteOfficeManagerPoolEntry.java:306)
    at org.jodconverter.core.office.AbstractOfficeManagerPoolEntry.lambda$execute$0(AbstractOfficeManagerPoolEntry.java:83)
    at java.base/java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:264)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.NullPointerException
    at java.base/java.util.Objects.requireNonNull(Objects.java:221)
    at org.jodconverter.remote.task.RemoteConversionTask.execute(RemoteConversionTask.java:135)
    ... 7 more
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.alibaba.fastjson2.util.JDKUtils (file:/Users/jayleonc/Developer/Environment/apache-maven-3.8.4/maven-repo/com/alibaba/fastjson2/fastjson2/2.0.17/fastjson2-2.0.17.jar) to constructor java.lang.invoke.MethodHandles$Lookup(java.lang.Class,int)
WARNING: Please consider reporting this to the maintainers of com.alibaba.fastjson2.util.JDKUtils
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

这是一些代码, 配置:

jodconverter.
  remote.
    url: http://xxx.xx.xx.xxx:9980
  local: # office-home: /Applications/LibreOffice.app/Contents/
    # office-home: /Applications/LibreOffice.app/Contents/
    enabled: false
package com.xxx.supt.infra.config;

import org.jodconverter.core.DocumentConverter;
import org.jodconverter.core.office.OfficeManager;
import org.jodconverter.remote.RemoteConverter;
import org.jodconverter.remote.office.RemoteOfficeManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class LibreofficeConfig {

    @Value("${jodconverter.remote.url:''}")
    private String jodconverterUrl;

    @Bean(
        initMethod = "start",
        destroyMethod = "stop"
    )
    public OfficeManager officeManager() {
        RemoteOfficeManager.Builder builder = RemoteOfficeManager.builder();
        builder.urlConnection(jodconverterUrl);
        builder.poolSize(10);
        builder.taskExecutionTimeout(60000L);
        builder.taskQueueTimeout(60000L);
        return builder.build();
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnBean({OfficeManager.class})
    public DocumentConverter jodConverter(OfficeManager officeManager) {
        return RemoteConverter.make(officeManager);
    }

}
public void convertToPdfAndOutput(File file) throws IOException, OfficeException {
        File tempFile = File.createTempFile("temp", ".pdf");
        converter.convert(file).to(tempFile).execute();
        outputToResponse(tempFile, "application/pdf");
    }

当我调试这条线时,

converter.convert(file).to(tempFile).execute();

报错了,怎么回事?我应该怎么办?帮帮我,谢谢!

此外,当我在笔记本电脑上部署 libreoffice 服务时,一切正常! 我想知道文件传输过程是否有问题,我不确定,你有什么建议?

再次感谢!

java libreoffice jodconverter
1个回答
0
投票

这个问题解决了吗?我和你的问题几乎一样,我在同事电脑上启动libreoffice服务,是windows系统

@RestController
@RequestMapping("jodconverter")
public class JodConverterController {

    // TODO: 2024/5/16 不好用的
    @Autowired(required = false)
    private DocumentConverter documentConverter;

    @SneakyThrows
    @GetMapping("test1")
    public void test(HttpServletResponse response) {
        if (documentConverter == null) {
            throw new Exception("转换失败,请检查LibreOffice及系统相关配置");
        }

        File file = new File("D:\\ideaProjects\\LibreOfficeRemoteDemo\\test.doc"); // 替换为实际文件路径
        if (!file.exists()) {
            throw new FileNotFoundException("文件不存在");
        }

        InputStream inputStream = new FileInputStream(file);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        try {
            documentConverter
                    .convert(inputStream)
                    .as(DefaultDocumentFormatRegistry.DOC)
                    .to(byteArrayOutputStream)
                    .as(DefaultDocumentFormatRegistry.PDF)
                    .execute();
        } catch (Exception ex) {
            byteArrayOutputStream.close();
            throw ex;
        }

        byte[] pdfBytes = byteArrayOutputStream.toByteArray();
        response.setContentType("application/pdf");
        response.setContentLength(pdfBytes.length);
        response.getOutputStream().write(pdfBytes);
    }
}

yml:
jodconverter:
  remote:
    enabled: true
    url: http://192.168.2.4
    port: 8901
    connect-timeout: 5000  # 提高连接超时时间到5000毫秒(5秒)
    socket-timeout: 150000  # 提高套接字超时时间到15000毫秒(15秒)
    pool-size: 10
    task-execution-timeout: 120000 #设置单个任务的最大执行时间

org.apache.http.client.HttpResponseException:状态代码:302,原因短语:重定向 在 org.apache.http.client.fluid.Response.saveContent(Response.java:121) ~[fluid-hc-4.5.13.jar:4.5.13] 在 org.jodconverter.remote.task.RemoteConversionTask.execute(RemoteConversionTask.java:144) ~[jodconverter-remote-4.3.0.jar:4.3.0] 在 org.jodconverter.remote.office.RemoteOfficeManagerPoolEntry.doExecute(RemoteOfficeManagerPoolEntry.java:306) [jodconverter-remote-4.3.0.jar:4.3.0] 在 org.jodconverter.core.office.AbstractOfficeManagerPoolEntry.lambda$execute$0(AbstractOfficeManagerPoolEntry.java:83) [jodconverter-core-4.3.0.jar:4.3.0] 在 java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266) ~[na:1.8.0_301] 在 java.util.concurrent.FutureTask.run(FutureTask.java) ~[na:1.8.0_301] 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[na:1.8.0_301] 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) ~[na:1.8.0_301] 在 java.lang.Thread.run(Thread.java:748) ~[na:1.8.0_301]

© www.soinside.com 2019 - 2024. All rights reserved.