我们具体实现思路是这样的 首先下载并安装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下开发 如果环境不一致,请移植一下。

这是代码的整体架构

代码首先需要修改 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

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

  1. Java生鲜电商平台-小程序或者APP优惠券的设计与源码实战

    Java生鲜电商平台-小程序或者APP优惠券的设计与源码实战 说明:Java生鲜电商平台-小程序或者APP优惠券的设计与源码实战,优惠券是一种常见的促销方式,在规定的周期内购买对应商品类型和额度的商品 ...

  2. java将word文件转为pdf

    import java.io.File; import com.jacob.activeX.ActiveXComponent;import com.jacob.com.Dispatch; public ...

  3. Java转换Word文件到PDF文件

    使用Docx4j将Word文件转换为PDF文件: public static void convertDocxToPDF(String docxFilePath, String pdfPath) th ...

  4. pywin32 pywin32 docx文档转html页面 word doc docx 提取文字 图片 html 结构

    https://blog.csdn.net/X21214054/article/details/78873338# python docx文档转html页面 - 程序猿tx - 博客园 https:/ ...

  5. 【ASP.NET 进阶】仿百度文库文档在线预览(支持格式.pdf,.doc,docx,xls,xlsx,.ppt,pptx)

    在[ASP.NET]PDF文件在线预览(类似百度文库)基础上进行了office文件到pdf文件的转换,然后在显示出来,效果如下: 问题说明: 1.请通过以下方式添加 Office COM 组件. 2. ...

  6. Java+FlexPaper+swfTools仿百度文库文档在线预览系统设计与实现

    笔者最近在给客户开发文档管理系统时,客户要求上传到管理系统的文档(包括ppt,word,excel,txt)只能预览不允许下载.笔者想到了百度文库和豆丁网,百度文库和豆丁网的在线预览都是利用flash ...

  7. Java网络编程与NIO详解11:Tomcat中的Connector源码分析(NIO)

    本文转载 https://www.javadoop.com 本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://github.c ...

  8. Java日期时间API系列8-----Jdk8中java.time包中的新的日期时间API类的LocalDate源码分析

    目录 0.前言 1.TemporalAccessor源码 2.Temporal源码 3.TemporalAdjuster源码 4.ChronoLocalDate源码 5.LocalDate源码 6.总 ...

  9. word doc转pdf

    from win32com.client import constants, gencache # TODO pip install pywin32 -i http://mirrors.aliyun. ...

随机推荐

  1. 转:如何取得Spring管理的bean

    原文链接:http://blog.csdn.net/a9529lty/article/details/42145545 1.servlet方式加载时,[web.xml] <servlet> ...

  2. 调起MT096的配置过程

    FTP::cips\/var/cics_regions/RGCIPS/database/PD/PD.RGCIPS|PD.auto 更加新的PD号(其中的路径指向新的程序ibmp),并修改FTP::ci ...

  3. Newtonsoft.Json随手记

    private static Newtonsoft.Json.JsonSerializerSettings CreateSettings(string dateFormat) { Newtonsoft ...

  4. Linux作业控制

    在Linux中,利用Shell的作业控制是比较常用的操作,在这一节中我们将探究作业控制相关的操作.为了方便我们查看区分不同的进行,我们编写如下程序,其功能是每间隔2秒输出一次自己的编号. /* ** ...

  5. javascript 弹出的窗口返回值给 父窗口

    直接上代码,有些地方可以用到: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <H ...

  6. 关于WPF中Popup控件的小记

    在wpf开发中,常需要在鼠标位置处弹出一个“提示框”(在此就以“提示框”代替吧),通过“提示框”进行信息提示或者数据操作,如果仅仅是提示作用,使用ToolTip控件已经足够,但是有些是需要在弹出的框中 ...

  7. 用PHP实现单向链表

    供参考,代码还可继续打磨 同时放在了我的github上:https://github.com/hheedat/demo/blob/master/learn_php/58_linked_list.php ...

  8. IOC----LightInject

    开源项目 引入 LightInject.cs 默认服务 new ServiceContainer 注册跟获取获取服务 container.Register<IFoo, Foo>();con ...

  9. [HTML] <input> 标签

      可选的属性 属性 值 描述 accept mime_type 规定通过文件上传来提交的文件的类型. align left right top middle bottom 不赞成使用.规定图像输入的 ...

  10. Linux文件保护禁止修改、删除、移动文件等,使用chattr +i保护

    不让用户修改.删除文件等,使用 chattr保护 chattr命令的用法:chattr [ -RV ] [ -v version ] [ mode ] files… 最关键的是在[mode]部分,[m ...