Java模拟实现百度文档在线浏览
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(
conn);
converter.convert(sourceFile, pdfFile);
// 断开链接
conn.disconnect();
System.out.println("转换成功");
} catch (ConnectException e) {
e.printStackTrace();
}
} else {
System.out.println("已经存在PDF文件,不需要在转换!!");
}
} else {
System.out.println("文件路径不存在!!!");
} } /**
* @Author:NuoYan
* @Date:2015-2-2 下午2:28:32
* @throws Exception
* TODO:PDF转化为SWF文件
*/
private void pdf2swf() throws Exception {
if (!swfFile.exists()) {
if (pdfFile.exists()) {
String command = "C:\\Pdf2swf\\pdf2swf.exe "
+ pdfFile.getPath() + " -o " + swfFile.getPath()
+ " -T 9";
System.out.println("转换命令:" + command);
// Java调用外部命令,执行pdf转化为swf
Runtime r = Runtime.getRuntime();
Process p = r.exec(command);
System.out.println(loadStream(p.getInputStream()));
System.out.println("swf文件转份成功!!!");
System.out.println(swfFile.getPath());
} else {
System.out.println("不存在PDF文件");
}
} } private static String loadStream(InputStream in) throws Exception {
int len = 0;
in = new BufferedInputStream(in);
StringBuffer buffer = new StringBuffer();
while ((len = in.read()) != -1) {
buffer.append((char) len);
}
return buffer.toString();
} }
5、 修改index.jsp
<%@ page language="java" import="java.util.*"pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>百度文库在线预览</title>
<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">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<a href="<%=request.getContextPath()%>/BaiDuServlet?savFile=1234.xls">在线预览</a>
</body>
</html>
6、 编写baseFile.jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>在线阅读</title>
<script type="text/javascript" src="../FlexPaper/js/flexpaper_flash.js"></script>
<style type="text/css">
html,body{height: 100%;}
body {
margin: 0;padding: 0;overflow: auto;
}
#flashContent { display:none; }
</style>
</head>
<body>
<div style="position:absolute;left:10px;top:10px;">
<a id="viewerPlaceHolder" style="width:1000px;height:480px;display:block"></a>
<script type="text/javascript">
var fp = new FlexPaperViewer(
'../FlexPaper/FlexPaperViewer',
'viewerPlaceHolder', { config : {
SwfFile : escape('../reader/<%=(String)session.getAttribute("swfName")%>'),
Scale : 0.6,
ZoomTransition : 'easeOut',
ZoomTime : 0.5,
ZoomInterval : 0.2,
FitPageOnLoad : true,
FitWidthOnLoad : false,
FullScreenAsMaxWindow : false,
ProgressiveLoading : false,
MinZoomSize : 0.2,
MaxZoomSize : 5,
SearchMatchAll : false,
InitViewMode : 'Portrait',
PrintPaperAsBitmap : false,
ViewModeToolsVisible : true,
ZoomToolsVisible : true,
NavToolsVisible : true,
CursorToolsVisible : true,
SearchToolsVisible : true,
localeChain: 'zh_CN'
}});
</script>
</div>
</body>
</html>
注意baseFile.jsp中的代码,不会你可以参考这里

/**************************************************************************************/
7、 到这里就完成,需要注意的是:
1、
文件安装路径不要太深,不然 Java 调用外部命令不能执行,我这里是 C盘下

2、 
2.1、红色1标记路径不能错,我就犯这个错误了
2.2、红色标记2还可以写 http://127.0.0.1:8080/baiduDoc/reader/...
3、 启动OpenOffice的命令,不是直接双击启动的。官网启动方式,使用cd命令打开安装目录!
安装完openoffice后
1.安装服务
cd C:\Program Files (x86)\OpenOffice4\program
这一步你可以看你的OpenOffice安装哪里
执行
soffice -headless-accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
2.查看是否安装成功
2.1查看端口对应的pid
netstat -ano|findstr "8100"
2.2查看pid对应的服务程序名
tasklist|findstr "ipd值"
效果图示:

