转:JAVA实现word doc docx pdf excel的在线浏览

来源:互联网 发布:知乎 核安全所 编辑:程序博客网 时间:2024/05/19 22:28

转自:http://www.cnblogs.com/luwenbin/p/4114576.html


JAVA实现word doc docx pdf excel的在线浏览 - 仿百度文库 源码

我们具体实现思路是这样的 首先下载并安装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 luwenbin006@163.com * @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       luwenbin006@163.com * @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       luwenbin006@163.com * @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 luwenbin006@163.com * @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

阅读全文
0 0
原创粉丝点击