java 文档在线预览 Linux版本(openoffice)

1.下载openoffice

openoffice的下载地址:http://www.openoffice.org/

2.安装(记住自己的安装路径,待会要用到)
在这里插入图片描述
3. pom.xml

<dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
      <version>19.0</version>
</dependency>


<dependency>
      <groupId>com.github.livesense</groupId>
      <artifactId>jodconverter-core</artifactId>
      <version>1.0.5</version>
</dependency>

4.文档转pdf业务层

// 文件上传根目录
    @Value("${UPLOAD_URL}")
    private String UPLOAD_URL;

    @Value("${DOCUMENT_URL}")
    private String DOCUMENT_URL;

    /**
     * 使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 转化为pdf文件<br>
     *
     * @param inputFilePath  源文件路径,如:"e:/test.docx"
     * @param outputFilePath 目标文件路径,如:"e:/test_docx.pdf"
     * @return
     */
    @Override
    public Map openOfficeToPDF(String inputFilePath, String outputFilePath) {
        return office2pdf(inputFilePath, outputFilePath);
    }

    /**
     * 根据操作系统的名称,获取OpenOffice 的安装目录<br>
     * 如我的OpenOffice 安装在:C:/Program Files (x86)/OpenOffice 4
     *
     * @return OpenOffice 的安装目录
     */
    private String getOfficeHome() {
        String osName = System.getProperty("os.name");
        if (Pattern.matches("Linux.*", osName)) {
            return "/opt/openoffice.org3";
        } else if (Pattern.matches("Windows.*", osName)) {
            return "C:/Program Files (x86)/OpenOffice 4";
        } else if (Pattern.matches("Mac.*", osName)) {
            return "/Application/OpenOffice.org.app/Contents";
        }
        return null;
    }

    /**
     * 连接OpenOffice 并且启动OpenOffice
     *
     * @return
     */
    private OfficeManager getOfficeManager() {
        DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();
        // 获取OpenOffice 安装目录
        String officeHome = getOfficeHome();
        config.setOfficeHome(officeHome);
        // 启动OpenOffice的服务
        OfficeManager officeManager = config.buildOfficeManager();
        officeManager.start();
        return officeManager;
    }

    /**
     * 转换文件
     *
     * @param inputFile
     * @param outputFilePath_end
     * @param inputFilePath
     * @param outputFilePath
     * @param converter
     */
    private void converterFile(File inputFile, String outputFilePath_end, String inputFilePath, String outputFilePath, OfficeDocumentConverter converter) {
        File outputFile = new File(outputFilePath_end);
        // 假如目标路径不存在,则新建该路径
        if (!outputFile.getParentFile().exists()) {
            outputFile.getParentFile().mkdirs();
        }
        converter.convert(inputFile, outputFile);
        System.out.println("文件:" + inputFilePath + "\n转换为\n目标文件:" + outputFile + "\n成功!");
    }

    /**
     * 使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 转化为pdf文件<br>
     *
     * @param inputFilePath  源文件路径
     * @param outputFilePath 目标文件路径
     * @return
     */
    private Map office2pdf(String inputFilePath, String outputFilePath) {
        Map<String, String> tResultMap = new HashMap<>();
        tResultMap.put("status", "fail");
        boolean flag = false;
        OfficeManager officeManager = getOfficeManager();
        // 连接OpenOffice
        OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
        long begin_time = new Date().getTime();
        outputFilePath = DOCUMENT_URL + inputFilePath; //这两行代码不可以调换,你可以试试调换之后会出现什么错误
        inputFilePath = UPLOAD_URL + inputFilePath; //你也可以不建,他会自己建

        String outputFilePath_end = "";
        File tFile = new File(inputFilePath);
        File oFile = new File(outputFilePath);
        if (!tFile.exists() || tFile.isDirectory()) {
            tResultMap.put("msg", "该文件或文件夹不存在");
            return tResultMap;
        }
        if (!oFile.getParentFile().exists()) {
            oFile.getParentFile().mkdirs();// 新建文件夹
        }
        //file.transferTo(dest);// 文件写入

        if (null != inputFilePath) {
            File inputFile = new File(inputFilePath);
            // 判断目标文件路径是否为空
            if (null == outputFilePath) {
                // 转换后的文件路径
                outputFilePath_end = getOutputFilePath(inputFilePath);
                if (inputFile.exists()) {// 找不到源文件, 则返回
                    converterFile(inputFile, outputFilePath_end, inputFilePath, outputFilePath, converter);
                    flag = true;
                }
            } else {
                outputFilePath_end = getOutputFilePath(outputFilePath);
                if (inputFile.exists()) {// 找不到源文件, 则返回
                    converterFile(inputFile, outputFilePath_end, inputFilePath, outputFilePath, converter);
                    flag = true;
                }
            }
            officeManager.stop();
        } else {
            System.out.println("con't find the resource");
        }
        long end_time = new Date().getTime();
        System.out.println("文件转换耗时:[" + (end_time - begin_time) + "]ms");
        tResultMap.put("result", outputFilePath_end);
        tResultMap.put("status", "success");
        return tResultMap;
    }

    /**
     * 获取输出文件
     *
     * @param inputFilePath
     * @return
     */
    private String getOutputFilePath(String inputFilePath) {
        String outputFilePath = inputFilePath.replaceAll("." + getPostfix(inputFilePath), ".pdf");
        return outputFilePath;
    }

    /**
     * 获取inputFilePath的后缀名,如:"e:/test.pptx"的后缀名为:"pptx"<br>
     *
     * @param inputFilePath
     * @return
     */
    private String getPostfix(String inputFilePath) {
        return inputFilePath.substring(inputFilePath.lastIndexOf(".") + 1);
    }

5.预览pdf业务层

    public Map<String, String> readPdf(String filePath,HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String, String> tResultMap = new HashMap<>();
        tResultMap.put("status", "fail");
        //String filePath = request.getParameter("filePath");
        File filePic = new File(filePath);
        if(filePath == null){
            tResultMap.put("status", "路径为空");
            return tResultMap;
        }
        if (filePic.exists()) {
            FileInputStream is = new FileInputStream(filePic);
            int i = is.available(); // 得到文件大小
            byte data[] = new byte[i];
            is.read(data); // 读数据
            is.close();
            response.setContentType("application/pdf;charset=utf-8"); // 设置返回的文件类型
            OutputStream toClient = response.getOutputStream(); // 得到向客户端输出二进制数据的对象
            toClient.write(data); // 输出数据
            toClient.close();
        }
        return tResultMap;
    }
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值