1. 程式人生 > >java SpringMvc 實現檔案線上預覽(openoffice+swftools+flexpaper)

java SpringMvc 實現檔案線上預覽(openoffice+swftools+flexpaper)

        專案需求:伺服器接受的檔案當下只能下載之後才能瀏覽內容,現需要後臺能線上瀏覽到檔案內容,避免繁瑣無用檔案下載操作.

     通過幾天網上資料搜尋,目前免費的線上預覽開發技術使用最多還是(openoffice+swftools+flexpaper),經過2天的學習,終於在本地測試成功,後在伺服器搭建環境也成功上線.          

   1.概述

     主要原理:

1.通過第三方工具openoffice,將word、excel、ppt、txt等檔案轉換為pdf檔案

2.通過swfTools將pdf檔案轉換成swf格式的檔案

3.通過FlexPaper文件元件在頁面上進行展示

2.安裝包下載

1.openoffice是Apache下的一個開放免費的文書處理軟體

下載地址:Apache oppenoffice 官網下載 版本-3.4.1

2.SWFTools是一組用來處理Flash的swf檔案的工具包,我們使用它將pdf檔案轉成swf檔案!

下載地址:SWFTools官網下載 swftools-2013-04-09-1007.exe

3.FlexPaper是一個開源輕量級的在瀏覽器上顯示各種文件的元件

下載地址:FlexPaper官網下載 版本1.4.0(可選擇新版)

4.JODConverter一個Java的OpenDocument 檔案轉換器,在此我們只用到它的jar包

下載地址:JODCConverter下載
openoffice與SWFTools下載完直接安裝就好了,openoffice需要開啟相應服務,進入安裝目錄,進入program檔案,在此處開啟命令列,或者CMD選擇cd到這個檔案,然後輸入命令soffice -headless -accept=”socket,host=127.0.0.1,port=8100;urp;” -nofirststartwizard

4、引入專案
專案結構如下
主要用到的就是其中flexpaper_flash.js和FlexPaperViewer.swf。html頁面是Demo格式,可以根據格式自定義自己的顯示頁面,兩個swf檔案是測試檔案,你直接訪問FlexPaperViewer.html指向的是Paper.swf.如果顯示成功表示你環境搭建好了.
另外我的是maven專案,jar包是統一管理的.其他專案可以直接把下載的JODConverter檔案裡面的lib包匯入專案中即可.
所有的相關jar包依賴如下:

<dependency>
            <groupId>com.artofsolving</groupId>
            <artifactId>jodconverter</artifactId>
            <version>2.2.2</version>
        </dependency>

        <dependency>
            <groupId>com.artofsolving</groupId>
            <artifactId>jodconverter-cli</artifactId>
            <version>2.2.2</version>
        </dependency>


        <dependency>
            <groupId>commons-cli</groupId>
            <artifactId>commons-cli</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.3</version>
        </dependency>
        <dependency>
            <groupId>org.openoffice</groupId>
            <artifactId>juh</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.openoffice</groupId>
            <artifactId>jurt</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.openoffice</groupId>
            <artifactId>ridl</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-jdk14</artifactId>
            <version>1.5.6</version>
        </dependency>
        <dependency>
            <groupId>org.openoffice</groupId>
            <artifactId>unoil</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.thoughtworks.xstream</groupId>
            <artifactId>xstream</artifactId>
            <version>1.3.1</version>
        </dependency>

