1

我正在尝试基于 LibreOffice 4.0 实现 Office 一个 PDF 转换系统,并且总是得到 ErrorCodeIOException(尝试了文件格式 doc、docx、txt),我的堆栈跟踪如下:

com.sun.star.task.ErrorCodeIOException: 

at com.sun.star.lib.uno.environments.remote.Job.remoteUnoRequestRaisedException(Job.java:177)
at com.sun.star.lib.uno.environments.remote.Job.execute(Job.java:143)
at com.sun.star.lib.uno.environments.remote.JobQueue.enter(JobQueue.java:335)
at com.sun.star.lib.uno.environments.remote.JobQueue.enter(JobQueue.java:304)
at com.sun.star.lib.uno.environments.remote.JavaThreadPool.enter(JavaThreadPool.java:91)
at com.sun.star.lib.uno.bridges.java_remote.java_remote_bridge.sendRequest(java_remote_bridge.java:639)
at com.sun.star.lib.uno.bridges.java_remote.ProxyFactory$Handler.request(ProxyFactory.java:151)
at com.sun.star.lib.uno.bridges.java_remote.ProxyFactory$Handler.invoke(ProxyFactory.java:133)
at com.sun.proxy.$Proxy49.storeAsURL(Unknown Source)
at Framework.Config.DocumentConverter.traverse(DocumentConverter.java:103)
at Framework.Config.DocumentConverter.main1(DocumentConverter.java:184)
at Framework.DAO.CRUD_Generic.ping(CRUD_Generic.java:71)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.apache.struts2.json.JSONInterceptor.invoke(JSONInterceptor.java:248)
at org.apache.struts2.json.JSONInterceptor.intercept(JSONInterceptor.java:133)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263)
at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:270)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:190)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:498)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1822)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)

代码如下:

导入 com.sun.star.beans.PropertyValue;导入 com.sun.star.uno.UnoRuntime;

import java.io.File;
import ooo.connector.BootstrapSocketConnector;

