openoffice连接失败

96
吹奏一池春水
2018.08.20 11:53* 字数 135
问题

项目部署到新的测试环境后,执行word转pdf时连接失败报错

java.net.ConnectException: connection failed: socket,host=localhost,port=8100,tcpNoDelay=1: java.net.ConnectException: Connection refused

原因

openoffice连接代码写法

// 客户端连接
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
// 源码
public class SocketOpenOfficeConnection extends AbstractOpenOfficeConnection {
    
    public static final String DEFAULT_HOST = "localhost";
    public static final int DEFAULT_PORT = 8100;

    public SocketOpenOfficeConnection() {
        this(DEFAULT_HOST, DEFAULT_PORT);
    }

    public SocketOpenOfficeConnection(int port) {
        this(DEFAULT_HOST, port);
    }

    public SocketOpenOfficeConnection(String host, int port) {
        super("socket,host=" + host + ",port=" + port + ",tcpNoDelay=1");
    }
}

由源码看出默认连接主机名是"localhost",之后查看服务器/etc/hosts并未配置127.0.0.1主机名为localhost

解决方法

方法一:在/etc/hosts中添加127.0.0.1对应主机名localhost配置
方法二:客户端连接指定ip

OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
BugLog