模仿百度文库代码没试过。



java开发_模仿百度文库_OpenOffice2PDF_源码下载

这几天在研究模仿着做类似于百度文库的东西,在这里给大家分享一下我自己做的东西。

由于需要做这样的项目,我查阅了很多资料,最后选定一下方案去做:

Txt/Word/Excel/PPT=>PDF(OpenOffice+JodConverter)=>SWF(pdf2swf)=>FlexPaper浏览

今天就完成第一步:

Txt/Word/Excel/PPT=>PDF(OpenOffice+JodConverter)

做之前,我们要先做一些准备:

1.下载:Apache_OpenOffice_incubating_3.4.1_Win_x86_install_zh-CN.exe

下载地址http://www.openoffice.org/download/index.html

下载后得到:Apache_OpenOffice_incubating_3.4.1_Win_x86_install_zh-CN.exe

2.安装Apache_OpenOffice

双击Apache_OpenOffice_incubating_3.4.1_Win_x86_install_zh-CN.exe进行安装操作

注意:这里的安装位置,要在项目中用到....我安装在:C:/Program Files (x86)/OpenOffice.org 3目录下面

到这里,OpenOffice就算是安装完成了。

3.创建一个项目

/Office2PDF/src/com/b510/office2pdf/Office2PDF.java

复制代码
  1 /**
  2  * 
  3  */
  4 package com.b510.office2pdf;
  5 
  6 import java.io.File;
  7 import java.util.Date;
  8 import java.util.regex.Pattern;
  9 
 10 import org.artofsolving.jodconverter.OfficeDocumentConverter;
 11 import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
 12 import org.artofsolving.jodconverter.office.OfficeManager;
 13 
 14 /**
 15  * 这是一个工具类,主要是为了使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx)
 16  * 转化为pdf文件<br>
 17  * Office2010的没测试<br>
 18  * 
 19  * @date 2012-11-5
 20  * @author xhw
 21  * 
 22  */
 23 public class Office2PDF {
 24 
 25     /**
 26      * office中.doc格式
 27      */
 28     public static final String OFFICE_DOC = "doc";
 29     /**
 30      * office中.docx格式
 31      */
 32     public static final String OFFICE_DOCX = "docx";
 33     /**
 34      * office中.xls格式
 35      */
 36     public static final String OFFICE_XLS = "xls";
 37     /**
 38      * office中.xlsx格式
 39      */
 40     public static final String OFFICE_XLSX = "xlsx";
 41     /**
 42      * office中.ppt格式
 43      */
 44     public static final String OFFICE_PPT = "ppt";
 45     /**
 46      * office中.pptx格式
 47      */
 48     public static final String OFFICE_PPTX = "pptx";
 49     /**
 50      * pdf格式
 51      */
 52     public static final String OFFICE_TO_PDF = "pdf";
 53 
 54     public static void main(String[] args) {
 55         Office2PDF office2pdf = new Office2PDF();
 56         office2pdf.openOfficeToPDF("e:/test." + OFFICE_DOCX, "e:/test_" + new Date().getTime() + "." + OFFICE_TO_PDF);
 57         office2pdf.openOfficeToPDF("e:/test." + OFFICE_PPTX, null);
 58     }
 59 
 60     /**
 61      * 使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 转化为pdf文件<br>
 62      * 
 63      * @param inputFilePath
 64      *            源文件路径,如:"e:/test.docx"
 65      * @param outputFilePath
 66      *            目标文件路径,如:"e:/test_docx.pdf"
 67      * @return
 68      */
 69     public boolean openOfficeToPDF(String inputFilePath, String outputFilePath) {
 70         return office2pdf(inputFilePath, outputFilePath);
 71     }
 72 
 73     /**
 74      * 根据操作系统的名称,获取OpenOffice.org 3的安装目录<br>
 75      * 如我的OpenOffice.org 3安装在:C:/Program Files (x86)/OpenOffice.org 3<br>
 76      * 
 77      * @return OpenOffice.org 3的安装目录
 78      */
 79     public String getOfficeHome() {
 80         String osName = System.getProperty("os.name");
 81         if (Pattern.matches("Linux.*", osName)) {
 82             return "/opt/openoffice.org3";
 83         } else if (Pattern.matches("Windows.*", osName)) {
 84             return "C:/Program Files (x86)/OpenOffice.org 3";
 85         } else if (Pattern.matches("Mac.*", osName)) {
 86             return "/Application/OpenOffice.org.app/Contents";
 87         }
 88         return null;
 89     }
 90 
 91     /**
 92      * 连接OpenOffice.org 并且启动OpenOffice.org
 93      * 
 94      * @return
 95      */
 96     public OfficeManager getOfficeManager() {
 97         DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();
 98         // 获取OpenOffice.org 3的安装目录
 99         String officeHome = getOfficeHome();
100         config.setOfficeHome(officeHome);
101         // 启动OpenOffice的服务
102         OfficeManager officeManager = config.buildOfficeManager();
103         officeManager.start();
104         return officeManager;
105     }
106 
107     /**
108      * 转换文件
109      * 
110      * @param inputFile
111      * @param outputFilePath_end
112      * @param inputFilePath
113      * @param outputFilePath
114      * @param converter
115      */
116     public void converterFile(File inputFile, String outputFilePath_end, String inputFilePath, String outputFilePath, OfficeDocumentConverter converter) {
117         File outputFile = new File(outputFilePath_end);
118         // 假如目标路径不存在,则新建该路径
119         if (!outputFile.getParentFile().exists()) {
120             outputFile.getParentFile().mkdirs();
121         }
122         converter.convert(inputFile, outputFile);
123         System.out.println("文件:" + inputFilePath + "\n转换为\n目标文件:" + outputFile + "\n成功!");
124     }
125 
126     /**
127      * 使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 转化为pdf文件<br>
128      * 
129      * @param inputFilePath
130      *            源文件路径,如:"e:/test.docx"
131      * @param outputFilePath
132      *            目标文件路径,如:"e:/test_docx.pdf"
133      * @return
134      */
135     public boolean office2pdf(String inputFilePath, String outputFilePath) {
136         boolean flag = false;
137         OfficeManager officeManager = getOfficeManager();
138         // 连接OpenOffice
139         OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
140         long begin_time = new Date().getTime();
141         if (null != inputFilePath) {
142             File inputFile = new File(inputFilePath);
143             // 判断目标文件路径是否为空
144             if (null == outputFilePath) {
145                 // 转换后的文件路径
146                 String outputFilePath_end = getOutputFilePath(inputFilePath);
147                 if (inputFile.exists()) {// 找不到源文件, 则返回
148                     converterFile(inputFile, outputFilePath_end, inputFilePath, outputFilePath, converter);
149                     flag = true;
150                 }
151             } else {
152                 if (inputFile.exists()) {// 找不到源文件, 则返回
153                     converterFile(inputFile, outputFilePath, inputFilePath, outputFilePath, converter);
154                     flag = true;
155                 }
156             }
157             officeManager.stop();
158         } else {
159             System.out.println("con't find the resource");
160         }
161         long end_time = new Date().getTime();
162         System.out.println("文件转换耗时:[" + (end_time - begin_time) + "]ms");
163         return flag;
164     }
165 
166     /**
167      * 获取输出文件
168      * 
169      * @param inputFilePath
170      * @return
171      */
172     public String getOutputFilePath(String inputFilePath) {
173         String outputFilePath = inputFilePath.replaceAll("." + getPostfix(inputFilePath), ".pdf");
174         return outputFilePath;
175     }
176 
177     /**
178      * 获取inputFilePath的后缀名,如:"e:/test.pptx"的后缀名为:"pptx"<br>
179      * 
180      * @param inputFilePath
181      * @return
182      */
183     public String getPostfix(String inputFilePath) {
184         return inputFilePath.substring(inputFilePath.lastIndexOf(".") + 1);
185     }
186 
187 }
复制代码

4.效果

5.控制台效果

复制代码
 1 十一月 05, 2012 5:19:15 下午 org.artofsolving.jodconverter.office.ProcessPoolOfficeManager <init>
 2 INFO: ProcessManager implementation is PureJavaProcessManager
 3 十一月 05, 2012 5:19:15 下午 org.artofsolving.jodconverter.office.OfficeProcess start
 4 INFO: starting process with acceptString 'socket,host=127.0.0.1,port=2002,tcpNoDelay=1' and profileDir 'C:\Users\ADMINI~1\AppData\Local\Temp\.jodconverter_socket_host-127.0.0.1_port-2002'
 5 十一月 05, 2012 5:19:15 下午 org.artofsolving.jodconverter.office.OfficeProcess start
 6 INFO: started process
 7 十一月 05, 2012 5:19:16 下午 org.artofsolving.jodconverter.office.OfficeConnection connect
 8 INFO: connected: 'socket,host=127.0.0.1,port=2002,tcpNoDelay=1'
 9 十一月 05, 2012 5:19:19 下午 org.artofsolving.jodconverter.office.ProcessPoolOfficeManager stop
10 INFO: stopping
11 文件:e:/test.docx
12 转换为
13 目标文件:e:\test_1352107155307.pdf
14 成功!
15 十一月 05, 2012 5:19:19 下午 org.artofsolving.jodconverter.office.OfficeConnection$1 disposing
16 INFO: disconnected: 'socket,host=127.0.0.1,port=2002,tcpNoDelay=1'
17 十一月 05, 2012 5:19:19 下午 org.artofsolving.jodconverter.office.ManagedOfficeProcess doEnsureProcessExited
18 INFO: process exited with code 0
19 十一月 05, 2012 5:19:19 下午 org.artofsolving.jodconverter.office.ProcessPoolOfficeManager stop
20 INFO: stopped
21 文件转换耗时:[2838]ms
22 十一月 05, 2012 5:19:19 下午 org.artofsolving.jodconverter.office.ProcessPoolOfficeManager <init>
23 INFO: ProcessManager implementation is PureJavaProcessManager
24 十一月 05, 2012 5:19:19 下午 org.artofsolving.jodconverter.office.OfficeProcess start
25 INFO: starting process with acceptString 'socket,host=127.0.0.1,port=2002,tcpNoDelay=1' and profileDir 'C:\Users\ADMINI~1\AppData\Local\Temp\.jodconverter_socket_host-127.0.0.1_port-2002'
26 十一月 05, 2012 5:19:19 下午 org.artofsolving.jodconverter.office.OfficeProcess start
27 INFO: started process
28 十一月 05, 2012 5:19:20 下午 org.artofsolving.jodconverter.office.OfficeConnection connect
29 INFO: connected: 'socket,host=127.0.0.1,port=2002,tcpNoDelay=1'
30 文件:e:/test.pptx
31 转换为
32 目标文件:e:\test.pdf
33 成功!
34 十一月 05, 2012 5:19:26 下午 org.artofsolving.jodconverter.office.ProcessPoolOfficeManager stop
35 INFO: stopping
36 十一月 05, 2012 5:19:26 下午 org.artofsolving.jodconverter.office.OfficeConnection$1 disposing
37 INFO: disconnected: 'socket,host=127.0.0.1,port=2002,tcpNoDelay=1'
38 十一月 05, 2012 5:19:26 下午 org.artofsolving.jodconverter.office.ManagedOfficeProcess doEnsureProcessExited
39 INFO: process exited with code 0
40 十一月 05, 2012 5:19:26 下午 org.artofsolving.jodconverter.office.ProcessPoolOfficeManager stop
41 INFO: stopped
42 文件转换耗时:[6417]ms
复制代码

是不是很简单....

如果你想尝试一下,这里提供源码下载:http://files.cnblogs.com/hongten/Office2PDF.rar

希望大家多多交流,一起进步...

下面是注意事项:java开发_模仿百度文库_OpenOffice2PDF_注意事项

2
0
(请您对文章做出评价)
« 上一篇: 网页上播放视频的免费的播放器_CKPlayer
» 下一篇: 实习工作两月
posted @ 2012-11-05 17:44 Hongten 阅读( 4820) 评论( 32) 编辑 收藏
  
#1楼 2012-11-27 23:31 小子欠扁  
我也做了一个类似的,用的python的unconv库,然后创建队列,开4个线程处理,在线程里面去调用转换程序
  
#2楼 [ 楼主] 2012-11-28 00:08 Hongten  
@小子欠扁
不过做这个例子程序的的时候,必须安装软件,这个稍微较麻烦一点,其他的还好啦...没有出现乱码...
http://pic.cnitblog.com/face/284226/20130809195932.png
  
#3楼 2013-03-20 11:28 zjashy  
你好,你这个程序不能转换office2003?打开失败?
  
#4楼 [ 楼主] 2013-03-21 19:57 Hongten  
@zjashy
后台有报什么错误信息...
http://pic.cnitblog.com/face/284226/20130809195932.png
  
#5楼 2013-04-25 14:19 booleanz  
你好,我开发好的要部署到服务器,这个openoffice是不是要安装到服务器的C盘什么的?
  
#6楼 [ 楼主] 2013-04-25 14:21 Hongten  
@booleanz
是的....
http://pic.cnitblog.com/face/284226/20130809195932.png
  
#7楼 2013-04-25 14:33 booleanz  
@
我的怎么报以下错误呢?
java.lang.NoClassDefFoundError: com/sun/star/lang/DisposedException
at org.artofsolving.jodconverter.office.PooledOfficeManager.(PooledOfficeManager.java:65)
at org.artofsolving.jodconverter.office.ProcessPoolOfficeManager.(ProcessPoolOfficeManager.java:55)
at org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration.buildOfficeManager(DefaultOfficeManagerConfiguration.java:185)
at com.kingdee.eas.cp.odm.util.Byte2LocalFileServlet.getOfficeManager(Byte2LocalFileServlet.java:156)
  
#8楼 2013-04-25 15:57 booleanz  
后来报这个错误:
org.artofsolving.jodconverter.office.OfficeException: could not load document: 1366876467315.doc
  
#9楼 [ 楼主] 2013-04-25 19:07 Hongten  
@booleanz
第一个是jar包没有加对,或者没有加
第一个可能是路径问题。。
http://pic.cnitblog.com/face/284226/20130809195932.png
  
#10楼 2013-04-26 09:57 booleanz  
@Hongten
第一个是确实就jar包没有加,我加了后报的第二个问题。
第二个问题是因为我的word在数据库里是用byte[] 格式保存的,
将byte[] 转成的word文档openoffice打不开,也就转不了。
请问下,我面对的情况还有其他方式解决吗?
  
#11楼 [ 楼主] 2013-04-26 14:09 Hongten  
@booleanz
你的这种感觉情况我还没有遇到过,不过有一个方案你可以参考一下:你把byte[]格式的东东转换为word后,手动打开看看转换后的word的格式是不是正确的,如果不是,则手动重新新建一个word,把之前转换后的word的内容复制到新建的word里面,在对新建的word进行转换...这个方案我对openoffice.org Writer打不开的,但是Microsoft Office Word又能打开有效...祝你好运..
http://pic.cnitblog.com/face/284226/20130809195932.png
  
#12楼 2013-06-04 23:03 残留  
去年的一个项目用到过,一直未看这部分的源代码,真的很强大!
  
#13楼 [ 楼主] 2013-06-05 00:01 Hongten  
@残留
因为我之前应该和你一样,也想得到一份完完整整的源代码,所以,我经过查阅很多资料,把这个功能整理出来,并且把源代码开放出来,希望能帮到需要这个功能的开发者。
http://pic.cnitblog.com/face/284226/20130809195932.png
  
#14楼 2013-09-03 16:13 ICHI  
你好,博主,最近实验室有个项目也需要做成类似于百度文库之类的东西,但是如果需要安装OpenOffice的话,实施起来就有点麻烦,有没有什么OpenOffice的库文件之类的可以不用安装OpenOffice吗?
  
#15楼 [ 楼主] 2013-09-03 17:01 Hongten  
@ICHI
我之前查过资料,使用word转换为swf有三种方式:
我只是选择其中的一种,即上面的方式,不过我没有去试过其他方法
还有就是使用上面的方法,我的做法是按照OpenOffice,而你所说的库文件之类的你可以到OpenOffice的官网看看: http://www.openoffice.org/
http://pic.cnitblog.com/face/284226/20130809195932.png
  
#16楼 2014-04-19 17:49 supmain  
我按博主的方法试了一下,抛出:java.lang.NoClassDefFoundError: org/json/JSONException
Caused by: java.lang.ClassNotFoundException: org.json.JSONException
异常。博主的工程我下载下来了,可是还是有很多包没有,可以列一下吗?还有我的jdk是1.6的,不知道可不可以?
  
#17楼 [ 楼主] 2014-04-19 19:17 Hongten  
@supmain
你可以到 java开发_模仿百度文库_OpenOffice2PDF_注意事项 获取到更多的注意事项。
http://pic.cnitblog.com/face/284226/20130809195932.png
  
#18楼 2014-04-28 12:32 我猜猜我猜猜  
楼主有遇到pdf转成swf文件速度慢么,我现在是同一个大小的文件,不同类型,pdf转swf要2分钟,word转就只需要20秒
  
#19楼 [ 楼主] 2014-04-28 21:33 Hongten  
@我猜猜我猜猜
这里是我之前做的demo: java开发_模仿百度文库_SWFTools_源码下载 我做的时候,不会花很长时间哦
http://pic.cnitblog.com/face/284226/20130809195932.png
  
#20楼 [ 楼主] 2014-04-28 21:48 Hongten  
@我猜猜我猜猜
今晚我有做了一下测试,都没有超过2分钟的哈。如果可以的话,把你做的代码贴一下,我看看
http://pic.cnitblog.com/face/284226/20130809195932.png
  
#21楼 2014-05-04 15:25 我猜猜我猜猜  

pdf转swf,楼主可以下载《JavaScript高级程序设计.pdf》自己测试下浏览速度
  
#22楼 2014-05-04 16:47 我猜猜我猜猜  
直接调用安装路径下的pdf2swf在cmd下执行转《JavaScript高级程序设计.pdf》 也需要2,3分钟 ,应该不是代码问题。
  
#23楼 [ 楼主] 2014-05-04 17:34 Hongten  
@我猜猜我猜猜
命令操作:D:\SWFTools/pdf2swf.exe -t "e:/javascript高级程序设计.pdf" -o "e:/test_1352107155307_1399195936609.swf" -s flashversion=9 -s languagedir=D:\xpdf\xpdf-chinese-simplified
开始转换...
转换结束...
转换共耗时 :[43453]ms
转换文件成功!!
差不多是40多秒的样子!
我的电脑配置: Hongten's Computer
http://pic.cnitblog.com/face/284226/20130809195932.png
  
#24楼 2014-05-04 17:37 我猜猜我猜猜  
@Hongten
引用 @我猜猜我猜猜
命令操作:D:\SWFTools/pdf2swf.exe -t "e:/javascript高级程序设计.pdf" -o "e:/test_1352107155307_1399195936609.swf" -s flashversion=9 -s languagedir=D:\xpdf\xpdf-chinese-simplified
开始转换...
转换结束...
转换共耗时 :[43453]ms
转换文件成功!!
差不多是40多秒的样子!
我的电脑配置:Hongten's Computer

加个QQ 412537827 详细请教下
  
#25楼 [ 楼主] 2014-05-04 17:38 Hongten  
@我猜猜我猜猜
我使用的是: java开发_模仿百度文库_SWFTools_源码下载中的 PDF2SWF 类
http://pic.cnitblog.com/face/284226/20130809195932.png
  
#26楼 [ 楼主] 2014-05-04 17:47 Hongten  
@我猜猜我猜猜
如果使用: java开发_模仿百度文库_SWFTools_源码下载中的 ConvertToSwf 类转换,时间一定是比 PDF2SWF 类转换慢。
具体缘由,在看看代码就知道了哈
http://pic.cnitblog.com/face/284226/20130809195932.png
  
#27楼 2014-05-05 10:13 我猜猜我猜猜  
命令操作:E:/Program Files (x86)/SWFTools/pdf2swf.exe -t "d:/JavaScript高级程序设计.pdf" -o "e:/test_1352107155307_1399255439071.swf" -s flashversion=9 -s languagedir=D:\xpdf\xpdf-chinese-simplified
开始转换...
转换结束...
转换共耗时 :[355358]ms
转换文件成功!!
直接用的你的代码。。。
电脑配置8G内存,4核 ,win7 64位 ,jdk6.0。
我装的swf软件是swftools-2013-04-09-1007.exe。难道是这个软件问题,因为我直接在命令窗口转换也是几分钟

换了一个版本swftools-0.9.0.exe
命令操作:E:/Program Files (x86)/SWFTools/pdf2swf.exe -t "d:/JavaScript高级程序设计.pdf" -o "e:/test_1352107155307_1399269794612.swf" -s flashversion=9 -s languagedir=D:\xpdf\xpdf-chinese-simplified
开始转换...
转换结束...
转换共耗时 :[101129]ms
转换文件成功!!
竟然快了3倍,当然跟你的比还是慢了将近3倍
  
#28楼 [ 楼主] 2014-05-05 20:36 Hongten  
@我猜猜我猜猜
我测试了一下,应该是swftools的版本问题哈,版本高的,转换的快..
http://pic.cnitblog.com/face/284226/20130809195932.png
  
#29楼 2014-05-06 09:26 我猜猜我猜猜  
@Hongten
这个就是最高版本了吧swftools-2013-04-09-1007.exe,可是它最慢,楼主把你的安装文件发我下QQ邮箱吧
  
#30楼 2014-05-13 12:30 我猜猜我猜猜  
楼主的 swf软件 共享发一下撒
  
#31楼 [ 楼主] 2014-05-14 08:20 Hongten  
@我猜猜我猜猜
我用的是也是: swftools-2013-04-09-1007.exe
http://pic.cnitblog.com/face/284226/20130809195932.png
  
#32楼 2938528 2014/5/14 9:35:55 2014-05-14 09:35 我猜猜我猜猜  
@Hongten
不是吧。你用这个版本转换只要40s...我用这个版本+你的代码要2分多钟
历史上的今天:
2011-11-05 android开发-TextView控件学习

猜你喜欢

转载自blog.csdn.net/cs123456789dn/article/details/39927907