下一步我們把相應的工具類引入專案:建立DocConverter.class作為我們將檔案轉換成PDF,然後把PDF轉換成SWF格式檔案,此工具類是直接將兩個步驟一起執行的.程式碼:` import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

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;

/**
* doc docx格式轉換
*/
public class DocConverter {
private static final int environment = 1;// 環境 1:windows 2:linux
private String fileString;// (只涉及pdf2swf路徑問題)
private String outputPath = “”;// 輸入路徑 ,如果不設定就輸出在預設的位置
private String fileName;
private File pdfFile;
private File swfFile;
private File docFile;

public DocConverter(String fileString) {  
    ini(fileString);  
}  

/** 
 * 重新設定file 
 *  
 * @param fileString 
 */  
public void setFile(String fileString) {  
    ini(fileString);  
}  

/** 
 * 初始化 
 *  
 * @param fileString 
 */  
private void ini(String fileString) {  
    this.fileString = fileString;  
    fileName = fileString.substring(0, fileString.lastIndexOf("."));  
    docFile = new File(fileString);  
    pdfFile = new File(fileName + ".pdf");  
    swfFile = new File(fileName + ".swf");  

    //我的路徑是從這裡指定的,因為此處擷取的是全路徑+檔名字,去掉的是檔案字尾,如果想指定轉換到指定指定位置可以如下操作:
   //       fileName=fileString.substring(fileString.lastIndexOf("/"),fileString.lastIndexOf("."));

// String path=”全路徑”;
// docFile=new File(fileString);
// pdfFile=new File(path+fileName+”.pdf”);
// swfFile=new File(path+fileName+”.swf”);
}

/** 
 * 轉為PDF 
 *  
 * @param file 
 */  
private void doc2pdf() throws Exception {  
    if (docFile.exists()) {  
        if (!pdfFile.exists()) {  
            OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);  
            try {  
                connection.connect();  
                DocumentConverter converter = new OpenOfficeDocumentConverter(connection);  
                converter.convert(docFile, pdfFile);  
                // close the connection  
                connection.disconnect();  
                System.out.println("****pdf轉換成功,PDF輸出:" + pdfFile.getPath()+ "****");  
            } catch (java.net.ConnectException e) {  
                e.printStackTrace();  
                System.out.println("****swf轉換器異常,openoffice服務未啟動!****");  
                throw e;  
            } catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {  
                e.printStackTrace();  
                System.out.println("****swf轉換器異常,讀取轉換檔案失敗****");  
                throw e;  
            } catch (Exception e) {  
                e.printStackTrace();  
                throw e;  
            }  
        } else {  
            System.out.println("****已經轉換為pdf,不需要再進行轉化****");  
        }  
    } else {  
        System.out.println("****swf轉換器異常,需要轉換的文件不存在,無法轉換****");  
    }  
}  

/** 
 * 轉換成 swf 
 */  
@SuppressWarnings("unused")  
private void pdf2swf() throws Exception {  
    Runtime r = Runtime.getRuntime();  
    if (!swfFile.exists()) {  
        if (pdfFile.exists()) {  
            if (environment == 1) {// windows環境處理  
                try {  
                //此處要指定你機器安裝的pdf2swf.exe全路徑
                    Process p = r.exec("D:/Program Files/SWFTools/pdf2swf.exe "+ pdfFile.getPath() + " -o "+ swfFile.getPath() + " -T 9");  
                    System.out.print(loadStream(p.getInputStream()));  
                    System.err.print(loadStream(p.getErrorStream()));  
                    System.out.print(loadStream(p.getInputStream()));  
                    System.err.println("****swf轉換成功,檔案輸出:"  
                            + swfFile.getPath() + "****");  
                    if (pdfFile.exists()) {  
                        pdfFile.delete();  
                    }  

                } catch (IOException e) {  
                    e.printStackTrace();  
                    throw e;  
                }  
            } else if (environment == 2) {// linux環境處理  
                try {  
                    Process p = r.exec("pdf2swf " + pdfFile.getPath()  
                            + " -o " + swfFile.getPath() + " -T 9");  
                    System.out.print(loadStream(p.getInputStream()));  
                    System.err.print(loadStream(p.getErrorStream()));  
                    System.err.println("****swf轉換成功,檔案輸出:"  
                            + swfFile.getPath() + "****");  
                    if (pdfFile.exists()) {  
                        pdfFile.delete();  
                    }  
                } catch (Exception e) {  
                    e.printStackTrace();  
                    throw e;  
                }  
            }  
        } else {  
            System.out.println("****pdf不存在,無法轉換****");  
        }  
    } else {  
        System.out.println("****swf已經存在不需要轉換****");  
    }  
}  

static String loadStream(InputStream in) throws IOException {  

    int ptr = 0;  
    in = new BufferedInputStream(in);  
    StringBuffer buffer = new StringBuffer();  

    while ((ptr = in.read()) != -1) {  
        buffer.append((char) ptr);  
    }  

    return buffer.toString();  
}  
/** 
 * 轉換主方法 
 */  
@SuppressWarnings("unused")  
public boolean conver() {  

    if (swfFile.exists()) {  
        System.out.println("****swf轉換器開始工作,該檔案已經轉換為swf****");  
        return true;  
    }  

    if (environment == 1) {  
        System.out.println("****swf轉換器開始工作,當前設定執行環境windows****");  
    } else {  
        System.out.println("****swf轉換器開始工作,當前設定執行環境linux****");  
    }  
    try {  
        doc2pdf();  
        pdf2swf();  
    } catch (Exception e) {  
        e.printStackTrace();  
        return false;  
    }  

    if (swfFile.exists()) {  
        return true;  
    } else {  
        return false;  
    }  
}  

/** 
 * 返回檔案路徑 
 *  
 * @param s 
 */  
public String getswfPath() {  
    if (swfFile.exists()) {  
        String tempString = swfFile.getPath();  
        tempString = tempString.replaceAll("\\\\", "/");  
        return tempString;  
    } else {  
        return "";  
    }  

}  
/** 
 * 設定輸出路徑 
 */  
public void setOutputPath(String outputPath) {  
    this.outputPath = outputPath;  
    if (!outputPath.equals("")) {  
        String realName = fileName.substring(fileName.lastIndexOf("/"),  
                fileName.lastIndexOf("."));  
        if (outputPath.charAt(outputPath.length()) == '/') {  
            swfFile = new File(outputPath + realName + ".swf");  
        } else {  
            swfFile = new File(outputPath + realName + ".swf");  
        }  
    }  
}  

}
OK!接下來是顯示JSP頁面previewFile.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<!-- saved from url=(0014)about:internet --> 
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> 
    <head> 
        <title></title>         
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
        <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="${pageContext.request.contextPath}/FlexPaper_1.4.0_flash/js/swfobject/swfobject.js"></script>
        <script type="text/javascript" src="${pageContext.request.contextPath}/FlexPaper_1.4.0_flash/js/flexpaper_flash.js"></script>
        <script type="text/javascript"> 
            <!-- For version detection, set to min. required Flash Player version, or 0 (or 0.0.0), for no version detection. --> 
            var swfVersionStr = "10.0.0";

            var xiSwfUrlStr = "playerProductInstall.swf";

            var flashvars = { 
            //escape中指定的是你swf檔案的路徑
                  SwfFile : escape("${pageContext.request.contextPath}/preview${url}"),
                  //SwfFile : escape("${url}"),
                  Scale : 0.6, 
                  ZoomTransition : "easeOut",
                  ZoomTime : 0.5,
                  ZoomInterval : 0.1,
                  FitPageOnLoad : false,
                  FitWidthOnLoad : true,
                  PrintEnabled : true,
                  FullScreenAsMaxWindow : false,
                  ProgressiveLoading : true,

                  PrintToolsVisible : false,
                  ViewModeToolsVisible : true,
                  ZoomToolsVisible : true,
                  FullScreenVisible : true,
                  NavToolsVisible : true,
                  CursorToolsVisible : true,
                  SearchToolsVisible : true,

                  localeChain: "en_US"
                  };

             var params = {

                }
            params.quality = "high";
            params.bgcolor = "#ffffff";
            params.allowscriptaccess = "sameDomain";
            params.allowfullscreen = "true";
            var attributes = {};
            attributes.id = "FlexPaperViewer";
            attributes.name = "FlexPaperViewer";
            //此處一定要指定你專案下的FlexPaperViewer.swf路徑
            swfobject.embedSWF(             "${pageContext.request.contextPath}/FlexPaper_1.4.0_flash/FlexPaperViewer.swf", "flashContent", 
                "1000", "600",//指定高和寬
                swfVersionStr, xiSwfUrlStr, 
                flashvars, params, attributes);
            swfobject.createCSS("#flashContent", "display:block;text-align:left;");
        </script> 

    </head> 
    <body> 
        <div style="position:absolute;left:10px;top:10px;">
            <div id="flashContent"> 
                <p> 
                    To view this page ensure that Adobe Flash Player version 
                    10.0.0 or greater is installed. 
                </p> 
            </div>
        </div>
   </body> 
</html> 

到這裡,基本配置就完成了,緊接著就是呼叫了`@RequestMapping(“/center/preview.do”)
public String toView(Model model,String fileName,String Url,HttpServletRequest request)
//fileName = “F:/2.docx”;
String ContextPath = Url+fileName;//檔案路徑+檔名
DocConverter converter = new DocConverter(ContextPath);
converter.conver();
String getswfPath = converter.getswfPath();
System.out.println(getswfPath);
model.addAttribute(“url”, getswfPath);
return “previewFile”;
}
以上在windows測試沒有問題.
過程中我遇到過兩種問題情況.
第一種就是路徑問題,各種路徑需要梳理清楚,研究一下整個過程,差不多能解決路徑輸入與輸出問題.整個工作流程無非就是,指定一個檔案的路徑作為引數給轉換器,轉換器呼叫pdfconver()方法把檔案轉換成pdf格式,然後呼叫swfconver()方法將swf格式,裡面夾雜一些判斷,轉換成swf格式之後,將swf檔案路徑在顯示頁面指定.
第二種是生成的swf檔名稱夾帶中文,建議用英文或者數字,至於檔名為中文的swf檔案,我最後也沒測試成功.乾脆我生成swf的時候全生成英文和數字名稱.

最後本地windows實現後,在linux安裝openoffice與swftools,官網有相應的安裝包,和教程,根據教程一步步安裝就可以了,記住把pdf2swf命令設定為全域性,另外把openoffice服務開啟.把專案中的工具環境預設改為2,注意一下路徑.
遇到問題一定要耐心!!!!