java实现类似百度文库功能

公司需要开发一个类似百度文库功能的管理站,在网上找了好久,主要有两种实现方法,我在这里根据网上一篇文章,总结了一下具体的实现。

首先下载必要的文件。

1、SWF显示组件 flexpaper  下载地址 http://flexpaper.devaldi.com/

2、DOC文件转换为PDF文件 openoffice3.2

3、PDF文件转换SWF文件  pdf2swf.exe

4、实现在java类中操作openoffice3.2 的类包  jodconverter-2.2.2


flexpaper可以去上面的官网地址下载,但直接下载的组件会有广告和一些不需要用到的功能,所以最好是自己下载Flex源码进行修改

接下来要通过java类来实现文件类型的转换,在网上直接找到该类的代码。

001 package com;
002  
003 import java.io.BufferedInputStream;
004 import java.io.File;
005 import java.io.IOException;
006 import java.io.InputStream;
007  
008 import com.artofsolving.jodconverter.DocumentConverter;
009 import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
010 import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
011 import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
012  
013 /**
014  * doc docx格式转换
015  *
016  * @author Administrator
017  *
018  */
019 public class DocConverter {
020     private static final int environment = 1;// 环境 1:windows 2:linux
021                                                 // (只涉及pdf2swf路径问题)
022     private String fileString;
023     private String outputPath = "";// 输入路径 ,如果不设置就输出在默认的位置
024     private String fileName;
025     private File pdfFile;
026     private File swfFile;
027     private File docFile;
028  
029     public DocConverter(String fileString) {
030         ini(fileString);
031     }
032  
033     /**
034      * 重新设置file
035      *
036      * @param fileString
037      */
038     public void setFile(String fileString) {
039         ini(fileString);
040     }
041  
042     /**
043      * 初始化
044      *
045      * @param fileString
046      */
047     private void ini(String fileString) {
048         this.fileString = fileString;
049         fileName = fileString.substring(0, fileString.lastIndexOf("."));
050         docFile = new File(fileString);
051         pdfFile = new File(fileName + ".pdf");
052         swfFile = new File(fileName + ".swf");
053     }
054  
055     /**
056      * 转为PDF
057      *
058      * @param file
059      */
060     private void doc2pdf() throws Exception {
061         if (docFile.exists()) {
062             if (!pdfFile.exists()) {
063                 OpenOfficeConnection connection = new SocketOpenOfficeConnection(
064                         8100);
065                 try {
066                     connection.connect();
067                     DocumentConverter converter = new OpenOfficeDocumentConverter(
068                             connection);
069                     converter.convert(docFile, pdfFile);
070                     // close the connection
071                     connection.disconnect();
072                     System.out.println("****pdf转换成功,PDF输出:" + pdfFile.getPath()
073                             "****");
074                 catch (java.net.ConnectException e) {
075                     e.printStackTrace();
076                     System.out.println("****swf转换器异常,openoffice服务未启动!****");
077                     throw e;
078                 catch(com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {
079                     e.printStackTrace();
080                     System.out.println("****swf转换器异常,读取转换文件失败****");
081                     throw e;
082                 catch (Exception e) {
083                     e.printStackTrace();
084                     throw e;
085                 }
086             else {
087                 System.out.println("****已经转换为pdf,不需要再进行转化****");
088             }
089         else {
090             System.out.println("****swf转换器异常,需要转换的文档不存在,无法转换****");
091         }
092     }
093  
094     /**
095      * 转换成 swf
096      */
097     private void pdf2swf() throws Exception {
098         Runtime r = Runtime.getRuntime();
099         if (!swfFile.exists()) {
100             if (pdfFile.exists()) {
101                 if (environment == 1) {// windows环境处理
102                     try {
103                         Process p = r.exec("D:/pdf2swf.exe "+ pdfFile.getPath() + " -o "+ swfFile.getPath() + " -T 9");
104                         System.out.print(loadStream(p.getInputStream()));
105                         System.err.print(loadStream(p.getErrorStream()));
106                         System.out.print(loadStream(p.getInputStream()));
107                         System.err.println("****swf转换成功,文件输出:"
108                                 + swfFile.getPath() + "****");
109                         if (pdfFile.exists()) {
110                             pdfFile.delete();
111                         }
112  
113                     catch (IOException e) {
114                         e.printStackTrace();
115                         throw e;
116                     }
117                 else if (environment == 2) {// linux环境处理
118                     try {
119                         Process p = r.exec("pdf2swf " + pdfFile.getPath()
120                                 " -o " + swfFile.getPath() + " -T 9");
121                         System.out.print(loadStream(p.getInputStream()));
122                         System.err.print(loadStream(p.getErrorStream()));
123                         System.err.println("****swf转换成功,文件输出:"
124                                 + swfFile.getPath() + "****");
125                         if (pdfFile.exists()) {
126                             pdfFile.delete();
127                         }
128                     catch (Exception e) {
129                         e.printStackTrace();
130                         throw e;
131                     }
132                 }
133             else {
134                 System.out.println("****pdf不存在,无法转换****");
135             }
136         else {
137             System.out.println("****swf已经存在不需要转换****");
138         }
139     }
140  
141     static String loadStream(InputStream in) throws IOException {
142  
143         int ptr = 0;
144         in = new BufferedInputStream(in);
145         StringBuffer buffer = new StringBuffer();
146  
147         while ((ptr = in.read()) != -1) {
148             buffer.append((char) ptr);
149         }
150  
151         return buffer.toString();
152     }
153  
154     /**
155      * 转换主方法
156      */
157     public boolean conver() {
158  
159         if (swfFile.exists()) {
160             System.out.println("****swf转换器开始工作,该文件已经转换为swf****");
161             return true;
162         }
163  
164         if (environment == 1) {
165             System.out.println("****swf转换器开始工作,当前设置运行环境windows****");
166         else {
167             System.out.println("****swf转换器开始工作,当前设置运行环境linux****");
168         }
169         try {
170             doc2pdf();
171             pdf2swf();
172         catch (Exception e) {
173             e.printStackTrace();
174             return false;
175         }
176  
177         if (swfFile.exists()) {
178             return true;
179         else {
180             return false;
181         }
182     }
183  
184     /**
185      * 返回文件路径
186      *
187      * @param s
188      */
189     public String getswfPath() {
190         if (swfFile.exists()) {
191             String tempString = swfFile.getPath();
192             tempString = tempString.replaceAll("\\\\""/");
193             return tempString;
194         else {
195             return "";
196         }
197  
198     }
199  
200     /**
201      * 设置输出路径
202      */
203     public void setOutputPath(String outputPath) {
204         this.outputPath = outputPath;
205         if (!outputPath.equals("")) {
206             String realName = fileName.substring(fileName.lastIndexOf("/"),
207                     fileName.lastIndexOf("."));
208             if (outputPath.charAt(outputPath.length()) == '/') {
209                 swfFile = new File(outputPath + realName + ".swf");
210             else {
211                 swfFile = new File(outputPath + realName + ".swf");
212             }
213         }
214     }
215  
216     public static void main(String s[]) {
217         DocConverter d = new DocConverter("D:/1.doc");
218         d.conver();
219     }
220 }
贴入上面代码前

第一步:先确认OpenOffice是否已经安装,因为在代码中要引入到文件安装的路径

第二步:确认你的项目中引入了jodconverter-2.2.2的jar包

第三步:在DOS中启动OpenOffice的服务

找到OpenOffice的安装路径并执行下面代码

C:\Program Files\OpenOffice.org 3\program  soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" –nofirststartwizard

注:我在执行上面代码时,DOS报出了找不到–nofirststartwizard文件的异常,所以我去掉了–nofirststartwizard这一段,执行也成功了。

C:\Program Files\OpenOffice.org 3\program  soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;"

确认所有准备工作都完成后,在下面代码中变更你pdf2swf.exe的文件位置

1 Process p = r.exec("D:/pdf2swf.exe "+ pdfFile.getPath() + " -o "+ swfFile.getPath() + " -T 9");
最后在main方法传入你要转换的文件路径和文件名,执行就可以生成swf文件了。


在WEB项目中,生成的文件名可以根据自己的要求来变更,最终只用掉用上面的工具类代码就可以了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值