仿百度文库、豆丁在线阅读

14 篇文章 0 订阅

仿百度文库、豆丁在线阅读

由于项目需求,需要开发一个类似百度文库和豆丁那样的在线阅读功能。刚开始不知道如何下手,在百度和谷歌上面搜了下,相关帖子很多,不是内容相互抄袭就是肤浅的空谈(最痛恨的就是这种不负责任行为),没有一点儿参考价值。因为我没少纠结,没少碰到问题,所以就将心得分享下,希望能给看到本文的朋友些许帮助。

实现方式

① 

Txt/Word/Excel/PPT=>PDF(OpenOffice+JodConverter)=>SWF(pdf2swf)=>FlexPaper浏览

② 

Txt/Word/Excel/PPT=>PDF(MSOffice+JACOB)=>SWF(pdf2swf)=>FlexPaper浏览

③ 

Txt/Word/Excel/PPT=>SWF (FlashPaper)=> FlexPaper浏览

④ 

Txt/Word/Excel/PPT=>SWF(print2flash)=> FlexPaper浏览

前两种方式比较麻烦,都是先转成PDF,再转成SWF,最后用FlexPaper浏览。后两种比较快捷,可直接将源文件转为SWF,用FlexPaper浏览。由于FlashPaper不是开源工具,加之Win7系统不兼容(我现在用的系统),所以就没采用第三种实现方式。Print2flash是开源工具,即使公司产品中用到也不会出现版权纠纷,遗憾的是没找到如何用程序控制该工具转换文件的命令。第二种方式转PDF的时候相当复杂,也淘汰掉了。最后直接用的第一种方式。

下载工具

OpenOffice

http://zh.openoffice.org/new/zh_cn/downloads.html

JodConverter

http://dldx.csdn.net/fd.php?i=992314146801277&s=08dbee95a6e2dda1a95aa8cbf4df197b

Swftools(pdf2swf)

http://dldx.csdn.net/fd.php?i=389133735472350&s=2f7430ad3c00cca78ada8b4671a50b24

FlexPaper

http://flexpaper.googlecode.com/files/FlexPaper_1.4.5_flash.zip


① 打开MyEclipse,新建WEB工程。我的Demo如下图:

示例工程结构


② 、将下载的JodConverter压缩包中lib目录下的jar包放到工程WEB-INF/lib中。

JodConverter压缩包lib下的jar包


③ 、在WebRoot下新建flexpaper文件夹,把下载的FlexPaper解压缩后的内容放到该文件夹中。

FlexPaper压缩包中的文件


④ 、新建ConvertServlet类,下附该类代码。

package servlet;

import java.io.File;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

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;

public class ConvertServlet extends HttpServlet{

private File sourceFile;//转换源文件

private File pdfFile;//PDF目标文件

private File swfFile;//SWF目标文件

private Runtime r;

public void init() throws ServletException {

sourceFile = new File("D:/tomcat7/webapps/readonline/swfFile/s.ppt");

pdfFile = new File("D:/tomcat7/webapps/readonline/swfFile/s.pdf");

swfFile = new File("D:/tomcat7/webapps/readonline/swfFile/s.swf");

System.out.println("第一步:生成文件对象,准备转换");

}

@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp)

throws ServletException, IOException {

doPost(req, resp);

}

@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp)

