實現一個具有百度文庫文件轉換功能的工具類

鍾超發表於2012-02-07

轉載請註明來自“柳大的CSDN部落格”,原文地址:http://blog.csdn.net/poechant/article/details/7238056


用過百度文庫、豆丁的朋友,都知道,我們只要將文件上傳到這些網站上,就可以實現分享。無論你上傳的是pdfppt,還是doctxtpptx檔案,都會在網頁上顯示給使用者看。從技術上簡單地說,可以分成三步:


1)將格式為x的文件檔案上傳到伺服器;

2)在伺服器上完成文件的相關處理;

3)將處理後的結果展示在網頁上。


其中最關鍵的一步就是(2)。做技術的人都可以很容易地看出來,這類用web應用是用flash顯示出來的。那麼,一個文件檔案,是如何轉換為swf檔案的呢?本文就將為你介紹,如何避免再造車輪的冗餘行為發生,而藉助高效的工具將docpptpdf等文件檔案轉換為swf格式。比如百度文庫接收使用者上傳的檔案後,顯示在網頁上時是其轉換成的SWF格式檔案。這樣就很適合Web應用了。


一、JODConverter


首先利用JODConverterdocppttxtdocx等檔案轉換為pdf檔案。然後用SwfToolspdf檔案轉換為swf檔案。JODConverter是一個名叫Java OpenDocument Converter的基於LGPL協議的軟體,用於實現不同的office格式之間的轉換,這些office格式包括Microsoft OfficeOpenOfficeJODConverter的專案主頁是http://www.artofsolving.com/opensource/jodconverter,可以檢視更多的資訊。可以通過SourceForge.net下載。


首先要安裝OpenOfficeJODConverter的功能,基本就是實現了OpenOffice中的另存為的功能。個版本的OpenOffice的下載地址為:


Language

The names do not refer to countries.

Windows

Intel

MSI

Linux

Intel

RPM

Linux

Intel

DEB

Linux

x86-64

RPM

Linux

x86-64

DEB

MacOS

Intel

DMG

MacOS

PPC¹

DMG

Solaris

Intel

PKG

Solaris

Sparc

PKG

Arabic

عربي

Download

Download

Download

Download

Download

Download


Download

Download

Basque

Euskara

Download

Download

Download

Download

Download

Download

Download

Download

Download

Bengali

বাংলা

Download

Download

Download

Download

Download

Download

Download

Download

Download

Bulgarian

български език

Download

Download

Download

Download

Download

Download


Download

Download

Catalan

Català

Download

Download

Download

Download

Download

Download

Download

Download

Download

Catalan (Valencian)

Català (Valencià)

Download

Download

Download

Download

Download

Download

not available

Download

Download

Chinese (simplified)

簡體中文

Download

Download

Download

Download

Download

Download


Download

Download

Chinese (traditional)

正體中文

Download

Download

Download

Download

Download

Download


Download

Download

Czech

čeština

Download

Download

Download

Download

Download

Download


Download

Download

Danish

Dansk

Download

Download

Download

Download

Download

Download

3.2.1

Download

Download

Dutch

Nederlands

Download

Download

Download

Download

Download

Download

Download

Download

Download

English (British)

English (British)

Download

Download

Download

Download

Download

Download


Download

Download

English (US)

English (US)

Download

Download

Download

Download

Download

Download

3.2.1

Download

Download

Estonian

Eesti keel

Download

Download

Download

Download

Download

Download


Download

Download

Finnish

Suomi

Download


Download



3.2.1

Download

Download

Download

French

Français

Download

Download

Download

Download

Download

Download

Download

Download

Download

Galician

Galego

Download

Download

Download

Download

Download

Download


Download

Download

German

Deutsch

Download

3.2.1

Download

3.2.1

3.2.1

Download

3.2.0

Download

Download

Greek

Ελληνικά

Download

Download

Download

Download

Download

Download


Download

Download

Hebrew

עִבְרִית

Download

Download

Download

Download

Download

Download

Download

Download

Download

Hungarian

Magyar

Download

Download

Download

Download

Download

Download


Download

Download

Irish

Gaeilge

Download

Download

Download

Download

Download

Download


Download

Download

Italian

Italiano

Download

