JAVA实现word doc docx pdf excel的在线浏览 - 仿百度文库 源码-程序员宅基地

技术标签: java  开发工具  数据库  

我们具体实现思路是这样的 首先下载并安装openoffice和swftools

openoffice下载地址:http://www.openoffice.org/download/index.html
swftools下载地址:http://www.swftools.org/download.html

本源码下载地址:

去除FlexPaper水印的下载地址:http://pan.baidu.com/s/1pJDNunL

FlexPaper原版源码下载地址:http://pan.baidu.com/s/1c00C42O

本源码采用 j2ee eclipse luna+Apache tomcat7下开发 如果环境不一致,请移植一下。

image这是代码的整体架构

代码首先需要修改 com.eda.test.config.db 包下jdbc.properties 我用的是mysql数据库  数据库和数据库脚本都已经打包到了源码

下数据库和测试pdf中,大家自行导入即可,如果用的是其他数据库大家自己修改下建表脚本就好。

其次修改类:ConStant.java类 把OFFICE_HOME和SWFTOOLS_HOME配置为你本机或服务器安装路径。

package com.eda.test.common;

/**
 * 常量类
 *
 * @author [email protected]
 * @createTime 2014-11-22 下午01:49:58
 */
public class ConStant
{
    /** OpenOffice安装根目录 */
    public static final String OFFICE_HOME = "C:/Program Files/OpenOffice.org 3";

    /** SWFTools安装根目录 */
    public static final String SWFTOOLS_HOME = "D:/swftools/";

    /** PDF-SWF */
    public static final String SWFTOOLS_PDF2SWF_PATH = SWFTOOLS_HOME
            + "pdf2swf.exe";

    /** GIF-SWF */
    public static final String SWFTOOLS_GIF2SWF_PATH = SWFTOOLS_HOME
            + "gif2swf.exe";

    /** PNG-SWF */
    public static final String SWFTOOLS_PNG2SWF_PATH = SWFTOOLS_HOME
            + "png2swf.exe";

    /** JPEG-SWF */
    public static final String SWFTOOLS_JPEG2SWF_PATH = SWFTOOLS_HOME
            + "jpeg2swf.exe";

    /** WAV-SWF */
    public static final String SWFTOOLS_WAV2SWF_PATH = SWFTOOLS_HOME
            + "wav2swf.exe";

    /** PDF合并 */
    public static final String SWFTOOLS_PDFCOMBINE_PATH = SWFTOOLS_HOME
            + "swfcombine.exe";

    /** 文件上传保存根目录 */
    public static final String UPLOAD_BASE_DIR = "upload";

    /** SWF文件后缀 */
    public static final String SWF_STUFFIX = "swf";
}

 

下面贴点核心类的代码 FileType枚举类,主要是反映出是什么类型的文件,文件描述。:

package com.eda.test.common;

/**
 * 文件类型枚举
 * @author       [email protected]
 * @createTime   2014-11-22 下午05:06:11
 */
public enum FileType
{
    WORD2003 {
        public String getValue()
        {
            return "Word-2003文档";
        }
        public String getStuffix()
        {
            return "doc";
        }
    },
    EXCEL2003 {
        public String getValue()
        {
            return "Excel-2003文档";
        }
        public String getStuffix()
        {
            return "xls";
        }
    },
    PPT2003 {
        public String getValue()
        {
            return "PPT-2003文档";
        }
        public String getStuffix()
        {
            return "ppt";
        }
    },
    WORD2007 {
        public String getValue()
        {
            return "Word-2007文档";
        }
        public String getStuffix()
        {
            return "docx";
        }
    },
    EXCEL2007 {
        public String getValue()
        {
            return "Excel-2007文档";
        }
        public String getStuffix()
        {
            return "xlsx";
        }
    },
    PPT2007 {
        public String getValue()
        {
            return "PPT-2007文档";
        }
        public String getStuffix()
        {
            return "pptx";
        }
    },
    TXT {
        public String getValue()
        {
            return "记事本文档";
        }
        public String getStuffix()
        {
            return "txt";
        }
    },
    PDF {
        public String getValue()
        {
            return "PDF文档";
        }
        public String getStuffix()
        {
            return "pdf";
        }
    };
    public abstract String getValue();

    public abstract String getStuffix();
}

 

bean核心类 File文件类:

package com.eda.test.entity;

import java.io.Serializable;
import java.util.Date;

/**
 * 文件类
 * @author       [email protected]
 * @createTime   2014-11-22 下午04:56:54
 */
public class File implements Serializable
{
    private static final long serialVersionUID = -110840196972249172L;

