java连接远程服务器上的OpenOffice4被拒绝 connection failed连接失败

通过java远程连接OpenOffice4,需要在启动OpenOffice4时,将host=的ip地址写为0.0.0.0就可以通过java远程连接了

linux后台启动命令:

nohup /opt/openoffice4/program/soffice -headless -accept="socket,host=0.0.0.0,port=8100;urp;" -nofirststartwizard &

java中连接的工具类

 public static File officeConvertPdf(File inputFile, File outFilePath) {
        try {
            String outFilePath_ = outFilePath.getPath().concat(File.separator).concat(inputFile.getName().replaceAll("[.]{1}.*$", ".pdf"));
            File outFile = new File(outFilePath_);
            //连接远程服务器上的openoffice服务进行文档格式转换
            OpenOfficeConnection connection = new SocketOpenOfficeConnection("IP(127.0.0.1)", 8100);
            System.err.println("开启连接");
            connection.connect();
            System.err.println("连接成功");
            DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
            // 转换
            converter.convert(inputFile, outFile);
            // 关闭OpenOffice服务的进程
            connection.disconnect();
            return outFile;
        } catch (Exception e) {
            e.printStackTrace();
            log.error(errInfo(e));
        }
        return null;
    }


    private static String errInfo(Exception e) {
        StringWriter sw = null;
        PrintWriter pw = null;
        try {
            sw = new StringWriter();
            pw = new PrintWriter(sw);
            // 将出错的栈信息输出到printWriter中
            e.printStackTrace(pw);
            pw.flush();
            sw.flush();
        } finally {
            if (sw != null) {
                try {
                    sw.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (pw != null) {
                pw.close();
            }
        }
        return sw.toString();
    }


    public static File officeConvert(String inputFile, String outFilePath) {
        String suffix = inputFile.substring(inputFile.lastIndexOf(".") + 1);
        File inputFile_ = new File(inputFile);
        File outFilePath_ = new File(outFilePath);
        if (!"pdf".equals(suffix)) {
            File outFile = officeConvertPdf(inputFile_, outFilePath_);
            return outFile;
        } else {
            return createFile(inputFile, outFilePath);
        }
    }

    public static File createFile(String inputFilePath, String outFilePath) {
        File destFile = new File(outFilePath);
        if (!destFile.exists()) {
            destFile.mkdirs();
        }
        try {
            File inputFile = new File(inputFilePath);
            String outFile = outFilePath + inputFile.getName();
            if (inputFile.exists()) {
                FileInputStream ins = new FileInputStream(inputFile);
                FileOutputStream out = new FileOutputStream(outFile);
                byte[] b = new byte[1024];
                int n = 0;
                while ((n = ins.read(b)) != -1) {
                    out.write(b, 0, n);
                }
                ins.close();
                out.close();
            }
            return new File(outFile);
        } catch (Exception e) {
            System.out.println("文件复制出错!inputFilePath:" + inputFilePath);
            e.printStackTrace();
        }
        return null;
    }

你可能感兴趣的:(java连接远程服务器上的OpenOffice4被拒绝 connection failed连接失败)