实现一个具有百度文库文档转换功能的工具类
来源:互联网 发布:jre-7u9-windows-x64 编辑:程序博客网 时间:2024/06/05 03:20
用过百度文库、豆丁的朋友,都知道,我们只要将文档上传到这些网站上,就可以实现分享。无论你上传的是pdf、ppt,还是doc、txt、pptx文件,都会在网页上显示给用户看。从技术上简单地说,可以分成三步:
(1)将格式为x的文档文件上传到服务器;
(2)在服务器上完成文档的相关处理;
(3)将处理后的结果展示在网页上。
其中最关键的一步就是(2)。做技术的人都可以很容易地看出来,这类用web应用是用flash显示出来的。那么,一个文档文件,是如何转换为swf文件的呢?本文就将为你介绍,如何避免“再造车轮”的冗余行为发生,而借助高效的工具将doc、ppt、pdf等文档文件转换为swf格式。比如百度文库接收用户上传的文件后,显示在网页上时是其转换成的SWF格式文件。这样就很适合Web应用了。
一、JODConverter
首先利用JODConverter将doc、ppt、txt、docx等文件转换为pdf文件。然后用SwfTools将pdf文件转换为swf文件。JODConverter是一个名叫Java OpenDocument Converter的基于LGPL协议的软件,用于实现不同的office格式之间的转换,这些office格式包括Microsoft Office和OpenOffice。JODConverter的项目主页是http://www.artofsolving.com/opensource/jodconverter ,可以查看更多的信息。可以通过SourceForge.net 下载。
首先要安装OpenOffice,JODConverter的功能,基本就是实现了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
Mac OS
Intel
DMG
Mac OS
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
Mac OS
Intel
DMG
Mac OS
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 Flash的swf格式文件的软件,可以运行在Linux、Mac、FreeBSD等Unix-like系统以及Windows系统上。它可以实现pdf、avi、wav、png、jpeg、gif等格式向swf格式的转换。该项目主页是http://www.swftools.org/ ,在http://www.swftools.org/download.htmlx下载 。
1、Windows(Linux和Mac用户可以忽略)
(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
2、Linux(Windows和Mac用户可以忽略)
(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使用。
3、Mac(Windows和Linux用户可以忽略)
(1)安装swftools
最简单的方式,是用macport、fink等来安装。macport的命令为:
- sudo port install swftools
用MacPort安装SwfTools,不需要考虑其依赖库,MacPort会帮助你安装好。MacPort的安装,请参考《Mac OS X Lion安装MacPorts(让你在Mac的Shell下更加游刃有余)》一文。
(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) {
- //...
- }
- }
然后分别实现fileToPdf和pdfToSwf。fileToPdf中使用到JODConverter的API,如下:
- 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!
- 实现一个具有百度文库文档转换功能的工具类
- 实现一个具有百度文库文档转换功能的工具类
- 实现一个具有百度文库文档转换功能的工具类
- 实现一个具有百度文库文档转换功能的工具类
- 实现一个具有百度文库文档转换功能的工具类
- 实现一个具有百度文库文档转换功能的工具类
- 实现一个具有百度文库文档转换功能的工具类
- 实现一个具有百度文库文档转换功能的工具类
- 实现百度文库功能的工具类
- 实现类似百度文库的文档在线阅读功能
- 文档在线阅读的实现(类百度文库)
- 原创利用flashpaper实现百度文库、豆丁网文档预览功能
- 原创利用flashpaper实现百度文库、豆丁网文档预览功能
- PHP 仿百度文库,实现Word文档在线预览功能
- flashpaper实现百度文库、豆丁网文档预览功能 - 坑
- 百度文库和豆丁网的在线文档阅读功能
- java实现百度文库功能
- java实现百度文库功能 .
- 关于document.cookie的使用
- OS_CRITICAL_METHOD的意思
- 学习嵌入式所具备的条件
- Easyui EasyLoader(加载器)用法
- 用C++实现多线程Mutex锁(Win32)
- 实现一个具有百度文库文档转换功能的工具类
- Java File常见类小计
- 工作的事情
- 嵌入式linux与arm开发
- java一次性查询几十万,几百万数据解决办法
- oracle使用rownum做分页查询
- linux ADSL 安装
- Socket编程入门---TCP篇
- LINUX多线程互斥量和读写锁区别