当前位置:首页 > 网文 > 技术杂谈 > 实现类似百度文库在线观看功能笔记

实现类似百度文库在线观看功能笔记

实现方式调研: 网上大致看了下实现的方式(借鉴了前人的文档,在此谢过),发现还是蛮多资料的。大体上有四种转换的方式:

  1. Txt/Word/Excel/PPT=>PDF(OpenOffice+JodConverter)=>SWF(pdf2swf)=>FlexPaper浏览
  2. Txt/Word/Excel/PPT=>PDF(MSOffice+JACOB)=>SWF(pdf2swf)=>FlexPaper浏览
  3. Txt/Word/Excel/PPT=>SWF (FlashPaper)=> FlexPaper浏览
  4. Txt/Word/Excel/PPT=>SWF(print2flash)=> FlexPaper浏览
上面1,2两种实现方式相对比较复杂,需要先将其他类型转换为PDF类型,再转成SWF类型在容器中显示。所以先看了下后2种,先说说flashpaper吧,此款软件确实是转换格式的神器,可惜是收费的。不过可以试用30天,对于这种小型的可试用的软件,我一般是装在虚拟机中,一直试用,到期了再恢复快照即可。说实话,效果不错,但不适合开发用,因为木有买的冲动,只能忍痛割爱了!再说说print2flash,这款软件是开源的,也能实现转换swf的功能,但是上网找了半天,关于它的代码资料很是匮乏。Java的更是凤毛麟角,无从下手,只好先晾着。希望某位大虾有关于这个的代码资料可以共享,本人感激不尽啊!剩下的只有相对复杂的2种,不多说了,鉴于经典案例及网上资料的丰富性,我最终还是“屈服”在了第一种的组合上了。

资料和工具准备:

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  

资料方面,大致搜索了百度文库和开源中国上的一些资料,其实都大同小异。前辈们已经为我们做了很多了,再次谢过各位分享的前辈。

项目目录及源码:

工程目录大致如下:

另外,在lib下需要加入JodConverter压缩包中lib目录下的jar包,全部复制进去即可。

ConvertServletservlet处理类代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<b>package org.gfg.test.servlet;
 
importjava.io.File;
importjava.io.IOException;
 
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjavax.servlet.http.HttpSession;
 
importcom.artofsolving.jodconverter.DocumentConverter;
importcom.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
importcom.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
importcom.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
 
publicclassConvertServletextends HttpServlet {
    private File sourceFile;       //转换源文件
    private File pdfFile;          //PDF目标文件
    private File swfFile;          //SWF目标文件
    private Runtime r;             
     
    public voidinit()throws ServletException {
        sourceFile =new File(“G:\\tomcat6\\webapps\\readonline\\swfFile\\1.doc”);
        pdfFile =new File(“G:\\tomcat6\\webapps\\readonline\\swfFile\\1.pdf”);
        swfFile =new File(“G:\\tomcat6\\webapps\\readonline\\swfFile\\Paper.swf”);
        System.out.println(“第一步:生成文件对象,准备转换”);
    }
    public voiddoGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
 
        doPost(request, response);
    }
 
    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     *
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public voiddoPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType(“text/html”);
        //转换成pdf文件
        if(sourceFile.exists()) {
            if(!pdfFile.exists()) {
                OpenOfficeConnection connection =new SocketOpenOfficeConnection(8100);
                try {
                    connection.connect();
                    DocumentConverter converter =new OpenOfficeDocumentConverter(connection);  
                    converter.convert(sourceFile, pdfFile);
                    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();
         
        new Thread(){
 
            @Override
            public voidrun() {
                if(!swfFile.exists()){
                     
                    if(pdfFile.exists()) {
                        try {
                            System.out.println(“E:/SWFTools/pdf2swf.exe “ + pdfFile.getPath() +
                                    ” -o “ + swfFile.getPath() +” -T 9″);
                             
                            Process p = r.exec(“E:/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文件,无需再次转换”);
                }
            }
             
        }.start();
         
        HttpSession session = request.getSession();
        session.setAttribute(“fileName”, swfFile.getName());
        System.out.println(“我是测试:”+session.getAttribute(“fileName”));
        response.sendRedirect(request.getContextPath()+”/readFile.jsp”);
    }
    }
</b>

Readfile.Jsp显示界面代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<%@ 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, body  { height:100%; }
            body { margin:0; padding:0; overflow:auto; }  
            #flashContent { display:none; }
        </style>
        <script type=”text/javascript” src=”flexpaper/js/flexpaper_flash_debug.js”></script>
        <script type=”text/javascript” src=”flexpaper/js/jquery.js”></script>
        <script type=”text/javascript” src=”flexpaper/js/flexpaper_flash.js”></script>
  </head>
   
  <body>
    <div style=”position:absolute;left:200px;top:10px;”>
     
           <center> <a id=”viewerPlaceHolder” style=”width:1000px;height:800px;display:block”>努力加载中…………</a></center>
            <script type=”text/javascript”>
            $(document).ready(function(){
            var fp =new FlexPaperViewer(  
                         ‘flexpaper/FlexPaperViewer’,
                         ‘viewerPlaceHolder’, { config : {
                         SwfFile : escape(‘http:\/\/192.168.0.13:8080\/readonline\/swfFile\/<%=request.getAttribute(“fileName”)%>’),
                         Scale :0.6,
                         ZoomTransition :’easeOut’,
                         ZoomTime :0.5,
                         ZoomInterval :0.2,
                         FitPageOnLoad :true,
                         FitWidthOnLoad :false,
                         PrintEnabled :true,
                         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>

要特别注意的是:FlexPaperViewer的路径一定要正确,id对上。另外,可以用官方的Paper.swf作为测试,只需要将SwfFile的地址改掉即可。

大致显示效果如下:


除此之外,关于一直加载无法显示的问题和无法打开自有swf文件的问题,据说是adobe的信任问题,还在解决中!测试了一下,好像生成的速度还行,就是貌似有servlet请求超时导致请求中断的情况!反正基本差不多了,就差优化了!就这样吧。

 

实现类似百度文库在线观看功能笔记:等您坐沙发呢!

发表评论

表情
还能输入210个字