throws ServletException, IOException {

//转换成pdf文件

if(sourceFile.exists()) {

if(!pdfFile.exists()) {

OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);

try {

connection.connect();

DocumentConverter converter = new OpenOfficeDocumentConverter(connection);   

converter.convert(sourceFilepdfFile);

pdfFile.createNewFile();

connection.disconnect();  

System.out.println("第二步:转换为PDF格式路径" + pdfFile.getPath());

catch (java.net.ConnectException e) {

e.printStackTrace();

System.out.println("OpenOffice服务未启动");

throw e;

catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {

e.printStackTrace();

System.out.println("读取文件失败");

throw e;

catch (Exception e){

e.printStackTrace();

try {

throw e;

catch (Exception e1) {

e1.printStackTrace();

}

}

else {

System.out.println("已转换为PDF,无需再次转换");

}

else {

System.out.println("要转换的文件不存在");

//转换成swf文件

r = Runtime.getRuntime();

if(!swfFile.exists()){

if(pdfFile.exists()) {

try {

Process p = r.exec("D:/Program Files/SWFTools/pdf2swf.exe " + pdfFile.getPath() + " -o " + swfFile.getPath() + " -T 9");

p.waitFor();

swfFile.createNewFile();

System.out.println("第三步:转换为SWF格式路径:" + swfFile.getPath());

System.out.println("si步:转换为SWF格式mingcheng" + swfFile.getName());

if(pdfFile.exists()) {

pdfFile.delete();

}

catch (Exception e) {

e.printStackTrace();

try {

throw e;

catch (Exception e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

}

else {

System.out.println("PDF文件不存在,无法转换");

}

else {

System.out.println("已经转为SWF文件,无需再次转换");

}

HttpSession session = req.getSession();

session.setAttribute("fileName"swfFile.getName());

resp.sendRedirect(req.getContextPath()+"/flexpaper/readFile.jsp");

}

}

⑤ web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" 

xmlns="http://java.sun.com/xml/ns/javaee" 

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

  <servlet>

  <servlet-name>convertUtil</servlet-name>

  <servlet-class>servlet.ConvertServlet</servlet-class>

  </servlet>

  <servlet-mapping>

  <servlet-name>convertUtil</servlet-name>

  <url-pattern>/convertUtil</url-pattern>

  </servlet-mapping>

</web-app>

⑥ readFile.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!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>

        <style type="text/css" media="screen"> 

html, bodyheight:100%; }

body { margin:0padding:0overflow:auto; }   

#flashContent { display:none; }

        </style> 

<script type="text/javascript" src="js/flexpaper_flash.js"></script>

  </head>

  

  <body>

    <div style="position:absolute;left:200px;top:10px;">

        <a id="viewerPlaceHolder" style="width:1000px;height:800px;display:block"></a>

        

        <script type="text/javascript">

     var fp = new FlexPaperViewer(

 'FlexPaperViewer',

 'viewerPlaceHolder', { config : {

 SwfFile : escape('http://localhost:8080/readonline/swfFile/<%=(String)session.getAttribute("fileName")%>'),

 Scale : 0.6, 

 ZoomTransition : 'easeOut',

 ZoomTime : 0.5,

 ZoomInterval : 0.2,

 FitPageOnLoad : true,

 FitWidthOnLoad : false,

 PrintEnabled : false,

 FullScreenAsMaxWindow : false,

 ProgressiveLoading : true,

 MinZoomSize : 0.2,

 MaxZoomSize : 5,

 SearchMatchAll : false,

 InitViewMode : 'Portrait',

 

 ViewModeToolsVisible : true,

 ZoomToolsVisible : true,

 NavToolsVisible : true,

 CursorToolsVisible : true,

 SearchToolsVisible : true,

  

   localeChain: 'zh_CN'

 }});

        </script>

        </div>

   </body>

</html>

需要注意的问题

① 

ConvertServlet中的p.waitFor();很重要,如果没有改行代码,可能出现PDF不能成功转换成SWF的情况。

② 

新建readFile.jsp的时候一定要把<head>标签中间的<base href="<%=basePath%>">删掉,否则FlexPaper不能浏览文件。

③ 

FlexPaper不能正常浏览文件的时候,可以试试在官网添加信任。网址http://www.cnblogs.com/qinpeifeng107/archive/2011/08/29/2158879.html

④ 

readFile.jsp中动态显示文件名称的写法:第一种SwfFile : escape('http://localhost:8080/readonline/swfFile/<%=(String)session.getAttribute("fileName")%>'); 第二种:escape('../swfFile/<%=(String)session.getAttribute("fileName")%>')

⑤ 

项目执行需要启动OpenOffice服务,在系统命令窗口执行命令。

cd C:\Program Files\OpenOffice.org 3\program

soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

实现效果图


第三种实现方式

http://blog.csdn.net/liuyuhua0066/article/details/6603493

安装上FlashPaper之后,将上面网址中的代码替换ConvertServlet中的转换代码即可。FlexPaper用法同上面介绍。

FlashPaper序列号FPD200-59917-97447-41275

如果仍有问题或者想获取Demo或者提出改进请加QQ897259924

参考资料

http://topic.csdn.net/u/20110712/18/4daf5746-e64e-434d-aeb0-77b05f6c9903.html

http://guojun2sq.blog.163.com/blog/static/643308612011328115516368/

http://www.cnblogs.com/qinpeifeng107/archive/2011/08/29/2158879.html

http://yanjingying1986.iteye.com/blog/713187

http://www.cnblogs.com/compass/articles/2046311.html


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值