天天看點

建構檔案格式轉換伺服器

為什麼要做檔案格式轉換伺服器

     不知道各位是否遇到這樣的情況, 當您在浏覽ibm sun等公司的技術網站的時候, 你會發現在每個頁面的上方有小圖示, 你可以把目前的頁面内容以word, pdf或者是其他的格式進行下載下傳, 可能你在想, 恩, 大公司的做法确實很正規, 他們提供了許多的格式版本提供不同的使用者下載下傳, 我想你可能是對的, 但是我們換一種想法假設我們建構一個伺服器, 讓所有的格式可以自由地轉換成我們所希望地格式:

假設我們的java代碼是這樣寫的

public static filetransfer{

     public static convert(file inputfile ,file outputfile){

      ………

}

那麼我們可以在jsp servlet裡面調用這個靜态方法, 實作文檔的格式的轉換。

2.   怎麼實作

   好了, 設計思想有了, 怎麼實作呢, 好在java有大量的開源社群,而且我們有google , 不出一分鐘, 我們可以從網際網路找到答案:

<a href="http://www.artofsolving.com/">http://www.artofsolving.com/</a>

這個是一個利用openoffice來實作的解決方案, 他已經幫忙做好了整個解決方案。

jodconverter是這個項目的子項,用java語言來實作:

n            microsoft office 轉換成opendocument和viceversa

n            word 格式轉換成 opendocument text (odt);

n            opendocument text (odt) 轉換成 word

n            excel轉換成opendocument spreadsheet (ods);

n            opendocument spreadsheet (ods) 轉換成excel

n            powerpoint轉換成opendocument presentation (odp);

n            opendocument presentation (odp) 轉換成powerpoint

n            任何格式轉換成 pdf

n            opendocument (text, spreadsheet, presentation) 轉換成pdf

n            word to pdf; excel轉換成pdf; powerpoint轉換成pdf

n            rtf轉換成pdf; wordperfect轉換成pdf; ...

n            還有:

n            opendocument presentation (ods) 轉換成flash;

n            powerpoint轉換成flash

n            rtf轉換成opendocument; wordperfect轉換成opendocument

n            任何格式轉化成 html (with limitations)

n            support for openoffice.org 1.0 and old staroffice formats

3.   部署服務

這個文檔假設你已經安裝了openoffice在作業系統上, 在windows上安裝openoffice是非常簡單和愉快的事情, 在linux/unix下面, openoffice網站提供的下載下傳包是tar.gz格式, 用

gunzip –c **.tar.gz

tar –xf *.tar

pkgadd -d /your path/ooo/packages

指令就可以安裝

為了友善使用我按照不同的作業系統來編寫

3.1.           windows 2k xp 2003 server

2.         利用srvany.exe建立一個服務, 如openofficeunoserver

在系統資料庫項 :

 hkey_local_machine/system/controlset001/services/openofficeunoserver

建立子項: parameters 

在parameters下面 建立字元串值 application 和 appparameters,

分别設定值為

c:/program files/openoffice.org 2.2/program/soffice.exe

-accept=socket,host=0.0.0.0,port=8100;urp; -headless

其中soffice.exe的路徑根據您安裝的路徑來修改

在control panel / administrative tools / services 打開該服務, 修改屬性 log on account 為  local service

3.         修改您安裝openoffice路徑下的share/registry/data/org/openoffice/setup.xcu檔案

找到:

&lt;prop oor:name="oosetupinstcompleted"&gt;

 &lt;value&gt;false&lt;/value&gt;

&lt;/prop&gt;

&lt;prop oor:name="oosetupshowintro"&gt;

 &lt;value&gt;true&lt;/value&gt;

修改成

&lt;prop oor:name="oosetupinstcompleted" oor:type="xs:boolean"&gt;

&lt;prop oor:name="licenseacceptdate" oor:type="xs:string"&gt;

 &lt;value&gt;2006-07-25t17:34:04&lt;/value&gt;

&lt;prop oor:name="firststartwizardcompleted" oor:type="xs:boolean"&gt;

4.         從開始, 程式裡面啟動一次openoffice 将注冊選項設定成不注冊 

5.         啟動openofficeunoserver服務

6.         檢視是否服務已經存在telnet 127.0.0.1 8100

3.2.           linux / unix

linux和unix建立服務相對簡單, 但是由于soffice需要使用到xwindow界面, 是以在做服務的時候, 由于在指令行狀态, 沒有圖形界面的支援, 是以需要使用xvfb來設定一個虛拟的界面。

在soffice.bin目錄建立一個ooservice檔案

# touch ooservice

# vi ./ooservice

将以下内容寫入這個檔案

#!/sbin/sh

case "$1" in

start)

     display=:5.0

     export display

     /usr/openwin/bin/xvfb :5 screen 1024x768x24 &amp;

        /usr/opt/openoffice.org2.2/program/soffice.bin -headless -display:5 -accept="socket,host=0.0.0.0,port=8100;urp;" &amp;

       ;;

