最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

學習Java模擬實現(xiàn)百度文檔在線瀏覽

 更新時間:2015年07月30日 09:02:49   作者:小夜的傳說  
這片文章介紹了如何使用Java模擬實現(xiàn)百度文檔在線瀏覽,文章思路清晰,需要的朋友可以參考下

這個思路是我參考網(wǎng)上而來,代碼是我實現(xiàn)。
采用Apache下面的OpenOffice將資源文件轉(zhuǎn)化為pdf文件,然后將pdf文件轉(zhuǎn)化為swf文件,用FlexPaper瀏覽。
ok,
A、下載OpenOffice (轉(zhuǎn)換資源文件)
B、下載JodConverter(調(diào)用OpenOffice)
C、下載Swftools(Pdf2Swf)
D、下載 FlexPaper(瀏覽swf文件)

下載之后,先別急安裝,請看完這篇博文

1、先看我們的MyEclipse工程結(jié)構(gòu)

2、將我們下載下來的jodconverter-2.2.2.zip解壓之后將所有的jar文件拷貝到baiduDoc的lib下面去

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版本轉(zhuǎn)化為PDF),這一步需要調(diào)用OpenOffice
 *    3、第三部,將資源文件轉(zhuǎn)換好的PDF文件轉(zhuǎn)換為swf文件,使用FlexPaperViewer.swf進行瀏覽查看
 */