源码地址:
http://down.51cto.com/data/1980608
有问题留言,来找我吧!
- 本文来自:Linux学习教程网
Java模拟实现百度文档在线浏览的更多相关文章
- sharepoint 2013 office web app 2013 文档在线浏览 IE11 浏览器不兼容解决方法
昨晚配置完成office web apps 2013的外部网络访问之后,今天发现了一个很奇怪的问题,就是IE 11不支持文档在线浏览,找了很多方法,打补丁什么的,都不管用,最后在预览文件的页面,看到& ...
- FlexPaper实现文档在线浏览(附源码)
园子里也有关于FlexPaper的文章,但都不怎么详细. 没有较全的参数说明.就连官方网站都没有.没法,最后只得将swf文件反编译后查看了源码才将里面的参数全部弄出来. 好了,废话不多说,开始正题. ...
- Linux下实现文档在线浏览
使用php实现百度文库功能,网上搜索到的方案,实现doc转pdf,pdf转swf,然后显示出来. 这里简单的记录下,[doc转pdf,pdf转swf]两个功能的搭建流程. doc转pdf 使用到下列程 ...
- Java+FlexPaper+swfTools仿百度文库文档在线预览系统设计与实现
笔者最近在给客户开发文档管理系统时,客户要求上传到管理系统的文档(包括ppt,word,excel,txt)只能预览不允许下载.笔者想到了百度文库和豆丁网,百度文库和豆丁网的在线预览都是利用flash ...
- Java版office文档在线预览
java将office文档pdf文档转换成swf文件在线预览 第一步,安装openoffice.org openoffice.org是一套sun的开源office办公套件,能在widows,linux ...
- Java+FlexPaper+swfTools 文档在线预览demo
1.概述 主要原理 1.通过第三方工具openoffice,将word.excel.ppt.txt等文件转换为pdf文件 2.通过swfTools将pdf文件转换成swf格式的文件 3.通过FlexP ...
- Java实现word文档在线预览,读取office文件
想要实现word或者其他office文件的在线预览,大部分都是用的两种方式,一种是使用openoffice转换之后再通过其他插件预览,还有一种方式就是通过POI读取内容然后预览. 一.使用openof ...
- java 实现word文档在线预览
一.准备工具 1.通过第三方工具openoffice,将word.excel.ppt.txt等文件转换为pdf文件 下载地址:http://www.openoffice.org/download/in ...
- Print2flash在.NET(C#)64位中的使用,即文档在线预览
转:http://www.cnblogs.com/flowwind/p/3411106.html Print2flash在.NET(C#)中的使用,即文档在线预览 office文档(word,ex ...
随机推荐
- [Suricata]无法禁用某些规则的解决办法
背景: 生产环境中部署了suricata,日常规则更新使用suricata-update,如果想禁用某些规则,可以在配置文件/etc/suricata/disable.conf中添加,比如: #禁用规 ...
- Fetch(原生)的简单使用
前言: Fetch 提供了对 Request 和 Response 等对象通用的定义. 发送请求或者获取资源,需要使用 fetch() 方法. 具体使用代码: <script> fetch ...
- 浮动float和清除clear
一.浮动(float) float简介 取值:left,right,none,inherit,默认none(不浮动) 可应用与所有元素 没有继承性 不在正常流中,但会影响布局.因为一个元素浮动时,其他 ...
- 题解 【POJ1952】 BUY LOW, BUY LOWER
题目意思: 给你一个长度为\(n\)(\(1<=n<=5000\))的序列,并求出最长下降子序列的长度及个数, 并且,如果两个序列中元素的权值完全相同,那么即使它们的位置不一样,也只算一种 ...
- h5构建页面(rem布局准备)
新建一个flex.js ! function () { function t(t) { return this.config = t, this } t.prototype = { reset: fu ...
- IT行业常见职位英文缩写
1.PG Programer 程序员 2.AA ...
- RAID技术超详细讲解
RAID 技术是一种多磁盘技术,面对数据的各方面有着两面性的影响,整体来说优点大于缺点的,下面我将详细介绍一下 RAID ,简称磁盘阵列技术. 一.RAID 概述 1988 年美国加州大学伯克利分校的 ...
- [SCOI2009]windy数 代码 (对应数位dp入门)
Code1 (DP版) #include<bits/stdc++.h> #define in(i) (i=read()) using namespace std; int read() { ...
- Routing(exchange--direct)
引言 它是一种完全按照routing key(路由关键字)进行投递的:当消息中的routing key和队列中的binding key完全匹配时,才进行会将消息投递到该队列中 1.模型 2.创建生产者 ...
- Jmeter -- 对并发认识的误区
1. 误区 如下图所示,并发数设置为1000,启动时间设置1s,就是每秒发起1000个请求(错误) 上述的设置,表示在1s内启动1000个线程,之后,jmeter便以最大限度的1000个并发进行压测, ...