Download

Download

Download

Download

Download




Language

The names do not refer to countries.

Windows

Intel

MSI

Linux

Intel

RPM

Linux

Intel

DEB

Linux

x86-64

RPM

Linux

x86-64

DEB

MacOS

Intel

DMG

MacOS

PPC¹

DMG

Solaris

Intel

PKG

Solaris

Sparc

PKG

Japanese

日本語

Download

Download

Download

Download

Download

Download

Download

Download

Download

Khmer

ភាសាខ្មែរ

Download

Download

Download

Download

Download

Download

3.2.1

Download

Download

Korean

한국어

Download

Download

Download



Download


Download

Download

Kurdish

Kurdî

3.2.1









Lithuanian

Lietuvių

Download

Download

Download

Download

Download

Download

3.0.1

Download

Download

Macedonian

Македонски

Download

Download

Download

Download

Download

Download

3.0.0

Download

Download

Mongolian

Монгол

3.0.0

3.0.0

3.0.0







Norwegian (Bokmål)

Norsk (Bokmål)

Download

Download

Download

Download

Download

Download


Download

Download

Norwegian (Nynorsk)

Norsk (Nynorsk)

3.2.0

3.2.0

3.2.0

3.2.0

3.2.0





Oromo

ኦሮሚኛ

Download

Download

Download

Download

Download

Download


Download

Download

Polish

Polski

Download

Download

Download

Download

Download

Download


Download

Download

Portuguese (Brazilian)

Português (do Brasil)

Download

Download

Download

Download

Download

Download


Download

Download

Portuguese (European)

Português (Europeu)

Download

Download

Download

3.2.1

3.2.1

Download


Download

Download

Russian

Русский

Download

Download

Download

Download

Download

Download


Download

Download

Serbian (Cyrillic)

Cрпски (ћирилицом)

Download

Download

Download

Download

Download

Download


Download

Download

Serbian (Latin)

Srpski (latinicom)

Download

Download

Download

Download

Download

Download


Download

Download

Slovenian

Slovenski jezik

Download

Download

Download

Download

Download

Download

Download

Download

Download

Spanish

Español

Download

Download

Download

Download

Download

Download

3.0.0

Download

Download

Swedish

Svenska

Download

Download

Download

Download

Download

Download

3.1.0

Download

Download

Turkish

Türkçe

Download

Download

Download

Download

Download

Download


Download

Download

Vietnamese

Tiếng Việt

Download

Download

Download

Download

Download

Download


Download

Download


安裝完OpenOffice後,要記住你的OfficeHome。不同平臺的OfficeHome是不同的。比如你將OpenOffice安裝到Windows“D:\Program Files\OpenOffice.org 3”,那麼這就是你在Windows上的OfficeHome。如果你是Linux使用者,你可能會將OpenOffice安裝到“/opt/openoffice.org3”,這就是你的OfficeHome。如果你是Mac使用者,在Mac系統的自動安裝後,你需要知道你的OfficeHome“/Application/OpenOffice.org.app/Contents”

一定要知道你的OfficeHome,否則JODConverter是沒法工作的。然後下載JODConverter API,下載頁:http://sourceforge.net/projects/jodconverter/


二、SwfTools


然後利用SwfTools就中間生成的pdf檔案,轉換為swf檔案。SwfTools是將各種不同格式的檔案轉換為Adobe Flashswf格式檔案的軟體,可以執行在LinuxMacFreeBSDUnix-like系統以及Windows系統上。它可以實現pdfaviwavpngjpeggif等格式向swf格式的轉換。該專案主頁是http://www.swftools.org/,在http://www.swftools.org/download.htmlx下載


1WindowsLinuxMac使用者可以忽略)

1)下載安裝swftools

下載地址:swftools-0.9.1.exe

安裝目錄:D:\Program Files\swftools


2)將不同格式的檔案轉換為SWF

使用安裝目錄下的轉換命令:

avi2swf inputFile -o outputFile
font2swf inputFile -o outputFile
gif2swf inputFile -o outputFile
jpeg2swf inputFile -o outputFile
pdf2swf inputFile -o outputFile
png2swf inputFile -o outputFile
wav2swf inputFile -o outputFile



2LinuxWindowsMac使用者可以忽略)

