- 浏览: 572523 次
- 性别:
- 来自: 广州
文章分类
- 全部博客 (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流方式判断文件类型
引用
文章分类:Java编程
之前找了一种方式是通过jacob调用adobe PDF虚拟打印机生成PDF,但多人同时调用时有同步问题;在同事的提醒下找了下面的实现方式:
1、安装OpenOffice 3:下载路径:http://zh.openoffice.org/new/zh_cn/downloads.html
2、用以下命令启动OpenOffice服务
cd C:\Program Files\OpenOffice.org 3\program
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
3、附上我写的DEMO,第三方包: JODConverter v2.2.1
JODConverterDoc2PDF.rar (1.7 MB)
官方網站:
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啟動, 才可以使用.
使用教學:
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.doc
Step5: 執行以下程式
import java.io.File;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
public class JodDemo {
public static void main(String[] args) throws Exception{
File inputFile = new File("c:/document.doc");
File outputFile = new File("c:/document.pdf");
// connect to an OpenOffice.org instance running on port 8100
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
connection.connect();
// convert
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(inputFile, outputFile);
// close the connection
connection.disconnect();
}
}
程式說明:
程式的部份相當簡潔, 特別要注意的地方是第12行連線的port必須與你啟動OpenOffice的Port相同,
另外JODConverter預設是用副檔名作文件種類的判斷, 所以副檔名必須要正確才行.
如果副檔名比較特別的話, 就必須在convert()的時候強制指定Document Type.
心得:
JODConverter使用起來相當方便, 官網也提供War檔讓JODConverter變成Web Service提供給不同的語言來呼叫.
特別要注意的是, OpenOffice Service並不是ThreadSafe的, 多個Web AP在使用的時候必須要注意.
參考資料:
http://www.artofsolving.com/opensource/jodconverter
那我也來補充一些好了
之前也在試這個檔案轉換的程式
程式最好加上 try-catch
因為之前發現有些檔案 format 不能轉,發生 Exception 後,connection 不會自動切斷,程式會 hand 住
所以改成如下方式:
1234567891011121314
public void convert(String input, String output){
File inputFile = new File(input);
File outputFile = new File(output);
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
try {
connection.connect();
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(inputFile, outputFile);
} catch(Exception e) {
e.printStackTrace();
} finally {
try{ if(connection != null){connection.disconnect(); connection = null;}}catch(Exception e){}
}
}
再來,明明就是 open office 的檔案,卻生不能轉換的問題。例如:*.STW, *.SXD, *.ODF 等,後來才知道可以自行指定來源檔和輸出檔的 mime-type,程式如下:
1234567891011121314151617
public void convertSTW(String input, String output){
DocumentFormat stw = new DocumentFormat("OpenOffice.org 1.0 Template", DocumentFamily.TEXT, "application/vnd.sun.xml.writer", "stw");
DefaultDocumentFormatRegistry formatReg = new DefaultDocumentFormatRegistry();
DocumentFormat pdf = formatReg.getFormatByFileExtension("pdf");
File inputFile = new File(input);
File outputFile = new File(output);
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
try {
connection.connect();
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(inputFile, stw, outputFile, pdf);
} catch(Exception e) {
e.printStackTrace();
} finally {
try{ if(connection != null){connection.disconnect(); connection = null;}}catch(Exception e){}
}
}
上面的程式是轉換 STW 到 PDF,如果是 SXD / ODF 則只需要變更 DocumentFormat 的內容即可。
123
DocumentFormat sxd = new DocumentFormat("OpenOffice.org 1.0 Drawing", DocumentFamily.DRAWING, "application/vnd.sun.xml.sraw", "sxd");
DocumentFormat odf = new DocumentFormat("OpenDocument Math", DocumentFamily.TEXT, "application/vnd.oasis.opendocument.formula", "odf");
所有 default support 的 DocumentFormat 都在 com.artofsolving.jodconverter.DefaultDocumentFormatRegistry 裡,但並非所有 open office 支援的 file format 都有,所以要像上面的方法自行去定義 DocumentFormat。
在此獻給所有需要作 File Convert 的人試試。
免錢的,最好用。還有 source code 可以自己改。
Java读取word文档解决方案 | JAVA读取word文件 15:01浏览 (2428)评论 (5)分类: POI收藏相关推荐评论
5 楼 ainoke 2010-12-16 引用
非常感谢...
4 楼 javatim 2010-11-04 引用
兄弟,谢谢啦,我也在研究这个东东……
3 楼 zhangrong_rongzhang2 2010-08-31 引用
不过我想知道怎么让openOffice的服务做到开机启动
2 楼 zhangrong_rongzhang2 2010-08-31 引用
按照你这种方式,我的可以实现转pdf了。谢谢
1 楼 guoxhvip 2010-05-06 引用
请问这位朋友 为什么我照你的步骤执行 程序却一直卡住不动了
2010-5-6 16:08:33 com.artofsolving.jodconverter.openoffice.connection.AbstractOpenOfficeConnection connect
信息: connected
请问是哪个地方出现问题了?
之前找了一种方式是通过jacob调用adobe PDF虚拟打印机生成PDF,但多人同时调用时有同步问题;在同事的提醒下找了下面的实现方式:
1、安装OpenOffice 3:下载路径:http://zh.openoffice.org/new/zh_cn/downloads.html
2、用以下命令启动OpenOffice服务
cd C:\Program Files\OpenOffice.org 3\program
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
3、附上我写的DEMO,第三方包: JODConverter v2.2.1
JODConverterDoc2PDF.rar (1.7 MB)
官方網站:
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啟動, 才可以使用.
使用教學:
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.doc
Step5: 執行以下程式
import java.io.File;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
public class JodDemo {
public static void main(String[] args) throws Exception{
File inputFile = new File("c:/document.doc");
File outputFile = new File("c:/document.pdf");
// connect to an OpenOffice.org instance running on port 8100
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
connection.connect();
// convert
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(inputFile, outputFile);
// close the connection
connection.disconnect();
}
}
程式說明:
程式的部份相當簡潔, 特別要注意的地方是第12行連線的port必須與你啟動OpenOffice的Port相同,
另外JODConverter預設是用副檔名作文件種類的判斷, 所以副檔名必須要正確才行.
如果副檔名比較特別的話, 就必須在convert()的時候強制指定Document Type.
心得:
JODConverter使用起來相當方便, 官網也提供War檔讓JODConverter變成Web Service提供給不同的語言來呼叫.
特別要注意的是, OpenOffice Service並不是ThreadSafe的, 多個Web AP在使用的時候必須要注意.
參考資料:
http://www.artofsolving.com/opensource/jodconverter
那我也來補充一些好了
之前也在試這個檔案轉換的程式
程式最好加上 try-catch
因為之前發現有些檔案 format 不能轉,發生 Exception 後,connection 不會自動切斷,程式會 hand 住
所以改成如下方式:
1234567891011121314
public void convert(String input, String output){
File inputFile = new File(input);
File outputFile = new File(output);
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
try {
connection.connect();
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(inputFile, outputFile);
} catch(Exception e) {
e.printStackTrace();
} finally {
try{ if(connection != null){connection.disconnect(); connection = null;}}catch(Exception e){}
}
}
再來,明明就是 open office 的檔案,卻生不能轉換的問題。例如:*.STW, *.SXD, *.ODF 等,後來才知道可以自行指定來源檔和輸出檔的 mime-type,程式如下:
1234567891011121314151617
public void convertSTW(String input, String output){
DocumentFormat stw = new DocumentFormat("OpenOffice.org 1.0 Template", DocumentFamily.TEXT, "application/vnd.sun.xml.writer", "stw");
DefaultDocumentFormatRegistry formatReg = new DefaultDocumentFormatRegistry();
DocumentFormat pdf = formatReg.getFormatByFileExtension("pdf");
File inputFile = new File(input);
File outputFile = new File(output);
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
try {
connection.connect();
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(inputFile, stw, outputFile, pdf);
} catch(Exception e) {
e.printStackTrace();
} finally {
try{ if(connection != null){connection.disconnect(); connection = null;}}catch(Exception e){}
}
}
上面的程式是轉換 STW 到 PDF,如果是 SXD / ODF 則只需要變更 DocumentFormat 的內容即可。
123
DocumentFormat sxd = new DocumentFormat("OpenOffice.org 1.0 Drawing", DocumentFamily.DRAWING, "application/vnd.sun.xml.sraw", "sxd");
DocumentFormat odf = new DocumentFormat("OpenDocument Math", DocumentFamily.TEXT, "application/vnd.oasis.opendocument.formula", "odf");
所有 default support 的 DocumentFormat 都在 com.artofsolving.jodconverter.DefaultDocumentFormatRegistry 裡,但並非所有 open office 支援的 file format 都有,所以要像上面的方法自行去定義 DocumentFormat。
在此獻給所有需要作 File Convert 的人試試。
免錢的,最好用。還有 source code 可以自己改。
Java读取word文档解决方案 | JAVA读取word文件 15:01浏览 (2428)评论 (5)分类: POI收藏相关推荐评论
5 楼 ainoke 2010-12-16 引用
非常感谢...
4 楼 javatim 2010-11-04 引用
兄弟,谢谢啦,我也在研究这个东东……
3 楼 zhangrong_rongzhang2 2010-08-31 引用
不过我想知道怎么让openOffice的服务做到开机启动
2 楼 zhangrong_rongzhang2 2010-08-31 引用
按照你这种方式,我的可以实现转pdf了。谢谢
1 楼 guoxhvip 2010-05-06 引用
请问这位朋友 为什么我照你的步骤执行 程序却一直卡住不动了
2010-5-6 16:08:33 com.artofsolving.jodconverter.openoffice.connection.AbstractOpenOfficeConnection connect
信息: connected
请问是哪个地方出现问题了?
发表评论
-
通过JVM获取相关的服务器信息 .
2012-02-02 14:24 1126分类: j2ee 2009-05-12 16:12 1034人 ... -
JVM调优总结 -Xms -Xmx -Xmn -Xss
2011-11-10 09:15 7382009-03-05 JVM调优总结 -Xms -Xmx - ... -
关于java Runtime.getRunTime.exec(String command)的使用
2011-10-19 19:31 90652008-09-26 19:44当要调用一个外部程序的时候,j ... -
关于java调用bat文件,不打开窗口
2011-10-19 19:31 2129Runtime.getRuntime().exec(" ... -
Runtime.getRuntime().exec(cmd) cd
2011-10-19 18:49 2826BashLinux.如果要在java程序里执行一条linux可 ... -
11款用于优化、分析源代码的Java工具
2011-08-03 09:16 599from http://java.csdn.net/a/201 ... -
用java实现html转pdf
2011-02-28 12:58 6617import java.io.File; import ja ... -
[转]用java流方式判断文件类型
2011-02-28 11:46 2436文章分类:Java编程 今天在群里面看有人贴的一个帖子,觉 ... -
jodconverter纯文本文件转为pdf时中文问题解决方案
2011-02-28 11:28 2010文章分类:Java编程 jodconverter转换ms文 ... -
老紫竹JAVA提高教程-信号量(Semaphore)在生产者和消费者模式的使用
2011-02-14 17:07 2039Semaphore 信号量,就是一个允许实现设置好的令牌。也许 ... -
北理工Java技术与应用考试试题参考答案及点评(下)
2011-01-24 12:12 961from :http://blog.csdn.net/bitf ... -
北理工Java技术与应用考试试题参考答案及点评(上)
2011-01-24 12:11 1254from http://blog.csdn.net/bitfa ... -
自测一下你的Java掌握得怎么样
2011-01-24 12:10 822引用自测一下你的Java掌握得怎么样? ========= ... -
Java执行脚本代码分析
2011-01-21 16:46 1249Java, 执行脚本 1、可用的脚本引擎 Java 6 ... -
java开发守护进程
2011-01-11 13:29 1181其实就是想开发个Windows下系统服务一样的程序。而查了好久 ... -
JDK5.0垃圾收集优化
2010-09-08 10:37 684JDK5.0垃圾收集优化(转 ... -
volatile 变量使用指南
2010-06-10 10:40 729Java 理论与实践: 正确使 ...
相关推荐
NULL 博文链接:https://titanseason.iteye.com/blog/1471606
本例通过VB脚本使用openoffice将word转成PDF
asp.net利用OpenOffice转换WORD/EXCEL/PDF为PDF,加水印源码
java OpenOffice wordExcel转换PDF.zip 带jar包
网站有许多转换pdf的代码.本人也用过office和wps,上传服务器期后出现了许多bug,最后选用openoffice实现最方便。前提服务器或电脑必须下载openoffice.具体配置文件参考:...
openoffice word转pdf excel转pdf ppt转pdf
NULL 博文链接:https://bigbird2012.iteye.com/blog/2078213
利用OpenOffice技术将word、ppt、excel等文本文件转换成pdf。以实现在线预览的功能
Word、Excel等常用文档格式通过OpenOffice转换为pdf及图片,亲测,可用。Word、Excel等常用文档格式通过OpenOffice转换为pdf及图片,亲测,可用。Word、Excel等常用文档格式通过OpenOffice转换为pdf及图片,亲测,可用...
OpenOffice转换Office文档为PDF、HTML,将word,ppt转成html,Excel转为Html
Entry.java这个类的原理是先通过OfficeToPDF.java把文档转成pdf,然后再通过Pdf2Jpg.java这个类把PDF转成JPG或者PNG,想转成什么格式图片在Pdf2Jpg.java这个类当中设置一下,代码经过测试,完全没有问题,如果碰到...
NULL 博文链接:https://zhang-637.iteye.com/blog/1487556
是基于OpenOffice开发的Word转换PDF程序,程序是提供可录入Word文件地址转PDF文件地址方式,将Word转换为PDF文件
C# 将Word格式文件转换为Pdf格式文件源码
使用OpenOffice将文档转换为PDF,将PDF转换为Swf
支持将 .doc|.docx|.xls|.xlsx|.ppt|.pptx文件 转化为pdf文件 linux安装包: Apache_OpenOffice_4.1.2_Linux_x86_install-rpm_zh-CN.tar.gz Apache_OpenOffice_4.1.7_Linux_x86-64_install-rpm_zh-CN.tar.gz ...
基于openoffice实现的将word文件转换为PDF文件。 DocConverter.java是代码实现 controller.txt中写了简单的使用方法 所需jar包中提供了一些必须导入的jar包。 仅提供了核心部分代码,因为是从项目中提取出来的,测试...
网络上有很多种Word转PDF的方式(openoffice,jacob,POI),但都不是很完美,要不依赖三防包还要下载dll文件,要不转换不完美,还不支持中文,我的转换方式使用eclipse自带的swt包即可,程序引用swt的jar后直接运行
1、需要用到组建在转PDF,安装OpenOffice 3:下载路径:http://www.openoffice.org/download/...我实现的是读取access数据库中对应WORD文件名然后到指定文件夹查找并转换,你也可以到指定文件夹中去遍历所有WORD文件转换
Java通过OpenOffice将word、ppt、excel、图片、txt文档转换成PDF-附件资源