    /**主键ID*/
    private Long id;
    /**文件名*/
    private String fileName;
    /**对应SWF文件web虚拟路径*/
    private String webBasePath;
    /**文件服务器路径*/
    private String distPath;
    /**文件大小(KB)*/
    private Long fileSize;
    /**文件类型*/
    private String fileType;
    /**文件描述*/
    private String description;
    /**上传时间*/
    private Date uploadDate;

    public Long getId()
    {
        return id;
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    public String getFileName()
    {
        return fileName;
    }

    public void setFileName(String fileName)
    {
        this.fileName = fileName;
    }

    public String getWebBasePath()
    {
        return webBasePath;
    }

    public void setWebBasePath(String webBasePath)
    {
        this.webBasePath = webBasePath;
    }

    public String getDistPath()
    {
        return distPath;
    }

    public void setDistPath(String distPath)
    {
        this.distPath = distPath;
    }

    public Long getFileSize()
    {
        return fileSize;
    }

    public void setFileSize(Long fileSize)
    {
        this.fileSize = fileSize;
    }

    public String getFileType()
    {
        return fileType;
    }

    public void setFileType(String fileType)
    {
        this.fileType = fileType;
    }

    public String getDescription()
    {
        return description;
    }

    public void setDescription(String description)
    {
        this.description = description;
    }

    public Date getUploadDate()
    {
        return uploadDate;
    }

    public void setUploadDate(Date uploadDate)
    {
        this.uploadDate = uploadDate;
    }

    public static long getSerialversionuid()
    {
        return serialVersionUID;
    }

    public File() {}

    @Override
    public int hashCode()
    {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj)
    {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (!(obj instanceof File))
            return false;
        File other = (File) obj;
        if (id == null)
        {
            if (other.id != null)
                return false;
        }
        else if (!id.equals(other.id))
            return false;
        return true;
    }
}

FileAction操作类,上传,转换,浏览文件 入口action类 - 最重要的一个类:

package com.eda.test.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.eda.test.action.model.FileModel;
import com.eda.test.common.ConStant;
import com.eda.test.common.FileType;
import com.eda.test.common.GerneralSessionAction;
import com.eda.test.entity.File;
import com.eda.test.util.FileUtil;
import com.eda.test.util.FileUtils;

/**
 * 文件处理Action
 *
 * @author [email protected]
 * @createTime 2014-11-22 下午05:04:29
 */
public class FileAction extends GerneralSessionAction<FileModel>
{

    /**
     * 跳至首页 方法摘要:这里一句话描述方法的用途
     *
     * @param
     * @return String
     */
    public String toUpload()
    {
        return "index";
    }

    /**
     * 上传文件 方法摘要:这里一句话描述方法的用途
     *
     * @param
     * @return String
     */
    public String uploadFile()
    {
        if(isEmptyFile())
        {
            this.getModel().setMessage("不允许上传空文件!");
            return "toUpload";
        }
        if(checkMaxFileSize())
        {
            this.getModel().setMessage("上传文件大小限制在20M以内!");
            return "toUpload";
        }
        if (checkFileType())
        {
            File file = createFile();
            boolean isSuccess = getFileService().saveFile(file, this.getModel().getDoc());
            this.getModel().setFiles(file);
            return "toList";
        }
        else
        {
            this.getModel().setMessage("该文件类型不允许上传!");
            return "toUpload";
        }

    }

    /**
     * 文件预览 方法摘要:这里一句话描述方法的用途
     *
     * @param
     * @return String
     */
    public String view()
    {

        return "view";
    }

    /**
     * 查询所有文件 方法摘要:这里一句话描述方法的用途
     *
     * @param
     * @return String
     */
    public String list()
    {
        this.getModel().setFileList(getFileService().findAll());
        return "list";
    }

    /**
     * 创建File对象 方法摘要:这里一句话描述方法的用途
     *
     * @param
     * @return File
     */
    private File createFile()
    {
        /** 文件上传至服务器存放路径 */
        String UPLOAD_DIR = getProjectRealPath() + ConStant.UPLOAD_BASE_DIR + "\\";
        String BASE_PATH = getBasePath() + ConStant.UPLOAD_BASE_DIR + "/";
        File file = new File();
        String fileName = this.getModel().getDocFileName();
        file.setFileName(fileName);
        String temp = getUploadFileName(fileName, null);
        String swfBasePath = BASE_PATH + temp;
        file.setDistPath(UPLOAD_DIR + temp);
        temp = FileUtils.getFileNameNoStuffix(temp) + "." + ConStant.SWF_STUFFIX;
        swfBasePath = BASE_PATH + temp;
        file.setWebBasePath(swfBasePath);
        file.setDescription(this.getModel().getDescription());
        file.setFileSize(this.getModel().getDoc().length());
        file.setUploadDate(new Date());
        String stuffix = FileUtil.getFileSufix(fileName);
        file.setFileType(getFileTypeName(stuffix));
        return file;
    }

