- 浏览: 601165 次
- 性别:
- 来自: 广州
-
文章分类
- 全部博客 (365)
- Tomcat调优 (2)
- Apache Http (20)
- Webserver安装 (5)
- Linux安装 (28)
- Linux常用命令 (17)
- C语言及网络编程 (10)
- 文件系统 (0)
- Lucene (12)
- Hadoop (9)
- FastDFS (8)
- 报表 (0)
- 性能测试 (1)
- JAVA (18)
- CSharp (3)
- C++ (38)
- BI (0)
- 数据挖掘 (0)
- 数据采集 (0)
- 网址收集整理 (3)
- Resin (0)
- JBoss (0)
- nginx (0)
- 数据结构 (1)
- 随记 (5)
- Katta (1)
- Shell (6)
- webservice (0)
- JBPM (2)
- JQuery (6)
- Flex (41)
- SSH (0)
- javascript (7)
- php (13)
- 数据库 (6)
- 搜索引擎排序 (2)
- LVS (3)
- solr (2)
- windows (1)
- mysql (3)
- 营销软件 (1)
- tfs (1)
- memcache (5)
- 分布式搜索 (3)
- 关注的博客 (1)
- Android (2)
- clucene (11)
- 综合 (1)
- c c++ 多线程 (6)
- Linux (1)
- 注册码 (1)
- 文件类型转换 (3)
- Linux 与 asp.net (2)
- perl (5)
- coreseek (1)
- 阅读器 (2)
- SEO (1)
- 励志 (1)
- 在线性能测试工具 (1)
- yii (7)
- 服务器监控 (1)
- 广告 (1)
- 代理服务 (5)
- zookeeper (8)
- 广告联盟 (0)
- 常用软件下载 (1)
- 架设自已的站点心得 (0)
最新评论
-
terry07:
java 7 用这个就可以了 Desktop desktop ...
关于java Runtime.getRunTime.exec(String command)的使用 -
HSINKING:
怎么设置打开的dos 窗口是指定的路径下
关于java调用bat文件,不打开窗口 -
liubang201010:
hyperic hq更多参考资料,请访问:http://www ...
hyperic-hq -
^=^:
STDIN_FILENO是unistd.h中定义的一个numb ...
深入理解dup和dup2的用法 -
antor:
留个记号,学习了
[转]用java流方式判断文件类型
2010-07-30 11:01 此页由Linux Wiki用户Chenxing于2008年3月11日 (星期二) 10:45的最后更改。 出自Linux Wiki 在Linux中常常涉及到多种文档格式,如doc、txt、html、rtf等等。为了方便文件传递,就可能需要在各种格式之间进行转换。 OpenOffice.Org可以方便的打开多各文档格式并按需进行转换,但要一次处理大量文件或要编写脚本进行转换时,仅用OpenOffice就难以胜任了。 unoconv利用OpenOffice.Org的UNO接口实现了命令行文档格式转换功能,支持所有OpenOffice支持的文件格式,为文档的批量转换提供了可能。 目录 [隐藏] •1 下载及安装 •2 使用方法 ◦2.1 单个文件转换 ◦2.2 批量转换 •3 其它功能简介 ◦3.1 支持的文件格式 ◦3.2 服务器/客户端模式 •4 附:支持的输出文件格式 •5 相关资料 下载及安装 目前Debian的源已经有unoconv,如果你使用的发行版还没有unoconv,可以到它的主页下载: http://dag.wieers.com/home-made/unoconv/ 。 注意: 该软件依赖于OpenOffice.Org。 使用方法 单个文件转换 举一个例子:要转换一个doc文件为PDF格式,只需在命令中执行: $ unoconv -f pdf myDoc.doc 即会生成一个名为myDoc.pdf的pdf文件。 批量转换 批量转换需要结合find命令或脚本使用,使用时需注意最好使用C/S模式以加快速度: unoconv --listener & unoconv -f pdf some-document.odt unoconv -f doc other-document.odt unoconv -f jpg some-image.png unoconv -f xsl some-spreadsheet.csv 其它功能简介 支持的文件格式 可以执行unoconv --show查看截止到2008年3月11日,unoconv支持以下几种导出格式(见本文末尾)[1] 服务器/客户端模式 此模式使文档的集中处理成为可能。 附:支持的输出文件格式 bib - BibTeX [.bib] doc - Microsoft Word 97/2000/XP [.doc] doc6 - Microsoft Word 6.0 [.doc] doc95 - Microsoft Word 95 [.doc] docbook - DocBook [.xml] html - HTML Document (OpenOffice.org Writer) [.html] odt - Open Document Text [.odt] ott - Open Document Text [.ott] ooxml - Microsoft Office Open XML [.xml] pdb - AportisDoc (Palm) [.pdb] pdf - Portable Document Format [.pdf] psw - Pocket Word [.psw] rtf - Rich Text Format [.rtf] latex - LaTeX 2e [.ltx] sdw - StarWriter 5.0 [.sdw] sdw4 - StarWriter 4.0 [.sdw] sdw3 - StarWriter 3.0 [.sdw] stw - Open Office.org 1.0 Text Document Template [.stw] sxw - Open Office.org 1.0 Text Document [.sxw] text - Text Encoded [.txt] txt - Plain Text [.txt] vor - StarWriter 5.0 Template [.vor] vor4 - StarWriter 4.0 Template [.vor] vor3 - StarWriter 3.0 Template [.vor] xhtml - XHTML Document [.html] 图形格式: bmp - Windows Bitmap [.bmp] emf - Enhanced Metafile [.emf] eps - Encapsulated PostScript [.eps] gif - Graphics Interchange Format [.gif] html - HTML Document (OpenOffice.org Draw) [.html] jpg - Joint Photographic Experts Group [.jpg] met - OS/2 Metafile [.met] odd - OpenDocument Drawing [.odd] otg - OpenDocument Drawing Template [.otg] pbm - Portable Bitmap [.pbm] pct - Mac Pict [.pct] pdf - Portable Document Format [.pdf] pgm - Portable Graymap [.pgm] png - Portable Network Graphic [.png] ppm - Portable Pixelmap [.ppm] ras - Sun Raster Image [.ras] std - OpenOffice.org 1.0 Drawing Template [.std] svg - Scalable Vector Graphics [.svg] svm - StarView Metafile [.svm] swf - Macromedia Flash (SWF) [.swf] sxd - OpenOffice.org 1.0 Drawing [.sxd] sxd3 - StarDraw 3.0 [.sxd] sxd5 - StarDraw 5.0 [.sxd] tiff - Tagged Image File Format [.tiff] vor - StarDraw 5.0 Template [.vor] vor3 - StarDraw 3.0 Template [.vor] wmf - Windows Metafile [.wmf] xhtml - XHTML [.xhtml] xpm - X PixMap [.xpm] 演示文稿: bmp - Windows Bitmap [.bmp] emf - Enhanced Metafile [.emf] eps - Encapsulated PostScript [.eps] gif - Graphics Interchange Format [.gif] html - HTML Document (OpenOffice.org Impress) [.html] jpg - Joint Photographic Experts Group [.jpg] met - OS/2 Metafile [.met] odd - OpenDocument Drawing (Impress) [.odd] odg - OpenOffice.org 1.0 Drawing (OpenOffice.org Impress) [.odg] odp - OpenDocument Presentation [.odp] otp - OpenDocument Presentation Template [.otp] pbm - Portable Bitmap [.pbm] pct - Mac Pict [.pct] pdf - Portable Document Format [.pdf] pgm - Portable Graymap [.pgm] png - Portable Network Graphic [.png] pot - Microsoft PowerPoint 97/2000/XP Template [.pot] ppm - Portable Pixelmap [.ppm] ppt - Microsoft PowerPoint 97/2000/XP [.ppt] pwp - PlaceWare [.pwp] ras - Sun Raster Image [.ras] sda - StarDraw 5.0 (OpenOffice.org Impress) [.sda] sdd - StarImpress 5.0 [.sdd] sdd3 - StarDraw 3.0 (OpenOffice.org Impress) [.sdd] sdd4 - StarImpress 4.0 [.sdd] sti - OpenOffice.org 1.0 Presentation Template [.sti] stp - OpenDocument Presentation Template [.stp] svg - Scalable Vector Graphics [.svg] svm - StarView Metafile [.svm] swf - Macromedia Flash (SWF) [.swf] sxi - OpenOffice.org 1.0 Presentation [.sxi] tiff - Tagged Image File Format [.tiff] vor - StarImpress 5.0 Template [.vor] vor3 - StarDraw 3.0 Template (OpenOffice.org Impress) [.vor] vor4 - StarImpress 4.0 Template [.vor] vor5 - StarDraw 5.0 Template (OpenOffice.org Impress) [.vor] wmf - Windows Metafile [.wmf] xhtml - XHTML [.xml] xpm - X PixMap [.xpm] 电子表格: csv - Text CSV [.csv] dbf - dBase [.dbf] dif - Data Interchange Format [.dif] html - HTML Document (OpenOffice.org Calc) [.html] ods - Open Document Spreadsheet [.ods] ooxml - Microsoft Excel 2003 XML [.xml] pdf - Portable Document Format [.pdf] pts - OpenDocument Spreadsheet Template [.pts] pxl - Pocket Excel [.pxl] sdc - StarCalc 5.0 [.sdc] sdc4 - StarCalc 4.0 [.sdc] sdc3 - StarCalc 3.0 [.sdc] slk - SYLK [.slk] stc - OpenOffice.org 1.0 Spreadsheet Template [.stc] sxc - OpenOffice.org 1.0 Spreadsheet [.sxc] vor3 - StarCalc 3.0 Template [.vor] vor4 - StarCalc 4.0 Template [.vor] vor - StarCalc 5.0 Template [.vor] xhtml - XHTML [.xhtml] xls - Microsoft Excel 97/2000/XP [.xls] xls5 - Microsoft Excel 5.0 [.xls] xls95 - Microsoft Excel 95 [.xls] xlt - Microsoft Excel 97/2000/XP Template [.xlt] xlt5 - Microsoft Excel 5.0 Template [.xlt] xlt95 - Microsoft Excel 95 Template [.xlt] 相关资料 1.↑ http://dag.wieers.com/home-made/unoconv/ 转自:http://www.linux-wiki.cn/index.php/%E6%96%87%E6%A1%A3%E6%A0%BC%E5%BC%8F%E6%89%B9%E9%87%8F%E8%BD%AC%E6%8D%A2(doc,txt,pdf%E7%AD%89)#.E5.8D.95.E4.B8.AA.E6.96.87.E4.BB.B6.E8.BD.AC.E6.8D.A2 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/antony1029/archive/2010/04/30/5547767.aspx
官方网站: http://www.artofsolving.com/opensource/jodconverter
下载地点:
http://www.artofsolving.com/opensource/jodconverter
http://zh.openoffice.org/new/zh_tw/downloads.html
目前版本: JODConverter v2.2.1, OpenOffice v3.0.0
使用需求: JDK1.4以上, 安装OpenOffice v2.0.3以上
基本简介:
JODConverter主要的功能是用来做各种档案的转换. 目前测试过, Word,Excel,PowerPoint转PDF都是没问题的.
因为JODConverter是透过OpenOffice来做转换, 所以使用前需要先安装OpenOffice, 并且将OpenOffice的Service启动, 才可以使用.
OpenOffice.org具有一个鲜为人知的特性就是其能够作为一个服务来运行,而这种能力具有一定的妙用。举例来说,你可以把openoffice.og变成一个转换引擎,利用这种转换引擎你可以通过网络接口或命令行工具对文件的格式进行转换,JODConverter可以帮助你实现OpenOffice.org的这种文件转换功能。
为了将OpenOffice.org作为一个转换引擎,你必须以服务的方式将它启动,使它在某个特定的端口监听连接,在Linux平台你可以用如下的命令启动openoffice.org:
soffice -headless -accept=”socket,port=8100;urp;”(我在linux下使用soffice -headless -accept=”socket,host=127.0.0.1,port=8100;urp;”,open office server是开启来了,但是文件转换不成功,异常是连接失败,这个很可以是你用jodconverter来转换时使用的是localhost,而当你的机有host配置文件里没有将localhost与127.0.0.1对应起来时,就无法解析了,这里可以修改host文件或去掉host=127.0.0.1,这样我试过可以成功)
在Windows平台, 使用如下命令:
“C:\Program Files\OpenOffice.org 2.2\program\soffice” -accept=”socket,port=8100;urp;”
使用教学:
Step1: 安装OpenOffice
Step2: 启动OpenOffice Service
1 cd C:\Program Files\OpenOffice.org 3\program
2 soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
Step3:将JODConverter的Jar档放进专案中的Library, 请检查你的专案是否包含以下的Jar档:
jodconverter-2.2.1.jar
jurt-2.3.0.jar
xstream-1.2.2.jar
ridl-2.3.0.jar
commons-io-1.3.1.jar
juh-2.3.0.jar
slf4j-api-1.4.3.jar
unoil-2.3.0.jar
slf4j-jdk14-1.4.3.jar
Step4: 准备一个word档放在c:/document.rtf
Step5: 执行以下程式
Java代码 01.<span style="font-size: medium;">import java.io.File;
02.
03. import com.artofsolving.jodconverter.DocumentConverter;
04.
05. import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
06.
07. import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
08.
09. import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
10.
11. public class JodDemo {
12.
13. public static void main(String[] args) throws Exception{
14.
15. File inputFile = new File("c:/document.rtf");
16.
17. File outputFile = new File("c:/document.doc");
18.
19. // connect to an OpenOffice.org instance running on port 8100
20.
21. OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
22.
23. connection.connect();
24.
25. // convert
26.
27. DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
28.
29. converter.convert(inputFile, outputFile);
30.
31. // close the connection
32.
33. connection.disconnect();
34.
35. }
36.
37. } </span>
程式说明:
程式的部份相当简洁, 特别要注意的地方是第12行连线的port必须与你启动OpenOffice的Port相同,
另外JODConverter预设是用副档名作文件种类的判断, 所以副档名必须要正确才行.
如果副档名比较特别的话, 就必须在convert()的时候强制指定Document Type.
心得:
JODConverter使用起来相当方便, 官网也提供War档让JODConverter变成Web Service提供给不同的语言来呼叫.
特别要注意的是, OpenOffice Service并不是ThreadSafe的, 多个Web AP在使用的时候必须要注意.
那我也來補充一些好了
之前也在試這個檔案轉換的程式
程式最好加上 try-catch
因為之前发現有些檔案 format 不能转,发生 Exception 后,connection 不會自动切断,程序会hand 住
所以改成如下方式:
Java代码 01.<span style="font-size: medium;">public void convert(String input, String output){
02. File inputFile = new File(input);
03. File outputFile = new File(output);
04. OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
05. try {
06. connection.connect();
07. DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
08. converter.convert(inputFile, outputFile);
09. } catch(Exception e) {
10. e.printStackTrace();
11. } finally {
12. try{ if(connection != null){connection.disconnect(); connection = null;}}catch(Exception e){}
13. }
14. } </span>
再來,明明就是 open office 的檔案,卻生不能轉換的問題。例如:*.STW, *.SXD, *.ODF 等,後來才知道可以自行指定來源檔和輸出檔的 mime-type,程式如下:
Java代码 01.<span style="font-size: medium;">public void convertSTW(String input, String output){
02. DocumentFormat stw = new DocumentFormat("OpenOffice.org 1.0 Template", DocumentFamily.TEXT, "application/vnd.sun.xml.writer", "stw");
03. DefaultDocumentFormatRegistry formatReg = new DefaultDocumentFormatRegistry();
04. DocumentFormat pdf = formatReg.getFormatByFileExtension("pdf");
05. File inputFile = new File(input);
06. File outputFile = new File(output);
07. OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
08. try {
09. connection.connect();
10. DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
11. converter.convert(inputFile, stw, outputFile, pdf);
12. } catch(Exception e) {
13. e.printStackTrace();
14. } finally {
15. try{ if(connection != null){connection.disconnect(); connection = null;}}catch(Exception e){}
16. }
17. } </span>
上面的程式是轉換 STW 到 PDF,如果是 SXD / ODF 則只需要變更 DocumentFormat 的內容即可。
Java代码 01.<span style="font-size: medium;">DocumentFormat sxd = new DocumentFormat("OpenOffice.org 1.0 Drawing", DocumentFamily.DRAWING, "application/vnd.sun.xml.sraw", "sxd");
02.
03.DocumentFormat odf = new DocumentFormat("OpenDocument Math", DocumentFamily.TEXT, "application/vnd.oasis.opendocument.formula", "odf"); </span>
所有 default support 的 DocumentFormat 都在 com.artofsolving.jodconverter.DefaultDocumentFormatRegistry 裡,但並非所有 open office 支援的 file format 都有,所以要像上面的方法自行去定义 DocumentFormat,至于它里面的参数可以从jodconverter-2.2.2.jar包的com.artofsolving.jodconverter包下的document-formats.xml文件里面得到,这样就可以完成多种格式的转换,如open office,ms office , wps office及所有的纯文本文件。
分享到: 上一篇:如何写一个RTF阅读器
下一篇:InputStream,OutputStream,byte[]转化
发表评论
-
swf文件压缩
2011-12-01 18:29 1424from http://www.9ria.com/news/2 ... -
swf文件的数据结构以及转为exe或从exe中剥离出swf的代码
2011-11-14 22:11 2180swf文件的数据结构以及转为exe或从exe中剥离出swf的源 ... -
swf的详细介绍
2011-11-14 21:17 1296介绍链接一 http://as3.iteye.com/blog ... -
三个重要的处理swf的开源软件
2011-11-14 18:33 1375抓紧时间研究jswiff,swfmill,swfml jsw ... -
IText中文处理问题!!!
2011-11-08 16:19 1222引用2008年06月05日 星期四 下午 10:29这里说的I ... -
FlexBook
2011-05-06 13:12 1610FlexBook from [url ... -
[2011-04] Flex里自定义进度条ProgressBar样式皮肤
2011-04-20 15:47 27202011-01-29 11:08trackbar是整个的条 ... -
[2011-04] flex弹出模式窗口
2011-04-19 17:25 16781、创建一个component,继承自 TitleWindow ... -
[2011-04] Flex调用C# Webservice
2011-04-19 14:19 1208关键字: flex数据交互 终于试出flex用WebSer ... -
[2011-04-19]Flex程序实现背景贴图的两种方式
2011-04-19 10:33 1329Flex程序实现背景贴图的两种方式 Two ways to i ... -
Flex Component Kit for Flash CS3 安装方法及前提
2011-04-15 10:22 1350from http://kingapex.iteye.com/ ... -
as3 flash web 应用 (6)swfobject的使用:将flash嵌入页面
2011-04-14 10:27 2513from http://hi.baidu.com/yukon_ ... -
pdftk 为偶数页加水印
2011-03-23 17:27 2302引用查看完整版本 : 找 ... -
借助 unoconv 批量转 xls 到 pdf文件
2011-03-23 14:44 3384文章分类:Web前端 因为 ... -
swftools安装
2011-03-09 08:44 1527安装swftools工具时,在windows平台下是很简单的事 ... -
flex3下使用全屏模式
2011-03-03 10:12 1051引用flex3下使用全屏模式 MXML: -------- ... -
Flex生成SDK下Local目录下的语言包
2011-02-25 16:44 1709from http://sensaran.wordpress. ... -
swf文件格式解析入门(tag解析)
2011-02-24 15:24 2898收藏 swf文件格式解析入门(tag解析) 2010 ... -
转成swf文件注意事项
2011-02-21 10:23 9581\ 这里同时给大家提供一个建议就是,对于纯位图的资源文 ... -
AS获取SWF文件的宽和高!(实现如下类)
2011-02-17 17:57 1487主页博客相册|个人档案 ...
相关推荐
这个工具对于处理文档转换需求非常有用,特别是当你需要批量转换大量文件时。下面我们将深入探讨 Unoconv 的工作原理、安装、使用方法以及一些常见应用场景。 1. **Unoconv 工作原理**: Unoconv 利用了 ...
unoconv 是一个开源命令行工具,它利用 LibreOffice 或 OpenOffice 的能力来转换各种文档格式,如 .doc、.xls、.ppt 等,支持多种格式之间的互相转换。它特别适用于自动化脚本和批量转换任务。 首先,我们需要理解 ...
如Pandoc、unoconv等开源工具提供跨平台的文件格式转换,它们支持多种格式,并且可以集成到自动化工作流中。 总的来说,文件格式转换是一项涉及多方面技术的任务,从简单的文本解析到复杂的布局和图像处理。理解...
Unoconv支持多种输入格式,如.doc、.docx、.xls、.xlsx等,以及多种输出格式,如.pdf、.odt、.txt等。由于转换过程是通过实际的文档处理软件执行的,所以 Unoconv 能够保持文档的原始格式和布局。 现在,让我们关注...
除了Word转PDF,OpenOffice还支持多种其他格式之间的转换,例如ODT(OpenOffice文档)转DOC,ODS(OpenOffice电子表格)转XLS,以及ODP(OpenOffice演示文稿)转PPT等。这些转换同样基于OpenOffice的服务功能,只...
`unoconv`是基于OpenOffice/LibreOffice的一个工具,它能将多种文档格式互相转换,包括Word到PDF。通过Java的Runtime.exec()方法,你可以调用这个命令并传递Word文件和期望的PDF输出路径作为参数。 7. **安全性和...
在IT行业中,转换文档格式是一项常见的任务,尤其在处理Word文档和PDF文件时。Word文档以其编辑友好性而被广泛使用,而PDF则因其稳定性和跨平台一致性受到青睐。本教程将详细介绍如何在Windows和Linux操作系统中进行...