1)安裝依賴庫

sudo apt-get install libpng12-dev
sudo apt-get install libjpeg62-dev
sudo apt-get install libgif-dev
sudo apt-get install libfreetype6-dev

2)安裝swftools

wget http://www.swftools.org/swftools-0.9.1.tar.gz
tar zxvf swftools-0.9.1.tar.gz
cd swftools-0.9.1
./configure
make
sudo make install

3)將其他檔案格式轉換為SWF

參考Windows平臺上的SwfTools使用。


3MacWindowsLinux使用者可以忽略)

1)安裝swftools

最簡單的方式,是用macportfink等來安裝。macport的命令為:

sudo port install swftools

MacPort安裝SwfTools,不需要考慮其依賴庫,MacPort會幫助你安裝好。MacPort的安裝,請參考《Mac OS X Lion安裝MacPorts(讓你在MacShell下更加遊刃有餘)》一文。


2)將其他檔案格式轉換為SWF

參考Windows平臺上的SwfTools使用。


三、Java例項實現各office檔案格式向swf的轉換


建立一個實現型別轉換的工具類,命名為FileConverterUtil,如下:


public class FileConverterUtil {
    public static String fileToSwf(String inputFilePath) {
        return pdfToSwf(fileToPdf(inputFilePath));
    }
    private static String fileToPdf(String inputFilePath) {
        //...
    }
    private static String pdfToSwf(String inputFilePath) {
        //...
    }
}


然後分別實現fileToPdfpdfToSwffileToPdf中使用到JODConverterAPI,如下:


import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;
import org.artofsolving.jodconverter.OfficeDocumentConverter;
import java.io.File;

private static String fileToPdf(String inputFilePath) {
    DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();
		
    String officeHome = getOfficeHome();
    config.setOfficeHome(officeHome);
	
    OfficeManager officeManager = config.buildOfficeManager();
    officeManager.start();

    OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
    String outputFilePath = getOutputFilePath(inputFilePath);
    converter.convert(new File(inputFilePath), new File(outputFilePath));

    officeManager.stop();
}

其中getOfficeHome()可以根據你使用該工具類的平臺增加程式的可移植性。fileToPdf可以實現各種office檔案到pdf檔案的轉換。


private static String getOfficeHome(String inputFilePath) {
    String osName = System.getProperty("os.name");
    if (Pattern.matches("Linux.*", osName) {
        return "/opt/openoffice.org3";
    } else if (Pattern.matches("Windows.*", osName) {
        return "D:\\Applications\\OpenOffice.org 3";
    } else if (Pattern.matches("Mac.*", osName) {
        return "/Application/OpenOffice.org.app/Contents";
    }
    return null;
}


提供統一的轉換檔案命名規則,可以提供如下方法,具體實現看你應用場景的方便性了。


private static String getOutputFilePath(String inputFilePath) {
    // Initialized by yourself.
}

然後利用SwfTools實現PdfToSwf方法。


private static String pdfToSwf(String inputFilePath) {
    try {
        String outputFilePath = getOutputFilePath(inputFilePath);
        String[] command = new String[4];
        command[0] = getSwftoolsHome();
        command[1] = inputFilePath;
        command[2] = "-o";
        command[3] = outputFilePath;
        Process process = Runtime.getRuntime().exec(command);
        process.waitFor();
        return outputFilePath;
    } catch (Exception e) {
        // initialized by yourself.
    }
}


其中getSwftoolsHome方法就是獲取你的SwfTools所在目錄,依據不同平臺,你可以如下實現該方法:


private static String getSwftoolsHome() {
    String osName = System.getProperty("os.name");
    if (Pattern.matches("Linux.*", osName) {
        return "/usr/local/bin";
    } else if (Pattern.matches("Windows.*", osName) {
        return "D:\\Program Files\\SwfTools";
    } else if (Pattern.matches("Mac.*", osName) {
        return "/usr/local/bin";
    }
    return null;
}

這樣類似於百度文庫、豆丁網的文件轉換工具的核心功能就基本實現了。Enjoy It


轉載請註明來自“柳大的CSDN部落格”,原文地址:http://blog.csdn.net/poechant/article/details/7238056

-

相關文章