OpenOffice转换Office

文章描述了一个Java工具类`OpenOfficeUtils`,用于在Linux和Windows环境下通过Maven依赖`jodconverter`启动OpenOffice服务并进行文件转换,支持本地和远程调用。转换过程中涉及Socket连接,转换不同格式的文件,如从docx转为pdf。
摘要由CSDN通过智能技术生成

1,启动本地OpenOffice文件

Linux启动:nohup /opt/openoffice4/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &  后台启动
windows启动(进入program目录中):soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;"-nofirststartwizard

2,maven依赖

        <!--openoffice引lib包下的 -->
        <dependency>
            <groupId>org.artofsolving.jodconverter</groupId>
            <artifactId>jodconverter-core</artifactId>
            <version>1.0</version>
            <scope>system</scope>
            <systemPath>${basedir}/src/main/resources/lib/jodconverter-2.2.2.jar</systemPath>
        </dependency>


        <dependency>
            <groupId>org.artofsolving.jodconverter</groupId>
            <artifactId>jodconverter-cli-2.2.2</artifactId>
            <version>1.0</version>
            <scope>system</scope>
            <systemPath>${basedir}/src/main/resources/lib/jodconverter-cli-2.2.2.jar</systemPath>
        </dependency>

3,工具类:

import com.artofsolving.jodconverter.DefaultDocumentFormatRegistry;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.DocumentFormatRegistry;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
import com.sx.conf.SxAppConfig;

import org.apache.commons.lang.StringUtils;

import java.io.*;
import java.net.ConnectException;

import javax.servlet.http.HttpServletResponse;

/**
 * @author josnow
 * @version 1.0.0
 * @date 2017年5月9日 下午12:38:39
 * @desc openoffice转换工具
 */
public class OpenOfficeUtils {

    public static final String LOCAL_HOST = "localhost";
    public static final int LOCAL_PORT = 8100;

    // Format
    public static DocumentFormatRegistry formatFactory = new DefaultDocumentFormatRegistry();

    /**
     * @param inputFilePath  待转换的文件路径
     * @param outputFilePath 输出文件路径
     * @return void
     * @author old six
     * @Description: TODO(本地调用openoffice转换)
     * @Date 16:03 2020/11/2
     */
    public static void convert(String inputFilePath, String outputFilePath) throws ConnectException {

        convert(inputFilePath, outputFilePath, LOCAL_HOST, LOCAL_PORT);
    }

    /**
     * @param inputFilePath  待转换的文件路径
     * @param outputFilePath 输出文件路径
     * @param connectIp      远程调用ip
     * @param connectPort    远程调用端口
     * @return void
     * @author old six
     * @Description: TODO(远程调用openoffice转换)
     * @Date 16:02 2020/11/2
     */
    public static void convert(String inputFilePath, String outputFilePath, String connectIp, int connectPort)
            throws ConnectException {

        if (StringUtils.isEmpty(inputFilePath) || StringUtils.isEmpty(outputFilePath)
                || StringUtils.isEmpty(connectIp)) {
            throw new IllegalArgumentException("参数异常!!");
        }
        OpenOfficeConnection connection = new SocketOpenOfficeConnection(connectIp, connectPort);
        connection.connect();

        DocumentConverter converter = getConverter(connectIp, connection);

        converter.convert(new File(inputFilePath), new File(outputFilePath));
        connection.disconnect();
    }

    /**
     * @param inputStream         输入流
     * @param inputFileExtension  待转换文件的扩展名,例如: xls,doc
     * @param outputStream        输出流
     * @param outputFileExtension 输出文件扩展名,例如:pdf
     * @return void
     * @author old six
     * @Description: TODO(任何格式转换 - 本地调用openoffice)
     * @Date 16:05 2020/11/2
     */
    public static void convert(InputStream inputStream, String inputFileExtension, OutputStream outputStream,
            String outputFileExtension) throws ConnectException {

        convert(inputStream, inputFileExtension, outputStream, outputFileExtension, LOCAL_HOST, LOCAL_PORT);
    }

