- 浏览: 958 次
- 性别:
- 来自: 北京
-
最新评论
原创 http://hwl-sz.iteye.com/blog/1964397
1、开发前准备
1)下载第三方软件或插件进行安装
openOffice:官方下载:http://www.openoffice.org/download/
百度网盘:http://pan.baidu.com/s/1mpxdL
swftools:官方下载:http://www.swftools.org/swftools-0.9.0.exe
百度网盘:http://pan.baidu.com/s/11O0nS
FlexPaper_1.4.5_flash:http://pan.baidu.com/s/1oXmIL
Linux版本的安装及下载见:《openOffice、swftools安装指南(Linux).doc》
2)下载相关的jar包,如下图:
3)集成插件
解压FlexPaper_1.4.5_flash.zip,将以下文件移植到web目录下,如图
2、编写代码
项目源代码下载:[url]http://pan.baidu.com/s/18AcnQ [/url]
效果图
1、开发前准备
1)下载第三方软件或插件进行安装
openOffice:官方下载:http://www.openoffice.org/download/
百度网盘:http://pan.baidu.com/s/1mpxdL
swftools:官方下载:http://www.swftools.org/swftools-0.9.0.exe
百度网盘:http://pan.baidu.com/s/11O0nS
FlexPaper_1.4.5_flash:http://pan.baidu.com/s/1oXmIL
Linux版本的安装及下载见:《openOffice、swftools安装指南(Linux).doc》
2)下载相关的jar包,如下图:

3)集成插件
解压FlexPaper_1.4.5_flash.zip,将以下文件移植到web目录下,如图