public class DocumentConverter {

/**
 * Containing the loaded documents
 */
static com.sun.star.frame.XComponentLoader xCompLoader = null;
/**
 * Containing the given type to convert to
 */
static String sConvertType = "";
/**
 * Containing the given extension
 */
static String sExtension = "";
/**
 * Containing the current file or directory
 */
static String sIndent = "";
/**
 * Containing the directory where the converted files are saved
 */
static String sOutputDir = "";

/**
 * Traversing the given directory recursively and converting their files to
 * the favoured type if possible
 *
 * @param fileDirectory Containing the directory
 */
public static void traverse(File fileDirectory) {
    // Testing, if the file is a directory, and if so, it throws an exception
    if (!fileDirectory.isDirectory()) {
        throw new IllegalArgumentException(
                "not a directory: " + fileDirectory.getName());
    }

    // Prepare Url for the output directory
    File outdir = new File(DocumentConverter.sOutputDir);
    String sOutUrl = "file:///" + outdir.getAbsolutePath().replace('\\', '/');

    System.out.println("\nThe converted documents will stored in \""
            + outdir.getPath() + "!");

    System.out.println(sIndent + "[" + fileDirectory.getName() + "]");
    sIndent += "  ";

    // Getting all files and directories in the current directory
    File[] entries = fileDirectory.listFiles();


    // Iterating for each file and directory
    for (int i = 0; i < entries.length; ++i) {
        // Testing, if the entry in the list is a directory
        if (entries[ i].isDirectory()) {
            // Recursive call for the new directory
            traverse(entries[ i]);
        } else if (entries[i].getName().contains(".doc") || entries[i].getName().contains(".txt")) {
            // Converting the document to the favoured type
            try {
                // Composing the URL by replacing all backslashs
                String sUrl = "file:///"
                        + entries[ i].getAbsolutePath().replace('\\', '/');

                // Loading the wanted document
                com.sun.star.beans.PropertyValue propertyValues[];

                Object oDocToStore =
                        DocumentConverter.xCompLoader.loadComponentFromURL(
                        sUrl, "_blank", 0, new PropertyValue[0]);

                // Getting an object that will offer a simple way to store
                // a document to a URL.
                com.sun.star.frame.XStorable xStorable =
                        (com.sun.star.frame.XStorable) UnoRuntime.queryInterface(
                        com.sun.star.frame.XStorable.class, oDocToStore);

                // Preparing properties for converting the document
                propertyValues = new com.sun.star.beans.PropertyValue[1];
                // Setting the filter name
                propertyValues[0] = new com.sun.star.beans.PropertyValue();
                propertyValues[0].Name = "FilterName";
                propertyValues[0].Value = "writer_pdf_Export";

                // Appending the favoured extension to the origin document name
                int index1 = sUrl.lastIndexOf('/');
                int index2 = sUrl.lastIndexOf('.');
                String sStoreUrl = sOutUrl + sUrl.substring(index1, index2 + 1)
                        + DocumentConverter.sExtension;

                // Storing and converting the document
                xStorable.storeAsURL(sStoreUrl, propertyValues);

                // Closing the converted document. Use XCloseable.clsoe if the
                // interface is supported, otherwise use XComponent.dispose
                com.sun.star.util.XCloseable xCloseable =
                        (com.sun.star.util.XCloseable) UnoRuntime.queryInterface(
                        com.sun.star.util.XCloseable.class, xStorable);

                if (xCloseable != null) {
                    xCloseable.close(false);
                } else {
                    com.sun.star.lang.XComponent xComp =
                            (com.sun.star.lang.XComponent) UnoRuntime.queryInterface(
                            com.sun.star.lang.XComponent.class, xStorable);

                    xComp.dispose();
                }
            } catch (Exception e) {
                e.printStackTrace(System.err);
                return;
            }

            System.out.println(sIndent + entries[ i].getName());
        }
    }

    sIndent = sIndent.substring(2);
}

/**
 * Bootstrap UNO, getting the remote component context, getting a new
 * instance of the desktop (used interface XComponentLoader) and calling the
 * static method traverse
 *
 * @param args The array of the type String contains the directory, in which
 * all files should be converted, the favoured converting type and the
 * wanted extension
 */
public static void main1(String args[]) {
    if (args.length < 3) {
        System.out.println("usage: java -jar DocumentConverter.jar "
                + "\"<directory to convert>\" \"<type to convert to>\" "
                + "\"<extension>\" \"<output_directory>\"");
        System.out.println("\ne.g.:");
        System.out.println("usage: java -jar DocumentConverter.jar "
                + "\"c:/myoffice\" \"swriter: MS Word 97\" \"doc\"");
        return;
    }

    com.sun.star.uno.XComponentContext xContext = null;

    try {
        // get the remote office component context
        xContext = BootstrapSocketConnector.bootstrap("C:\\Program Files (x86)\\LibreOffice 4.0\\program");
        System.out.println("Connected to a running office ...");

        // get the remote office service manager
        com.sun.star.lang.XMultiComponentFactory xMCF =
                xContext.getServiceManager();

        Object oDesktop = xMCF.createInstanceWithContext(
                "com.sun.star.frame.Desktop", xContext);

        xCompLoader = (com.sun.star.frame.XComponentLoader) UnoRuntime.queryInterface(com.sun.star.frame.XComponentLoader.class,
                oDesktop);

        // Getting the given starting directory
        File file = new File(args[0]);

        // Getting the given type to convert to
        sConvertType = args[1];

        // Getting the given extension that should be appended to the
        // origin document
        sExtension = args[2];

        // Getting the given type to convert to
        sOutputDir = args[3];

        // Starting the conversion of documents in the given directory
        // and subdirectories
        traverse(file);


    } catch (Exception e) {
        e.printStackTrace(System.err);
    }
}
}

例外是在以下行中生成的:

xStorable.storeAsURL(sStoreUrl, propertyValues);

该代码基于 libreofice api 的原始示例:

http://api.libreoffice.org/examples/java/DocumentHandling/DocumentConverter.java

如果您能提供帮助,请提前致谢

4

1 回答 1

9

换行

 xStorable.storeAsURL(sStoreUrl, propertyValues);

 xStorable.storeToURL(sStoreUrl, propertyValues);

纠正了问题

于 2013-07-10T02:00:06.027 回答