public class BaiDuServlet extends HttpServlet {
  private File sourceFile;// 要轉(zhuǎn)化的源文件
  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("."));
    // 創(chuàng)建三個文件對象
    sourceFile = new File(filePath);
    pdfFile = new File(fileName + ".pdf");
    swfFile = new File(fileName + ".swf");
    System.out.println(pdfFile);
    System.out.println(swfFile);
    // 1、將源文件轉(zhuǎn)化為pdf格式文件
    src2pdf();
    try {
      // 2、將pdf文件轉(zhuǎn)化為swf文件
      pdf2swf();
    } catch (Exception e) {
      e.printStackTrace();
    }
    // 將轉(zhuǎn)化好的文件綁定到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://源文件轉(zhuǎn)化為PDF文件
   */
  private void src2pdf() {
    if (sourceFile.exists()) {
      // 如果不存在,需要轉(zhuǎn)份為PDF文件
      if (!pdfFile.exists()) {
        // 啟用OpenOffice提供的轉(zhuǎn)化服務
        OpenOfficeConnection conn = new SocketOpenOfficeConnection(8100);
        // 連接OpenOffice服務器
        try {
          conn.connect();
          // 建立文件轉(zhuǎn)換器對象
          DocumentConverter converter = new OpenOfficeDocumentConverter(
              conn);
          converter.convert(sourceFile, pdfFile);
          // 斷開鏈接
          conn.disconnect();
          System.out.println("轉(zhuǎn)換成功");
        } catch (ConnectException e) {
          e.printStackTrace();
        }
      } else {
        System.out.println("已經(jīng)存在PDF文件,不需要在轉(zhuǎn)換??!");
      }
    } else {
      System.out.println("文件路徑不存在?。。?);
    }
 
  }
 
  /**
   * @Author:NuoYan
   * @Date:2015-2-2 下午2:28:32
   * @throws Exception
   * TODO:PDF轉(zhuǎn)化為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("轉(zhuǎn)換命令:" + command);
        // Java調(diào)用外部命令,執(zhí)行pdf轉(zhuǎn)化為swf
        Runtime r = Runtime.getRuntime();
        Process p = r.exec(command);
        System.out.println(loadStream(p.getInputStream()));
        System.out.println("swf文件轉(zhuǎn)份成功!??!");
        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)、swftools-2013-04-09-1007.exe文件安裝路徑不要太深,不然Java調(diào)用外部命令不能執(zhí)行

(2)、

       

    2.1、紅色1標記路徑不能錯,我就犯這個錯誤了       
    2.2、紅色標記2還可以寫http://127.0.0.1:8080/baiduDoc/reader/...

(3)、啟動OpenOffice的命令,不是直接雙擊啟動的。官網(wǎng)啟動方式,使用cd命令打開安裝目錄!
安裝完openoffice后
A.安裝服務
cd C:\Program Files (x86)\OpenOffice4\program
這一步你可以看你的OpenOffice安裝哪里
執(zhí)行
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值"

效果圖示:

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助。

相關(guān)文章

  • vue+ java 實現(xiàn)多級菜單遞歸效果

    vue+ java 實現(xiàn)多級菜單遞歸效果

    這篇文章主要介紹了vue+ java 實現(xiàn)多級菜單遞歸效果,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-12-12
  • SpringBoot和Swagger結(jié)合提高API開發(fā)效率

    SpringBoot和Swagger結(jié)合提高API開發(fā)效率

    這篇文章主要介紹了SpringBoot和Swagger結(jié)合提高API開發(fā)效率的相關(guān)資料,需要的朋友可以參考下
    2017-09-09
  • Java計時器StopWatch實現(xiàn)方法代碼實例

    Java計時器StopWatch實現(xiàn)方法代碼實例

    這篇文章主要介紹了Java計時器StopWatch實現(xiàn)方法代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-07-07
  • Redis 訂閱發(fā)布_Jedis實現(xiàn)方法

    Redis 訂閱發(fā)布_Jedis實現(xiàn)方法

    下面小編就為大家?guī)硪黄猂edis 訂閱發(fā)布_Jedis實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • Java經(jīng)理與員工的差異實現(xiàn)方法

    Java經(jīng)理與員工的差異實現(xiàn)方法

    這篇文章主要介紹了Java經(jīng)理與員工的差異實現(xiàn)方法,需要的朋友可以參考下
    2014-03-03
  • Java線性結(jié)構(gòu)中棧、隊列和串的基本概念和特點詳解

    Java線性結(jié)構(gòu)中棧、隊列和串的基本概念和特點詳解

    前幾天小編給大家介紹了Java線性結(jié)構(gòu)中的鏈表,除了鏈表這種結(jié)構(gòu)之外,實際上還有棧、隊列、串等結(jié)構(gòu),那么這些結(jié)構(gòu)又有哪些特點呢,本文就給大家詳細的介紹一下,感興趣的小伙伴跟著小編一起來看看吧
    2023-07-07
  • 基于Java反射的map自動裝配JavaBean工具類設計示例代碼

    基于Java反射的map自動裝配JavaBean工具類設計示例代碼

    這篇文章主要給大家介紹了關(guān)于基于Java反射的map自動裝配JavaBean工具類設計的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用java具有一定的參考學習價值,需要的朋友們下面來一起看看吧
    2018-10-10
  • Java中定時任務的6種實現(xiàn)方式

    Java中定時任務的6種實現(xiàn)方式

    這篇文章主要給大家分享的是Java中定時任務的6種實現(xiàn)方式,幾乎在所有的項目中,定時任務的使用都是不可或缺的,如果使用不當甚至會造成資損,下面文章我們就來看看Java中定時任務的具體使用方式吧
    2021-10-10
  • java 遍歷request中的所有表單數(shù)據(jù)的實例代碼

    java 遍歷request中的所有表單數(shù)據(jù)的實例代碼

    下面小編就為大家?guī)硪黄猨ava 遍歷request中的所有表單數(shù)據(jù)的實例代碼。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-09-09
  • java對象池管理方式common-pool2使用

    java對象池管理方式common-pool2使用

    這篇文章主要為大家介紹了java對象池common-pool2使用示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-05-05

最新評論

察哈| 阳泉市| 荔波县| 万州区| 思茅市| 武胜县| 大渡口区| 修武县| 永州市| 安义县| 东方市| 靖宇县| 抚顺县| 崇信县| 罗甸县| 鄂尔多斯市| 宜阳县| 白山市| 鲁甸县| 东明县| 体育| 大新县| 灵丘县| 福贡县| 平陆县| 郯城县| 志丹县| 枣庄市| 涞源县| 东阳市| 鹿邑县| 交城县| 越西县| 八宿县| 客服| 土默特左旗| 喀喇| 双城市| 巴东县| 桑日县| 旬邑县|