编程语言
首页 > 编程语言> > java代码自动启动OpenOffice

java代码自动启动OpenOffice

作者:互联网

前言

之前用openOffice将docx转pdf实现在线预览,但每次都要自己手动cmd启动,看网上教程说可以下载Server 2003 Resource Kit Tools来自启动OpenOffice,但是这个连接已经失效并且下载不了了,就选择采取用java代码来启动OpenOffice,就只要我们电脑安装了openOffice就可以啦。

方法

* 在FileConvertUtil里加两个方法

//启动OpenOffice   servicePath 是OpenOffice的安装路径
    public static void startOpenOfficeService(String servicePath) {
        String command = servicePath + "program\\soffice -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\" -nofirststartwizard";
        try {
            Process pro = Runtime.getRuntime().exec(command);
        } catch (IOException e) {
            System.out.println("OpenOffice服务启动失败");
        }
    }


    /**
     *关闭OpenOffice
     **/
    public static void shutdownOpenOfficeService() {
        try {
            Process pro = Runtime.getRuntime().exec("tasklist");
            Scanner in = new Scanner(pro.getInputStream());
            while(in.hasNext()) {
                String proString = in.nextLine();
                if(proString.contains("soffice.exe")) {
                    String cmd = "taskkill /f /im soffice.exe";
                    pro = Runtime.getRuntime().exec(cmd);
                    System.out.println("soffice.exe关闭");
                }
                if(proString.contains("soffice.bin")) {
                    String cmd = "taskkill /f /im soffice.bin";
                    pro = Runtime.getRuntime().exec(cmd);
                    System.out.println("soffice.bin关闭");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

* 业务层代码调这个方法

//servicePath 是OpenOffice的安装路径
private static final String servicePath = "C:\\Program Files (x86)\\OpenOffice 4\\";

//在线预览
 public OutputStream onlinePreview(@RequestParam("url") String url, HttpServletResponse response) throws Exception {
        //获取文件类型

        String suffix = url.substring(url.lastIndexOf('.') + 1);

        if (!suffix.equals("txt") && !suffix.equals("doc") && !suffix.equals("docx") && !suffix.equals("xls")
                && !suffix.equals("xlsx") && !suffix.equals("ppt") && !suffix.equals("pptx") && !suffix.equals("sheet") && !suffix.equals("pdf")) {
            throw new Exception("文件格式不支持预览");
        }
        url = url.replace(STORE_PATH, LOCAL_STORE_PATH);
        InputStream in = null;
        //如果是pdf文件,则不用转,直接展示
        if (suffix.equals("pdf")) {
            in = new BufferedInputStream(new FileInputStream(url));
        } else {
            //docx文件则先启动openOffice
            FileConvertUtil.startOpenOfficeService(servicePath);
            in = FileConvertUtil.convertLocaleFile(url, suffix);
            //关闭openOffice
            FileConvertUtil.shutdownOpenOfficeService();
        }

        // url = String.format("http://"+url);


        OutputStream outputStream = response.getOutputStream();
        //创建存放文件内容的数组
        byte[] buff = new byte[1024];
        //所读取的内容使用n来接收
        int n;
        //当没有读取完时,继续读取,循环
        while ((n = in.read(buff)) != -1) {
            //将字节数组的数据全部写入到输出流中
            outputStream.write(buff, 0, n);
        }
        //强制将缓存区的数据进行输出
        outputStream.flush();
        //redisTemplate.opsForValue().set("Knowledge:" + url, outputStream.toString());
        //关流
        outputStream.close();
        in.close();

        return outputStream;
    }

ok!

标签:java,suffix,url,代码,equals,OpenOffice,&&,String
来源: https://www.cnblogs.com/jasmine-e/p/16317938.html