stop)

       pkill soffice

*)

       echo "usage: $0 { start | stop }"

       exit 1

esac

exit 0

# chmod a+x ./ooservice

在/etc/rc3.d 或者init.d裡面建立一個檔案 s90ooservice

# touch s90ooservice

# vi s90ooservice

将裡面的内容改成

/usr/opt/openoffice.org2.2/program/ooservice start

#chmod a+x ./s90ooservice

4.   使用執行個體

執行個體代碼如下

建構檔案格式轉換伺服器

&lt;%@ page import="java.io.file"%&gt;

建構檔案格式轉換伺服器

&lt;%@ page import="com.artofsolving.jodconverter.openoffice.connection.openofficeconnection"%&gt;

建構檔案格式轉換伺服器

&lt;%@ page import="com.artofsolving.jodconverter.openoffice.connection.socketopenofficeconnection"%&gt;

建構檔案格式轉換伺服器

&lt;%@ page import="com.artofsolving.jodconverter.openoffice.converter.openofficedocumentconverter"%&gt;

建構檔案格式轉換伺服器

&lt;%@ page import="com.artofsolving.jodconverter.documentconverter"%&gt;

建構檔案格式轉換伺服器

&lt;%--

建構檔案格式轉換伺服器

 created by intellij idea.

建構檔案格式轉換伺服器

 user: henry

建構檔案格式轉換伺服器

 date: 2007-7-27

建構檔案格式轉換伺服器

 time: 15:30:43

建構檔案格式轉換伺服器

 to change this template use file | settings | file templates.

建構檔案格式轉換伺服器

--%&gt;

建構檔案格式轉換伺服器

&lt;%@ page contenttype="text/html;charset=utf-8" language="java" %&gt;

建構檔案格式轉換伺服器

&lt;%

建構檔案格式轉換伺服器

 file inputfile = new file("c://temp//111.ppt");

建構檔案格式轉換伺服器

    file outputfile = new file("c://temp//111.html");

建構檔案格式轉換伺服器
建構檔案格式轉換伺服器

    // 連結 一個運作在8100端口的openoffice.org 執行個體

建構檔案格式轉換伺服器

    openofficeconnection connection = new socketopenofficeconnection(8100);

建構檔案格式轉換伺服器

    connection.connect();

建構檔案格式轉換伺服器
建構檔案格式轉換伺服器

    // 建立一個converter對象并轉換格式

建構檔案格式轉換伺服器

    documentconverter converter = new openofficedocumentconverter(connection);

建構檔案格式轉換伺服器

    converter.convert(inputfile, outputfile);

建構檔案格式轉換伺服器
建構檔案格式轉換伺服器

    // 關閉連接配接

建構檔案格式轉換伺服器

    connection.disconnect();

建構檔案格式轉換伺服器

%&gt;

建構檔案格式轉換伺服器
建構檔案格式轉換伺服器
建構檔案格式轉換伺服器

&lt;html&gt;

建構檔案格式轉換伺服器

 &lt;head&gt;&lt;title&gt;simple jsp page&lt;/title&gt;&lt;/head&gt;

建構檔案格式轉換伺服器

 &lt;body&gt;place your content here&lt;/body&gt;

建構檔案格式轉換伺服器

&lt;/html&gt;

5.   擴充使用

在上述代碼裡, 使用onverter.convert(inputstream , documentformat, outputstream , documentformat);方法, 可以直接将servlet的outwriter對象作為輸出流, 既可以實作在serverlet裡面轉換檔案了。

關于作者