2、编写代码
项目源代码下载:[url]http://pan.baidu.com/s/18AcnQ [/url]
readfile.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> </head> <body> <form action="uploadServlet" enctype="multipart/form-data" method="post"> <font style="color:red">只支持office文件在线预览,如doc,docx,ppt,pptx,xls,xlxs</font></br> <input type="file" name="file"/></br> <input type="submit" value="在线预览"> </form> </body> </html> UploadServlet.java package servlet; import java.io.File; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import util.Office2Swf; publicclass UploadServlet extends HttpServlet { privatestaticfinallongserialVersionUID = 1L; @Override protectedvoid doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String inputFilePath = this.uploadFile(req); if (null != inputFilePath && !"".equals(inputFilePath.trim())) { String outFilePath = inputFilePath.replace(new File(inputFilePath).getName(), System.currentTimeMillis() + ".swf"); outFilePath = Office2Swf.office2Swf(inputFilePath, outFilePath); req.getSession().setAttribute("fileName", new File(outFilePath).getName()); } req.getRequestDispatcher("/readonline.jsp").forward(req, resp); } @Override protectedvoid doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } @SuppressWarnings({"unchecked", "deprecation"}) private String uploadFile(HttpServletRequest request)throws ServletException, IOException { request.setCharacterEncoding("utf-8");//设置编码 //获得磁盘文件条目工厂 DiskFileItemFactory factory = new DiskFileItemFactory(); //获取文件需要上传到的路径 String path = request.getRealPath("/upload"); factory.setRepository(new File(path)); //设置缓存的大小,当上传文件的容量超过该缓存时,直接放到暂时存储室 factory.setSizeThreshold(1024*1024) ; //文件上传处理 ServletFileUpload upload = new ServletFileUpload(factory); String uploadFilePath = null; //可以上传多个文件 try { List<FileItem> list = (List<FileItem>)upload.parseRequest(request); for(FileItem item : list) { //获取表单的属性名字 String name = item.getFieldName(); // 表单文本信息 if(item.isFormField()) { String value = item.getString() ; request.setAttribute(name, value); } // 表单上传的文件 else { // 获取路径 String value = item.getName() ; int start = value.lastIndexOf("\\"); // 截取上传文件名称 String filename = value.substring(start+1); request.setAttribute(name, filename); item.write(new File(path,filename)); uploadFilePath = path + File.separator + filename; } } } catch (FileUploadException e) { e.printStackTrace(); } catch(Exception ex) { ex.printStackTrace(); } return uploadFilePath; } } Office2PDF.java package util; import java.io.File; import java.util.ArrayList; import java.util.Collections; import java.util.regex.Pattern; import org.artofsolving.jodconverter.OfficeDocumentConverter; import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration; import org.artofsolving.jodconverter.office.OfficeManager; /** * * @author hwl_sz * * @desc需要OpenOffice第三插件的支持 ,支持window\linux\mac等系统 */ publicclass Office2PDF { publicstaticfinal String[] OFFICE_POSTFIXS = {"doc", "docx", "xls", "xlsx", "ppt", "pptx"}; /** * 根据操作系统的名称,获取OpenOffice的安装目录 * 如我的安装目录:C:/Program Files/OpenOffice 4 */ privatestatic String getOfficeHome() { String osName = System.getProperty("os.name"); if (Pattern.matches("Linux.*", osName)) { return"/opt/openoffice.org3"; } elseif (Pattern.matches("Windows.*", osName)) { return"C:/Program Files/OpenOffice 4"; } elseif (Pattern.matches("Mac.*", osName)) { return"/Application/OpenOffice.org.app/Contents"; } returnnull; } /** * 转换文件 * * @param inputFilePath 转换的office源文件路径 * @param outputFilePath 输出目标文件路径 */ privatestaticvoid converterFile(String inputFilePath, String outputFilePath) { File inputFile = new File(inputFilePath); File outputFile = new File(outputFilePath); // 假如目标路径不存在,则新建该路径 if (!outputFile.getParentFile().exists()) { outputFile.getParentFile().mkdirs(); } DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration(); // 获取OpenOffice 的安装目录 String officeHome = getOfficeHome(); config.setOfficeHome(officeHome); // 启动OpenOffice的服务 OfficeManager officeManager = config.buildOfficeManager(); officeManager.start(); OfficeDocumentConverter converter = new OfficeDocumentConverter( officeManager); converter.convert(inputFile, outputFile); System.out.println("文件:" + inputFilePath + "\n转换为\n目标文件:" + outputFile + "\n成功!"); officeManager.stop(); } /** * 将(.doc|.docx|.xls|.xlsx|.ppt|.pptx)等office文件转化为pdf文件 * * @param inputFilePath 待转换的源文件路径 * @param outputFilePath 输出的目录文件路径,如果未指定(null),则按在源文件当前目录生成同名的pdf文件 * @return处理结果 */ publicstaticboolean openOffice2Pdf(String inputFilePath, String outputFilePath) { boolean flag = false; File inputFile = new File(inputFilePath); ArrayList<String> office_Formats = new ArrayList<String>(); Collections.addAll(office_Formats, OFFICE_POSTFIXS); if ((null != inputFilePath) && (inputFile.exists())) { // 判断目标文件路径是否为空 if (office_Formats.contains(getPostfix(inputFilePath))) { if (null == outputFilePath) { // 转换后的文件路径 String outputFilePath_new = inputFilePath.toLowerCase().replaceAll("." + getPostfix(inputFilePath), ".pdf"); converterFile(inputFilePath, outputFilePath_new); flag = true; } else { converterFile(inputFilePath, outputFilePath); flag = true; } } } return flag; } /** * 获取文件的后缀名 */ privatestatic String getPostfix(String inputFilePath) { String[] p = inputFilePath.split("\\."); if (p.length > 0) { return p[p.length - 1]; } else { returnnull; } } /** * @param args */ publicstaticvoid main(String[] args) { Office2PDF.openOffice2Pdf("E:/黄色地球商务PPT模板.ppt",null); } } Office2Swf.java package util; import java.util.regex.Pattern; /** * * @author hwl_sz * * @desc需要swftools第三插件的支持 ,支持window\linux\mac等系统 */ publicclass Office2Swf { /** * 根据操作系统的名称,获取执行pdf->swf文件的命令 * @param pdfFile 转换的pdf源文件路径 * @param swfOutFilePath 输出的swf文件路径 * @return */ privatestatic String getCommand(String pdfFile, String swfOutFilePath) { String command = null; String osName = System.getProperty("os.name"); if (null == swfOutFilePath || "".equals(swfOutFilePath.trim())) { swfOutFilePath = pdfFile.toLowerCase().replaceAll(".pdf", ".swf"); } if (Pattern.matches("Linux.*", osName)) { command = "pdf2swf -f " + pdfFile + " " + swfOutFilePath; } elseif (Pattern.matches("Windows.*", osName)) { command = "C:/Program Files/SWFTools/pdf2swf.exe -t " + pdfFile + " -o " + swfOutFilePath + " -T 9"; } elseif (Pattern.matches("Mac.*", osName)) { } return command; } /** * 将pdf转换swf文件,在线预览 * @param pdfInputFilePath 待转换的pdf源文件路径 * @param swfOutFilePath 输出的swf目标文件路径,如果未指定(null),则按在源文件当前目录生成同名的swf文件 * @return swf目标文件路径 */ publicstatic String pdf2Swf(String pdfInputFilePath, String swfOutFilePath) { String command = getCommand(pdfInputFilePath, swfOutFilePath); try { Process pro = Runtime.getRuntime().exec(command); pro.waitFor(); return pdfInputFilePath.replaceAll("." + getPostfix(pdfInputFilePath), ".swf"); } catch(Exception ex) { ex.printStackTrace(); } returnnull; } /** * 将office文件直接转换为swf文件 * @param inputFilePath 待转换的源office文件路径 * @param outputSwfPath 输出的swf目标文件路径,如果未指定(null),则按在源文件当前目录生成同名的swf文件 * @return swf目标文件路径 */ publicstatic String office2Swf(String inputFilePath, String outputSwfPath) { String outputPdfPath = null; if (null == outputSwfPath || "".equals(outputSwfPath.trim())) { outputPdfPath = inputFilePath.replace("." + getPostfix(inputFilePath), ".pdf"); } else { outputPdfPath = outputSwfPath.replace("." + getPostfix(outputSwfPath), ".pdf"); } boolean isSucc = Office2PDF.openOffice2Pdf(inputFilePath, outputPdfPath); if (isSucc) { outputSwfPath = pdf2Swf(outputPdfPath, outputSwfPath); } return outputSwfPath; } /** * 获取文件的后缀名 */ privatestatic String getPostfix(String inputFilePath) { String postfix = null; if (null != inputFilePath && !"".equals(inputFilePath.trim())) { int idx = inputFilePath.lastIndexOf("."); if (idx > 0) { postfix = inputFilePath.substring(idx + 1, inputFilePath.trim().length()); } } return postfix; } } readonline.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <head> <title>在线预览</title> <script type="text/javascript" src="FlexPaper/js/jquery.js"></script> <script type="text/javascript" src="FlexPaper/js/flexpaper_flash.js"></script> <script type="text/javascript" src="FlexPaper/js/flexpaper_flash_debug.js"></script> </head> <%-- <%=(String)session.getAttribute("fileName")%> --%> <body> <div style="position:absolute;left:10px;top:10px;"> <%-- 指定flexPaper的宽度和高度 --%> <a id="viewerPlaceHolder" style="width:100%;height:800px;display:block"></a> <script type="text/javascript"> var fp = new FlexPaperViewer( 'FlexPaper/swfFiles/FlexPaperViewer', 'viewerPlaceHolder', <!--对应于a 标签的id--> { config : { SwfFile : escape('upload/<%=(String)session.getAttribute("fileName")%>'), <!--导入的.swf的路径,文件名称使用英语表示,中文时显示不出来,暂时未解决这个问题--> Scale : 0.6, ZoomTransition : 'easeOut', ZoomTime : 0.5, ZoomInterval : 0.2, FitPageOnLoad : true, FitWidthOnLoad : false, PrintEnabled : true,<%-- 是否可以打印 --%> FullScreenAsMaxWindow : false, ProgressiveLoading : false, MinZoomSize : 0.2, MaxZoomSize : 5, SearchMatchAll : false, InitViewMode : 'Portrait', ViewModeToolsVisible : true, ZoomToolsVisible : true, NavToolsVisible : true, CursorToolsVisible : true, SearchToolsVisible : true, localeChain: 'en_US' }}); </script> </div> </body> </html>
效果图

相关推荐
java 实现的文档在线预览: 需要下载的工具有:OpenOffice+flexpaper+swftools+jodcconverter * .启动OpenOffice的服务 * 1 win+R开启dos窗口 * 2 执行:cd C:\Program Files (x86)\OpenOffice 4\program * 3 ...
【标题】"wpsoffice在线预览,在线编辑 Java版"所涉及的知识点主要集中在如何在Java环境中实现对WPS Office文档的在线预览和编辑功能。WPS Office是一款流行的办公软件套装,包含了文字处理、电子表格和演示文稿等...
综上所述,Java实现Web在线预览Office文档和PDF文档主要涉及文件读取、内容转换、文档预览和服务器端的文件操作。通过合理选择和使用相应的库和工具,可以构建出稳定且高效的在线预览系统。在Linux环境下,结合开源...
在Java开发中,实现Word文档的在线预览是一项常见的需求,尤其在企业级应用中,例如文档管理系统或者协同办公平台。这项功能可以让用户无需下载原始文件就能查看文档内容,提高工作效率并减少服务器存储压力。本资源...
标题"免费Office文件在线预览"所指的就是通过网络浏览器查看Word、Excel、PowerPoint等Microsoft Office格式的文档,而无需下载或安装任何额外软件。这种功能极大地提升了用户体验,特别是在共享和协作办公时。 ...
1. **在线预览**:WPS Office 提供的在线预览功能让用户可以直接在网页浏览器中打开文档,支持多种文件格式,如 .doc、.docx、.xls、.xlsx、.ppt、.pptx 等。这极大地方便了用户,无论何时何地,只要有网络连接,就...
java与flexpaper结合的office文件的在线预览功能。非常的好用,类似于百度文库之前的版本。最主要的是该插件是免费开源框架,提供大家学习使用。支持window和linux。需要安装2个服务软件openoffice和swttool,这个...
JAVA在线预览office文档JAVA在线预览office文档JAVA在线预览office文档
在Java开发环境中,有时我们需要为用户提供在线预览附件的功能,比如查看Excel、Word或PowerPoint文档,而无需下载文件到本地。在这种情况下,Aspose是一个强大的工具,它提供了丰富的API来处理各种办公文档格式,...
JAVA文件文档在线预览项目解决方案,对标业内付费产品有【永中office】【office365】【idocv】等,该项目使用流行的spring boot搭建,易上手和部署,支持主流办公文档的在线预览,如doc,docx,Excel,pdf,txt,zip,rar,...
本文将详细讲解如何利用Java后端技术实现文件在线预览,特别是针对文档(如.doc、.docx、.xls、.xlsx、.ppt、.pptx)和图片格式的预览。 首先,我们需要理解文件预览的基本原理:用户通过浏览器发送请求到服务器,...
在项目代码中,`java将office文档pdf文档转换成swf文件在线预览.docx`可能是一个示例文档,它展示了如何使用Java处理Office文档并进行转换。而`docview`可能是一个包含视图逻辑或预览界面的类或模块,负责在前端展示...
在Java开发中,有时我们需要提供在线预览Office文档的功能,比如Word、Excel、PPT以及PDF等。这个任务可以通过结合OpenOffice和pdf2htmlEX这两个工具来实现。下面将详细介绍如何利用Java进行这一操作。 首先,...
2. **文件处理与IO流**:在线预览和编辑文档涉及到文件上传、下载和转换,因此必须熟悉Java的File类和IO流操作,如InputStream和OutputStream,用于读写文件内容。 3. **WPS API集成**:为了实现WPS文档的在线编辑...
- **在线查看**:WebOffice支持多种格式的文档预览,包括常见的Microsoft Office系列(如Word、Excel、PowerPoint)以及PDF文件。 - **编辑与保存**:用户可以在浏览器中直接编辑文档,并保存更改回原始文件或另存...
除了 OpenOffice,还有其他开源和商业解决方案,如 Microsoft Azure 的 Office Online,Google Docs,以及各种基于 Web 的文档预览库(如 ViewerJS、PDF.js)。选择哪种方案取决于项目需求、预算和技术栈。 总之,...
5. **FlexPaper集成**:在前端页面上,引入FlexPaper的JavaScript库,并设置相应的配置,将转换后的SWF文件路径传递给FlexPaper,以显示文档预览。 以上步骤只是一个基础流程,实际应用中可能需要考虑更多因素,如...
最全JAVA实现Office在线预览调研,OpenOffice,LibreOffice,POI等已有技术归纳整理,附带链接。
office在线编辑ONLYOFFICE集成java和前端office在线编辑ONLYOFFICE集成java和前端office在线编辑ONLYOFFICE集成java和前端office在线编辑ONLYOFFICE集成java和前端office在线编辑ONLYOFFICE集成java和前端
java实现浏览器在线预览offic(doc、xls、ppt)和pdf文件,本人将例子集成到项目中,已成功。 技术原理:将文件先转换成pdf文件,再将pdf文件转换成swf文件,最后显示在浏览器上。 资源描述:本资源包括swftools...