    /**
     * 生成上传后文件名称 方法摘要:这里一句话描述方法的用途
     *
     * @param
     * @return String
     */
    public String getUploadFileName(String orignalFileName, String extension)
    {
        String stuffix = extension;
        if (null == extension || "".equals(extension))
        {
            stuffix = FileUtil.getFileSufix(orignalFileName);
        }
        Date currentDate = new Date();
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return dateFormat.format(currentDate) + "." + stuffix;
    }

    /**
     * 获取文件类型名称 方法摘要:这里一句话描述方法的用途
     *
     * @param
     * @return String
     */
    private String getFileTypeName(String fileStuffix)
    {
        String fileTypeName = "";
        if (fileStuffix.equals(FileType.WORD2003.getStuffix()))
        {
            fileTypeName = FileType.WORD2003.getValue();
        }
        else if (fileStuffix.equals(FileType.WORD2007.getStuffix()))
        {
            fileTypeName = FileType.WORD2007.getValue();
        }
        else if (fileStuffix.equals(FileType.EXCEL2003.getStuffix()))
        {
            fileTypeName = FileType.EXCEL2003.getValue();
        }
        else if (fileStuffix.equals(FileType.EXCEL2007.getStuffix()))
        {
            fileTypeName = FileType.EXCEL2007.getValue();
        }
        else if (fileStuffix.equals(FileType.PPT2003.getStuffix()))
        {
            fileTypeName = FileType.PPT2003.getValue();
        }
        else if (fileStuffix.equals(FileType.PPT2007.getStuffix()))
        {
            fileTypeName = FileType.PPT2007.getValue();
        }
        else if (fileStuffix.equals(FileType.TXT.getStuffix()))
        {
            fileTypeName = FileType.TXT.getValue();
        }
        else if (fileStuffix.equals(FileType.PDF.getStuffix()))
        {
            fileTypeName = FileType.PDF.getValue();
        }
        else
        {
            fileTypeName = "未知类型";
        }
        return fileTypeName;
    }

    /**
     * 检查文件类型是否允许上传 方法摘要:这里一句话描述方法的用途
     *
     * @param
     * @return boolean
     */
    private boolean checkFileType()
    {
        String fileType = this.getModel().getDocContentType();
        if (null == fileType || fileType.equals(""))
        {
            return false;
        }
        String[] allowTypes = this.getModel().getAllowTypes().split(",");
        for (int i = 0; i < allowTypes.length; i++)
        {
            if (allowTypes[i].equals(fileType))
            {
                return true;
            }
        }
        return false;
    }

    /**
     * 检查文件大小
     *方法摘要:这里一句话描述方法的用途
     *@param
     *@return boolean
     */
    private boolean checkMaxFileSize()
    {
        if(this.getModel().getMaxUploadSize() < this.getModel().getDoc().length())
        {
            return true;
        }
        return false;
    }

