JAVA實現word doc docx pdf excel的在線瀏覽 - 仿百度文庫 源碼

咱們具體實現思路是這樣的 首先下載並安裝openoffice和swftoolshtml

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

本源碼下載地址:mysql

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

FlexPaper原版源碼下載地址:http://pan.baidu.com/s/1c00C42Osql

本源碼採用 j2ee eclipse luna+Apache tomcat7下開發 若是環境不一致,請移植一下。數據庫

image這是代碼的總體架構tomcat

代碼首先須要修改 com.eda.test.config.db 包下jdbc.properties 我用的是mysql數據庫  數據庫和數據庫腳本都已經打包到了源碼服務器

下數據庫和測試pdf中,你們自行導入便可,若是用的是其餘數據庫你們本身修改下建表腳本就好。架構

其次修改類:ConStant.java類 把OFFICE_HOME和SWFTOOLS_HOME配置爲你本機或服務器安裝路徑。eclipse

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

相關文章
相關標籤/搜索