首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 綜合教學

Java + FlexPaper + SWFTools 實現文檔在線預覽完整方案

     編輯:Java 綜合教學

Java + FlexPaper + SWFTools 文檔在線預覽 Demo

1. 核心原理

  1. OpenOffice 將 Word/Excel/PPT/TXT 轉為 PDF。
  2. SWFTools 將 PDF 轉為 SWF。
  3. FlexPaper 組件在頁面播放 SWF。

2. 組件下載

3. 安裝與啟動 OpenOffice 服務

安裝完成後,以命令行啟動服務(端口 8100):

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

OpenOffice 服務啟動

4. 開發步驟

4.1 專案結構

將 FlexPaper 的 js/ (含 flexpaper_flash.js, jquery.js) 及 FlexPaperViewer.swf 複製至 Web 根目錄。

目錄結構

4.2 核心轉換類 DocConverterLocal

public class DocConverterLocal {
    private String SWFTools_Windows = "D:/.../pdf2swf.exe ";
    private File docFile, pdfFile, swfFile, odtFile;

    public DocConverterLocal(String filePath) { ini(filePath); }

    private void ini(String fileString) { /* 初始化文件路徑,處理 TXT 轉 ODT 邏輯 */ }

    private void doc2pdf() throws Exception {
        if (!pdfFile.exists()) {
            OpenOfficeConnection conn = new SocketOpenOfficeConnection(8100);
            conn.connect();
            DocumentConverter converter = new OpenOfficeDocumentConverter(conn);
            converter.convert(docFile, pdfFile);
            conn.disconnect();
        }
    }

    private void pdf2swf() throws Exception {
        if (!swfFile.exists() && pdfFile.exists()) {
            Process p = Runtime.getRuntime().exec(SWFTools_Windows + pdfFile.getPath() + " -o " + swfFile.getPath() + " -T 9");
            // 處理輸出流
            if (pdfFile.exists()) pdfFile.delete();
        }
    }

    public boolean conver() { /* 協調 doc2pdf + pdf2swf */ }
    public String getswfPath() { /* 返回 SWF 路徑 */ }
}

4.3 頁面入口 docUploadConvertLocalAction.jsp

接收上傳文件,調用 DocConverterLocal.conver(),獲取 SWF 路徑轉發至預覽頁。

4.4 預覽頁 documentlocalView.jsp

引入 FlexPaper JS,初始化 Viewer 指向 SWF 路徑。

5. 部署驗證

啟動 Tomcat,訪問 https://localhost:8080/.../docUploadConvertLocalAction.jsp 上傳文件預覽。

上傳頁面

預覽效果

源碼下載:百度網盤 (密碼 3v3q)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved