Java模拟实现百度文档在线浏览
这个思路是我参考网上而来,代码是我实现。
采用Apache下面的OpenOffice将资源文件转化为pdf文件,然后将pdf文件转化为swf文件,用FlexPaper浏览。
ok,
A、下载OpenOffice (转换资源文件)
B、下载JodConverter(调用OpenOffice)
C、下载Swftools(Pdf2Swf)
D、下载 FlexPaper(浏览swf文件)
这里我已经全部下载好了,大家只需要下载: http://down.51cto.com/data/1980603
下载之后,先别急安装,请看完这篇博文
1、先看我们的MyEclipse工程结构
2、
将我们下载下来的 解压之后将所有的 jar 文件拷贝到 baiduDoc 的 lib 下面去
3、 在 WebRoot 下面新建 文件夹,将解压后的 全部拷贝到 FlexPaper中去
4、 新建BaiDuServlet.java文件
package com.baidu.util; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.ConnectException; import javax.imageio.stream.FileImageInputStream; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.artofsolving.jodconverter.DocumentConverter; import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection; import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection; import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter; /**
* @Author:NuoYan
* @Date:2015-2-2 下午2:24:58
* TODO: 1、第一步,首先获取到需要查看的文件
* 2、第二部,将获取的文件(doc,xls,txt,ppt,03/07版本转化为PDF),这一步需要调用OpenOffice
* 3、第三部,将资源文件转换好的PDF文件转换为swf文件,使用FlexPaperViewer.swf进行浏览查看
*/ public class BaiDuServlet extends HttpServlet { private File sourceFile;// 要转化的源文件 private File pdfFile;// pdf中间文件对象 private File swfFile;// swf目标文件对象 private String filePath;// 用来保存文件路径 private String fileName;// 不包括后缀名的文件名 public File getSourceFile() { return sourceFile; } public void setSourceFile(File sourceFile) { this.sourceFile = sourceFile; } public File getPdfFile() { return pdfFile; } public void setPdfFile(File pdfFile) { this.pdfFile = pdfFile; } public File getSwfFile() { return swfFile; } public void setSwfFile(File swfFile) { this.swfFile = swfFile; } public String getFilePath() { return filePath; } public void setFilePath(String filePath) { this.filePath = filePath; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String saveFileName = request.getParameter("savFile"); System.out.println(saveFileName); String webPath = request.getRealPath("/"); filePath = webPath + "reader\\" + saveFileName; fileName = filePath.substring(0, filePath.lastIndexOf(".")); // 创建三个文件对象 sourceFile = new File(filePath); pdfFile = new File(fileName + ".pdf"); swfFile = new File(fileName + ".swf"); System.out.println(pdfFile); System.out.println(swfFile); // 1、将源文件转化为pdf格式文件 src2pdf(); try { // 2、将pdf文件转化为swf文件 pdf2swf(); } catch (Exception e) { e.printStackTrace(); } // 将转化好的文件绑定到session上去 request.getSession().setAttribute("swfName", swfFile.getName()); System.out.println(swfFile.getName()); // 重定向到预览页面 response.sendRedirect(request.getContextPath() + "/reader/baseFile.jsp"); } /** * @Author:NuoYan * @Date:2015-2-2 下午2:28:22 TODO://源文件转化为PDF文件 */ private void src2pdf() { if (sourceFile.exists()) { // 如果不存在,需要转份为PDF文件 if (!pdfFile.exists()) { // 启用OpenOffice提供的转化服务 OpenOfficeConnection conn = new SocketOpenOfficeConnection(8100); // 连接OpenOffice服务器 try { conn.connect(); // 建立文件转换器对象 DocumentConverter converter = new OpenOfficeDocumentConverter(