java - JODConverter问题

标签 java sockets netbeans japplet jodconverter

我正在使用 JODConverter 库 V 3.0 Beta 4 和 Open Office 3.4,并尝试将一些文件转换为 PDF/A-1 格式。然而,在 office manager 进程启动后,它只是挂起,什么也没有发生。这是输出:

Jul 26, 2012 12:04:03 PM org.artofsolving.jodconverter.office.ProcessPoolOfficeManager <init>
INFO: ProcessManager implementation is PureJavaProcessManager

C:\Users\Chris\AppData\Local\Temp\ArFile\PDF\blah.pdf : C:\Users\Chris\Documents\blah.txt

Jul 26, 2012 12:04:04 PM org.artofsolving.jodconverter.office.OfficeProcess start
INFO: starting process with acceptString 'socket,host=127.0.0.1,port=2002,tcpNoDelay=1' and profileDir 'C:\Users\Chris\AppData\Local\Temp\.jodconverter_socket_host-127.0.0.1_port-2002'
Jul 26, 2012 12:04:04 PM org.artofsolving.jodconverter.office.OfficeProcess start
INFO: started process

中间一行是打印输出文件名和用冒号分隔的输入文件名的输出,你可以看到它们是有效值...

这是我的转换器类:

package com.allcare.arfile;

import com.sun.star.beans.PropertyValue;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.document.DocumentFamily;
import org.artofsolving.jodconverter.document.DocumentFormat;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;


public class FileConverter 
{
OfficeManager officeManager;
String tempFilePath;

public FileConverter() 
{
    officeManager = new DefaultOfficeManagerConfiguration()
            //.setRetryTimeout(30000L)
            //.setTaskExecutionTimeout(60000L)
            .buildOfficeManager();
    tempFilePath = System.getProperty("java.io.tmpdir") + "\\ArFile\\PDF\\";
}

// if possible this function converts a file to a PDF/A-1 compliant file
public File convertToPdf(File inputFile, Log logger, User user)
{        
    if (isConvertableToPDF(inputFile.getName())) // REMEMBER TO CHANGE THE IF STATEMENT IN THE createMetadata() section to reflect this 
    {
        new File(tempFilePath).mkdirs();
        String filename = inputFile.getName();
        filename = filename.substring(0, filename.lastIndexOf("."));

        File outputFile = new File(tempFilePath + filename + ".pdf");

        System.out.println(outputFile + " : " + inputFile);

        officeManager.start(); // may tweak the start and stop code to appear elsewhere for additional efficiency

        DocumentFormat docFormat = new DocumentFormat("Portable Document Format", "pdf", "application/pdf");
        Map map = new HashMap();
        map.put("FilterName", "writer_pdf_Export");
        PropertyValue[] aFilterData = new PropertyValue[1];
        aFilterData[0] = new PropertyValue();
        aFilterData[0].Name = "SelectPdfVersion";
        aFilterData[0].Value = 1;
        map.put("FilterData", aFilterData);
        docFormat.setStoreProperties(DocumentFamily.TEXT, map);

        OfficeDocumentConverter docConverter = new OfficeDocumentConverter(officeManager);
        docConverter.convert(inputFile, outputFile, docFormat);

        officeManager.stop();

        return outputFile;
    }

    return inputFile;
}

// returns true if the file format is known to be convertible into the PDF/A-1 format
public boolean isConvertableToPDF(String filename)
{
    if (filename.endsWith(".doc") || filename.endsWith(".txt") || filename.endsWith(".xls") 
            || filename.endsWith(".ppt") || filename.endsWith(".docx"))
    {
        return true;
    }

    return false;
}

}

我正在使用 java 套接字,在我使用套接字之前转换器工作得很好,但是在我改用套接字之后它开始挂起。我不知道为什么...

最佳答案

问题可能出在这里:

officeManager.start(); // may tweak the start and stop code ...

如果代码正在等待启动的进程完成,它可能会等到您终止 OpenOffice 服务,然后它会导致程序的其余部分失败。 确保在后台服务中启动该服务,以便主程序可以继续。或者手动启动 OpenOffice 服务器(在您的程序之外)。不要 waitFor() 因为服务永远不会结束。

你上面的 System.out.println() 确实被打印出来了,所以到目前为止它仍然有效。

关于java - JODConverter问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11661422/

相关文章:

c - 如何用socket地址获取自己的IP地址?

netbeans - 更改 NetBeans "DoubleClick Highlighted"文本背景

c++ - 如何设置 Winsock UDP 套接字?

PHP 从 Windows 机器上的套接字读取

java - Netbeans 向导,方法 storeSettings 运行两次

java - 无法将 Tomcat 设置为项目的服务器

java - 将 2 equals JAXB 对象转换为另一个

'sort -V' 的 Java 替代方案

java - 使用 Spring Web 服务客户端解密消息

java - mvn 3.3.3 可能不支持父 POM 版本进行属性解析