    /**
     * @param inputStream         带转换输入流
     * @param inputFileExtension  待转换文件的扩展名,例如: xls,doc
     * @param outputStream        转换后输出流
     * @param outputFileExtension 输出文件扩展名,例如:pdf
     * @param connectIp           远程调用ip
     * @param connectPort         远程调用端口
     * @return void
     * @author old six
     * @Description: TODO(任何格式转换 - 远程调用openoffice)
     * @Date 16:06 2020/11/2
     */
    public static void convert(InputStream inputStream, String inputFileExtension, OutputStream outputStream,
            String outputFileExtension, String connectIp, int connectPort) throws ConnectException {

        if (inputStream == null || StringUtils.isEmpty(inputFileExtension) || outputStream == null
                || StringUtils.isEmpty(outputFileExtension) || StringUtils.isEmpty(connectIp)) {
            throw new IllegalArgumentException("参数异常!!");
        }

        OpenOfficeConnection connection = new SocketOpenOfficeConnection(connectIp, connectPort);
        connection.connect();
        DocumentConverter converter = getConverter(connectIp, connection);

        converter.convert(inputStream, formatFactory.getFormatByFileExtension(inputFileExtension), outputStream,
                formatFactory.getFormatByFileExtension(outputFileExtension));
        connection.disconnect();
    }

    /**
     * @param connectIp
     * @param connection
     * @return com.artofsolving.jodconverter.DocumentConverter
     * @author old six
     * @Description: TODO(创建远程连接)
     * @Date 16:09 2020/11/2
     */
    private static DocumentConverter getConverter(String connectIp, OpenOfficeConnection connection) {

        DocumentConverter converter = "localhost".equals(connectIp) || "127.0.0.1".equals(connectIp)
                || "0:0:0:0:0:0:0:1".equals(connectIp) ? new OpenOfficeDocumentConverter(connection)
                        : new StreamOpenOfficeDocumentConverter(connection);
        return converter;
    }

    /***
     * 测试方法
     * 
     * @param args
     */
    public static void main(String[] args) {
        // OpenOfficeUtils.convert("E:\\Desktop\\改造分布式session要点 - 副本.docx",
        // "E:\\Desktop\\aaawww.pdf", "10.100.100.2", 8100);
        try {
            // OpenOfficeUtils.convert("E:\\Desktop\\本地服务器.xlsx", "E:\\Desktop\\笔试准考证.pdf",
            // "10.100.100.2", 8100);
            // OpenOfficeUtils.convert("E:\\Desktop\\本地服务器.xlsx", "E:\\Desktop\\笔试准考证.pdf",
            // "10.100.100.2", 8100);
         
                // OpenOfficeUtils.convert(new FileInputStream("E:\\Desktop\\本地服务器.xlsx"),
                // "XLSX", new FileOutputStream("E:\\Desktop\\111.pdf"), "PDF",
                // "192.168.43.102", 8100);
                try {
                    OpenOfficeUtils.convert(new FileInputStream("C:\\Users\\Administrator\\Desktop\\外阜登记失业人员分析报告.docx"), "docx",
                            new FileOutputStream("C:\\Users\\Administrator\\Desktop\\23\\0000000011.pdf"), "PDF");
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
//            OpenOfficeUtils.convert("C:\\Users\\admin\\Desktop\\2020十一月的考试-准考证 (5).doc", "C:\\Users\\admin\\Desktop\\2020十一月的考试-准考证 (5).html");    
          
        } catch (ConnectException e) {
            e.printStackTrace();
        }

    }

    /**
     * 将pdf下载到本地
     */
    public static void pdfdownload(HttpServletResponse response, String exam_id) throws IOException {
        FileInputStream in = null;
        OutputStream out = null;
        try {
            // 获取文件名
            String fileNa = new String("体测准考证.pdf".getBytes("utf-8"), "ISO-8859-1");
            // 设置响应头,控制浏览器下载该文件
            response.setHeader("Content-disposition", "attachment; filename=" + fileNa);
            // 读取要下载的文件,保存到文件输入流
            in = new FileInputStream(System.getProperty("user.dir") + SxAppConfig.getEXAMPDFURI() + exam_id + ".pdf");
            // 创建输出流
            out = response.getOutputStream();
            // 缓存区
            byte buffer[] = new byte[1024];
            int len = 0;
            // 循环将输入流中的内容读取到缓冲区中
            while ((len = in.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }
        } finally {
            // 关闭
            in.close();
            out.close();
        }

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值