欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

学习Java模拟实现百度文档在线浏览

程序员文章站 2024-03-06 08:27:07
这个思路是我参考网上而来,代码是我实现。 采用apache下面的openoffice将资源文件转化为pdf文件,然后将pdf文件转化为swf文件,用flexpaper浏览...

这个思路是我参考网上而来,代码是我实现。
采用apache下面的openoffice将资源文件转化为pdf文件,然后将pdf文件转化为swf文件,用flexpaper浏览。
ok,
a、下载openoffice (转换资源文件)
b、下载jodconverter(调用openoffice)
c、下载swftools(pdf2swf)
d、下载 flexpaper(浏览swf文件)

下载之后,先别急安装,请看完这篇博文

1、先看我们的myeclipse工程结构
学习Java模拟实现百度文档在线浏览

2、将我们下载下来的jodconverter-2.2.2.zip解压之后将所有的jar文件拷贝到baidudoc的lib下面去

学习Java模拟实现百度文档在线浏览

3、在webroot下面新建flexpaper文件夹,将解压后的flexpaper全部拷贝到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中的代码,不会你可以参考这里

学习Java模拟实现百度文档在线浏览

/**************************************************************************************/

7、到这里就完成,需要注意的是:
(1)、swftools-2013-04-09-1007.exe文件安装路径不要太深,不然java调用外部命令不能执行

(2)、

    学习Java模拟实现百度文档在线浏览   

    2.1、红色1标记路径不能错,我就犯这个错误了       
    2.2、红色标记2还可以写http://127.0.0.1:8080/baidudoc/reader/...

(3)、启动openoffice的命令,不是直接双击启动的。官网启动方式,使用cd命令打开安装目录!
安装完openoffice后
a.安装服务
cd c:\program files (x86)\openoffice4\program
这一步你可以看你的openoffice安装哪里
执行
soffice -headless-accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
b.查看是否安装成功
   2.1查看端口对应的pid
   netstat -ano|findstr "8100"
   2.2查看pid对应的服务程序名
   tasklist|findstr "ipd值"

效果图示:

学习Java模拟实现百度文档在线浏览

以上就是本文的全部内容,希望对大家的学习有所帮助。