zoukankan      html  css  js  c++  java
  • java web通过openoffice实现文档网页预览(类似百度文库)

      最近研究了一下在网页上预览文档(包括office文档和txt、pdf),发现用openoffice+FlexPlayer实现比较理想,就参考了https://blog.csdn.net/ITBigGod/article/details/80300177#commentBox这个博客自己研究了一下。源码如下https://files.cnblogs.com/files/csdeblog/OpenOfficeDemo.zip

    需要安装,在程序中修改这个位置

     同时命令行开启openoffice,需要注意的是整个要转换的文件目录不能包含空格,而且txt需要转为utf-8,具体如下

    ChangeFileCode类源码

    package cn.com.mvc.util;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.Writer;
    
    public class ChangeFileCode {
    
        // 读取的文件
    
        private String fileIn;
    
        // 读取時文件用的编码
    
        private String fileInEn;
    
        // 写出的文件
    
        private String fileOut;
    
        // 写出時文件用的编码
    
        private String fileOutEn;
    
        /**
         * 
         * 获取源文件的编码
         * 
         * @param filePath
         *            源文件所在的绝对路径
         * 
         * @return
         * 
         */
    
        public String getFileEnCode(String filePath) {
    
            InputStream inputStream = null;
    
            String code = "";
    
            try {
    
                inputStream = new FileInputStream(filePath);
    
                byte[] head = new byte[3];
    
                inputStream.read(head);
    
                code = "gb2312";
    
                if (head[0] == -1 && head[1] == -2)
    
                    code = "UTF-16";
    
                if (head[0] == -2 && head[1] == -1)
    
                    code = "Unicode";
    
                if (head[0] == -17 && head[1] == -69 && head[2] == -65)
    
                    code = "UTF-8";
    
                System.out.println(code);
    
            } catch (Exception e) {
    
                e.printStackTrace();
    
            } finally {
    
                try {
    
                    inputStream.close();
    
                } catch (IOException e) {
    
                    e.printStackTrace();
    
                }
    
            }
    
            return code;
    
        }
    
        public void setFileIn(String fileInPath, String fileInEncoding) {
    
            this.setFileIn(fileInPath);
    
            this.setFileInEn(fileInEncoding);
    
        }
    
        public void setFileOut(String fileOutPath, String fileOutEncoding) {
    
            this.setFileOut(fileOutPath);
    
            this.setFileOutEn(fileOutEncoding);
    
        }
    
        public void start() {
    
            String str = this.read(fileIn, fileInEn);
    
            this.write(fileOut, fileOutEn, str);
    
        }
    
        /**
         * 
         * 读文件
         *
         * 
         * 
         * @param fileName
         * 
         * @param encoding
         * 
         */
    
        private String read(String fileName, String encoding) {
    
            try {
    
                BufferedReader in = new BufferedReader(new InputStreamReader(
    
                        new FileInputStream(fileName), encoding));
    
                String string = "";
    
                String str = "";
    
                while ((str = in.readLine()) != null) {
    
                    string += str + "
    ";
    
                }
    
                in.close();
    
                return string;
    
            } catch (Exception ex) {
    
                ex.printStackTrace();
    
            }
    
            return "";
    
        }
    
        /**
         * 
         * 写文件
         *
         * 
         * 
         * @param fileName
         * 
         *            新的文件名
         * 
         * @param encoding
         * 
         *            写出的文件的编码方式
         * 
         * @param str
         * 
         */
    
        private void write(String fileName, String encoding, String str) {
    
            try {
    
                Writer out = new BufferedWriter(new OutputStreamWriter(
    
                        new FileOutputStream(fileName), encoding));
    
                out.write(str);
    
                out.close();
    
            } catch (Exception ex) {
    
                ex.printStackTrace();
    
            }
    
        }
    
        public String getFileIn() {
    
            return fileIn;
    
        }
    
        public void setFileIn(String fileIn) {
    
            this.fileIn = fileIn;
    
        }
    
        public String getFileInEn() {
    
            return fileInEn;
    
        }
    
        public void setFileInEn(String fileInEn) {
    
            this.fileInEn = fileInEn;
    
        }
    
        public String getFileOut() {
    
            return fileOut;
    
        }
    
        public void setFileOut(String fileOut) {
    
            this.fileOut = fileOut;
    
        }
    
        public String getFileOutEn() {
    
            return fileOutEn;
    
        }
    
        public void setFileOutEn(String fileOutEn) {
    
            this.fileOutEn = fileOutEn;
    
        }
    }

    然后基本没啥问题了,openoffice和flowpaper的安装包可在https://share.weiyun.com/51jOQjB和https://share.weiyun.com/54GLAod下载。

  • 相关阅读:
    LearnMoreStudyLess《如何高效学习》斯科特.杨
    Asp.net 生成多个Excel打包zip进行下载(建立在Aspose.Cells.dll生成Excel,建立在ICSharpCode.SharpZipLib.dll打包zip)
    【面经】美团测试1,2,3面,一起来聊聊?
    【python】面试高频:浅拷贝 vs 深拷贝、'==' vs 'is'
    【图解Http 学习摘要】五、HTTPS 中的加密、证书介绍,不一直使用 HTTPS 的原因
    【图解Http 学习摘要】四、HTTP 缺点
    【图解Http 学习摘要】三、HTTP 协议基础、四次挥手
    【图解Http 学习摘要】二、IP,TCP 和 DNS、三次握手
    【图解Http 学习摘要】一、http介绍、TCP/IP 协议族
    【杂谈】关于常见架构的整理,单应用、微服务、SOA、分布式和集群
  • 原文地址:https://www.cnblogs.com/csdeblog/p/9906543.html
Copyright © 2011-2022 走看看