zoukankan      html  css  js  c++  java
  • java执行命令并通过libreoffice软件的方式将word转化成HTML的详细步骤解析

     

    一、实现代码 :

    import org.apache.commons.io.IOUtils;
    
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.UUID;
    
    /**
     * 将word文档通过java调用命令的方式转化成HTML
     */
    public class DocConvertDemo {
        public static void main(String[] args) throws IOException {
            /**
             * D:softLibreOffice_6.0.6programsoffice:表示libreoffice安装路径
             * D:DesktopDocCloud	estDirhadoopInstall.doc:表示要转化的word文件
             */
            String fileName= "D:\Desktop\DocCloud\testDir\hadoopInstall.doc";
            String command = "D:\soft\LibreOffice_6.0.6\program\soffice --headless --invisible --convert-to html "+fileName;
            /**
             *workDir:表示转化之后的HTML文件保存的路径地址
             */
            String workDir = "D:\tmp\doccloud-Administrator\.stage\"+UUID.randomUUID().toString()+"\";
            File file = new File(workDir);
            //创建目录--因为是UUID所以不用判断目录一定不存在
            file.mkdirs();
            /**
             * command:命令
             * null:操作系统运行程序时通过envp 参数将系统环境变量传递给程序
             * file:命令在那个路径下执行
             */
            //返回过程对象--Process
            Process exec = Runtime.getRuntime().exec(command,null,file);
            //错误信息
            InputStream errorStream = exec.getErrorStream();
            //结果信息
            InputStream inputStream = exec.getInputStream();
            //IOUtils-直接将流转化成字符串
            String error = IOUtils.toString(errorStream);
            String result = IOUtils.toString(inputStream);
            //打印信息
            System.out.println(error);
            System.out.println(result);
        }
    }

    二、测试结果

    控制台

     磁盘上

    HTML打开内容如下(不同文件内容不同):

  • 相关阅读:
    [Dynamic Language] Python 命名参数
    [Dynamic Language] Python OrderedDict 保证按插入的顺序迭代输出
    div水平垂直居中
    项目小结(v1.2v1.4)
    如何能尽快看完一个网页的结构
    在项目中使用谁存储过程orTSQL语句
    UDP协议(数据报协议)
    风恋尘香欢迎你!!!
    .NEt牛人帮帮我!!!谢谢啦~~~
    LWUIT 简易漂亮的相册
  • 原文地址:https://www.cnblogs.com/pigdata/p/10305580.html
Copyright © 2011-2022 走看看