    /**
     * 检查是否空文件
     *方法摘要:这里一句话描述方法的用途
     *@param
     *@return boolean
     */
    private boolean isEmptyFile()
    {
        return this.getModel().getDoc().length() == 0;
    }
}

其他的就大家自己去看源码吧,经过上面的步骤,把源码部署到tomcat就能看到源码运行的效果了,破解版本的FlexPaper似乎设置那些参数有些问题,

就是设置了放大比例什么的没效果,这个如果有影响且不在意logo的,那就用原版本吧或者买一个个人版本或者商业版本。

感谢大家的关注,本文可以转载,转载请注明出处:http://www.cnblogs.com/luwenbin/p/4114576.html

转载于:https://www.cnblogs.com/luwenbin/p/4114576.html

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/ajv66295/article/details/101129093

智能推荐

又见table full!!_padavan table full-程序员宅基地

文章浏览阅读920次。 RAS3,做nat,又是table full ,导致客户机上网奇慢无比,其实已经把ip_conntrack_max设得很大了,但是还是出了这样的问题,google找到一个解决办法。暂用测试 到http://www.hping.org/download.html 下载hping2,我下的是rc3了 tar xvfz hping2.0.0-rc3.tar.g_padavan table full

08_浮点类型_浮点数误差问题_浮点类型 误差-程序员宅基地

文章浏览阅读478次。1.浮点类型 类型 占用存储空间 表述范围 float 4字节 -3.403E38~3.403E38 double 8字节 -1.798E308~1.798E308 float 单精度浮点型,尾数可以精确到7位有效数字 double 双精度浮点型,数值精度为float的两倍;_浮点类型 误差

深度神经网络 FPGA 设计与现状_fpga对神经网络公式硬件电路是如何搭建的,寄存器等如何使用-程序员宅基地

本文主要介绍了轻量化神经网络在硬件部署方面的需求,并讨论了在提高FPGA带宽和利用率方面的挑战和发展趋势。此外,还提出了人工智能产品对快速计算的要求。

mysql语句_mysql服务启动语句-程序员宅基地

文章浏览阅读218次。mysql语句启动mysql services.msc登录mysql -uroot -proot退出exitmysql 数据库操作数据库表格操作修改表删除表表-数据-增删改查条件查询 (复杂)连接查询(连表查询)子查询 (一个查询的结果作为另一个查询的一部分)启动mysql services.msc登录mysql -uroot -proot退出exitmysql 数据库操作登录mysql -uroot -prootquit/exit查看当前使用数据库: select database();_mysql服务启动语句

网址跳转重定向浏览器html,域名301重定向页面转跳的操作方法-巅云建站-程序员宅基地

文章浏览阅读1.3k次。当网站地址变更时,需要将旧域名301重定向到新的URL地址,实际上就是把旧地址的访问请求重新引导到新域名上。301永久重定向无论是对用户还是搜索引擎都是比较友好的,对SEO完全没有不好的一面。通过旧网站的关键词排名和PR等级都会传递给新网站,网站更换了域名,用域名301永久重定向的方式告诉搜索引擎本网页已经永久性转移到新的域名,避免搜索引擎无法找到页面,网站对于搜索引擎相对比较友好。域名重定向的好..._一个域名301重定向到另一个域名的url上

【软考-软件设计师精华知识点笔记】第八章 算法分析设计_软考决策树-程序员宅基地

文章浏览阅读1.4k次,点赞2次,收藏11次。【软考-软件设计师精华知识点笔记】第八章 算法分析设计_软考决策树

随便推点

Less中&符号_less中&.red-程序员宅基地

文章浏览阅读724次。.export_box{ background: #fff; .export_box_header{ color:red; &:hover { color: #235FB8; } } .export_box_main &{ border-color: #235FB8; }}转化为CSS效果:.export_box{ background: #fff; } .ex_less中&.red

active dataguard搭建-程序员宅基地

文章浏览阅读91次。从oracle11g开始,支持windows与linux异构dg,同时也开时支持备节点只读打开。所以在企业中,可以实现读写分离,客户知道这个新特性后,要求我们帮他们部署一套这样的active dataguard,来分担他们生产库的压力。下面,我就把我的实施过程发布出来与大家共享!1、安装操作系统及数据库软件具体的安装、建库等操作2、开始配置..._activedataguard

python网络编程-程序员宅基地

文章浏览阅读1w次,点赞34次,收藏216次。python网络编程_python网络编程

python数据结构之数据类型_python 结构体数据类型定义-程序员宅基地

文章浏览阅读2.4k次,点赞21次,收藏80次。????数据结构以前是用java学习的,那都是大一大二的事情了,早忘的差不多了,前段日子刷力扣的数据结构有点忘了,于是打算近期捡起来,让我们用python学习一遍。1.数据是什么?在 Python 以及其他所有面向对象编程语言中,类都是对数据的构成(状态)以及数据 能做什么(行为)的描述。由于类的使用者只能看到数据项的状态和行为,因此类与抽象数据类 型是相似的。在面向对象编程范式中,数据项被称作对象。一个对象就是类的一个实例。2.数据类型2.1内建原子数据类型Python 有两大內建数据类实现了整_python 结构体数据类型定义

python周期函数的拟合_python自定义函数拟合-程序员宅基地

文章浏览阅读607次。import numpy as npimport matplotlib.pyplot as pltfrom scipy.optimize import curve_fit#用python拟合函数最主要模块就是cure_fit#准备数据x=[一组数据]y=[一组数据]#定义你自己想要拟合的函数def func(x,E0,B0,B1,V0):return E0+(9.0/16)*V0*B0*(((V0..._用python怎么拟合出周期函数

android 自定义view文字不齐,Android 解决TextView排版参差不齐的问题-程序员宅基地

文章浏览阅读403次。Android 解决TextView排版参差不齐的问题在app中,展示数据时,里面有汉字、数字、特殊字符时,由于全角、半角问题导致TextView参差不齐。在网上找了许多,半角转全角并没什么用,还有其他自定义TextView都有问题。最后终于找到一个,就像Word一样,可以使文字左右两端对齐:package com.monkey.monkeymushroom.view;import android..._android textview文本偏上

推荐文章

热门文章

相关标签