構建文件格式轉換服務器
1. 爲什麼要做文件格式轉換服務器
不知道各位是否遇到這樣的情況, 當您在瀏覽IBM SUN等公司的技術網站的時候, 你會發現在每個頁面的上方有小圖標, 你可以把當前的頁面內容以word, pdf或者是其他的格式進行下載, 可能你在想, 恩, 大公司的做法確實很正規, 他們提供了許多的格式版本提供不同的用戶下載, 我想你可能是對的, 但是我們換一種想法假設我們構建一個服務器, 讓所有的格式可以自由地轉換成我們所希望地格式:
假設我們的Java代碼是這樣寫的
Public static FileTransfer{
Public static convert(File inputFile ,File outputFile){
………
}
}
|
那麼我們可以在Jsp Servlet裏面調用這個靜態方法, 實現文檔的格式的轉換。
2. 怎麼實現
好了, 設計思想有了, 怎麼實現呢, 好在Java有大量的開源社區,而且我們有google , 不出一分鐘, 我們可以從互聯網找到答案:
這個是一個利用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文件
找到:
<prop oor:name="ooSetupInstCompleted">
<value>false</value>
</prop>
<prop oor:name="ooSetupShowIntro">
<value>true</value>
</prop>
|
修改成
<prop oor:name="ooSetupInstCompleted" oor:type="xs:boolean">
<value>true</value>
</prop>
<prop oor:name="LicenseAcceptDate" oor:type="xs:string">
<value>2006-07-25T17:34:04</value>
</prop>
<prop oor:name="FirstStartWizardCompleted" oor:type="xs:boolean">
<value>true</value>
</prop>
|
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 &
/usr/opt/openoffice.org2.2/program/soffice.bin -headless -display:5 -accept="socket,host=0.0.0.0,port=8100;urp;" &
;;
stop)
pkill soffice
;;
*)
echo "Usage: $0 { start | stop }"
exit 1
;;
esac
exit 0
|
以上代碼在solaris 10下面測試通過, 其他的版本, 根據Xvfb的版本不一 , 進行調整參數即可:/usr/openwin/bin/Xvfb :5 screen 1024x768x24 &
# chmod a+x ./ooService
在/etc/rc3.d 或者init.d裏面創建一個文件 S90ooService
# touch S90ooService
# vi S90ooService
將裏面的內容改成
#!/sbin/sh
/usr/opt/openoffice.org2.2/program/ooservice start
|
#chmod a+x ./S90ooService
4. 使用實例
實例代碼如下
<%@ page import="java.io.File"%>
<%@ page import="com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection"%>
<%@ page import="com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection"%>
<%@ page import="com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter"%>
<%@ page import="com.artofsolving.jodconverter.DocumentConverter"%>
<%--
Created by IntelliJ IDEA.
User: Henry
Date: 2007-7-27
Time: 15:30:43
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
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();
%>
<html>
<head><title>Simple jsp page</title></head>
<body>Place your content here</body>
</html>
5. 擴展使用
在上述代碼裏, 使用onverter.convert(inputStream , DocumentFormat, outputStream , DocumentFormat);方法, 可以直接將servlet的outwriter對象作爲輸出流, 既可以實現在serverlet裏面轉換文件了。
關於作者
OldJavaMan,長期致力於Java相關領域的技術工作, 主要參與J2EE相關程序的設計, 目前在南京的一家軟件企業就職,他希望和廣大的Java愛好者結交朋友。大家可以通過mail聯繫他 。