使用libreoffice将Doc转换为docx文件时出错

时间:2017-08-07 12:46:50

标签: java converter libreoffice

我目前正致力于将doc转换为java项目中的docx文件。 我使用com.sun.star中的一些lib,我设法在上一次迭代中工作,但现在一切都在第一次转换尝试时崩溃。

我的代码是:

            String sourceUrl = "file:///" + listOfFile[i].getAbsolutePath().replace('\\', fileSeparator);
            String targetUrl = "file:///" + listOfFile[i].getAbsolutePath().replace('\\', fileSeparator) + 'x';

            // Setup the source file for libreoffice
            PropertyValue propertyValue[] = new PropertyValue[1];
            propertyValue[0] = new PropertyValue();
            propertyValue[0].Name = "Hidden";
            propertyValue[0].Value = Boolean.TRUE;

            Object oDocToStor = DocConverter.xCompLoader.loadComponentFromURL(sourceUrl, "_blank", 0, propertyValue);

            XStorable xStorable = UnoRuntime.queryInterface(XStorable.class, oDocToStor);

            // Setting up the converter by giving him the source file qnd the tqrget type
            propertyValue = new PropertyValue[2];
            propertyValue[0] = new PropertyValue();
            propertyValue[0].Name = "Overwrite";
            propertyValue[0].Value = Boolean.TRUE;
            propertyValue[1] = new PropertyValue();
            propertyValue[1].Name = "FilterName";
            propertyValue[1].Value = "MS Word 2007 XML";

            // Apply convertion !!! It crash at this step !!!
            xStorable.storeAsURL(targetUrl, propertyValue);

            XCloseable xCloseable = UnoRuntime.queryInterface(XCloseable.class, xStorable);
            if (xCloseable != null) {
                xCloseable.close();
            } else {
                XComponent xComp = UnoRuntime.queryInterface(XComponent.class, xStorable);
                xComp.dispose();
            }

我得到以下异常:

java.lang.AssertionError: com.sun.star.task.ErrorCodeIOException: SfxBaseModel::impl_store <file:///D:\my\file.docx> failed: 0x31c
    at org.junit.Assert.fail(Assert.java:93)
    at fr.gouv.travail.utils.service.impl.DocConverterTest.testDirectory(DocConverterTest.java:67)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:254)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

当我第二次启动它时,我得到一个空指针异常,因为第一次迭代不会在libreoffice中释放ressource而无法再次打开它们。

我的套接字看起来已正确打开,我的文件位于严格位置,因为我定位了一个完整的非空目录,同事也没有收到错误。

编辑:我检查了我的文件夹中的所有写入权限,一切都很好。

提前感谢您的帮助:)

1 个答案:

答案 0 :(得分:0)

感谢Aaron,我发现问题在于注入了fileSeparator变量,我希望将其设置为&#39; /&#39;在我的属性文件中,但似乎最终为&#39; \&#39;在我的debuger。如果我发现更多内容,我稍后会对此进行调查并编辑我的qnser。