公司需要开发一个类似百度文库功能的管理站,在网上找了好久,主要有两种实现方法,我在这里根据网上一篇文章,总结了一下具体的实现。
首先下载必要的文件。
1、SWF显示组件 flexpaper 下载地址 http://flexpaper./
2、DOC文件转换为PDF文件 openoffice3.2
3、PDF文件转换SWF文件 pdf2swf.exe
4、实现在java类中操作openoffice3.2 的类包 jodconverter-2.2.2
flexpaper可以去上面的官网地址下载,但直接下载的组件会有广告和一些不需要用到的功能,所以最好是自己下载Flex源码进行修改
接下来要通过java类来实现文件类型的转换,在网上直接找到该类的代码。 003 | import java.io.BufferedInputStream; |
005 | import java.io.IOException; |
006 | import java.io.InputStream; |
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; |
016 | * @author Administrator |
019 | public class DocConverter { |
020 | private static final int environment = 1 ; // 环境 1:windows 2:linux |
022 | private String fileString; |
023 | private String outputPath = "" ; // 输入路径 ,如果不设置就输出在默认的位置 |
024 | private String fileName; |
025 | private File pdfFile; |
026 | private File swfFile; |
027 | private File docFile; |
029 | public DocConverter(String fileString) { |
038 | public void setFile(String fileString) { |
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" ); |
060 | private void doc2pdf() throws Exception { |
061 | if (docFile.exists()) { |
062 | if (!pdfFile.exists()) { |
063 | OpenOfficeConnection connection = new SocketOpenOfficeConnection( |
066 | connection.connect(); |
067 | DocumentConverter converter = new OpenOfficeDocumentConverter( |
069 | converter.convert(docFile, pdfFile); |
070 | // close the connection |
071 | connection.disconnect(); |
072 | System.out.println( "****pdf转换成功,PDF输出:" + pdfFile.getPath() |
074 | } catch (java.net.ConnectException e) { |
076 | System.out.println( "****swf转换器异常,openoffice服务未启动!****" ); |
078 | } catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) { |
080 | System.out.println( "****swf转换器异常,读取转换文件失败****" ); |
082 | } catch (Exception e) { |
087 | System.out.println( "****已经转换为pdf,不需要再进行转化****" ); |
090 | System.out.println( "****swf转换器异常,需要转换的文档不存在,无法转换****" ); |
097 | private void pdf2swf() throws Exception { |
098 | Runtime r = Runtime.getRuntime(); |
099 | if (!swfFile.exists()) { |
100 | if (pdfFile.exists()) { |
101 | if (environment == 1 ) { // windows环境处理 |
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()) { |
113 | } catch (IOException e) { |
117 | } else if (environment == 2 ) { // linux环境处理 |
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()) { |
128 | } catch (Exception e) { |
134 | System.out.println( "****pdf不存在,无法转换****" ); |
137 | System.out.println( "****swf已经存在不需要转换****" ); |
141 | static String loadStream(InputStream in) throws IOException { |
144 | in = new BufferedInputStream(in); |
145 | StringBuffer buffer = new StringBuffer(); |
147 | while ((ptr = in.read()) != - 1 ) { |
148 | buffer.append(( char ) ptr); |
151 | return buffer.toString(); |
157 | public boolean conver() { |
159 | if (swfFile.exists()) { |
160 | System.out.println( "****swf转换器开始工作,该文件已经转换为swf****" ); |
164 | if (environment == 1 ) { |
165 | System.out.println( "****swf转换器开始工作,当前设置运行环境windows****" ); |
167 | System.out.println( "****swf转换器开始工作,当前设置运行环境linux****" ); |
172 | } catch (Exception e) { |
177 | if (swfFile.exists()) { |
189 | public String getswfPath() { |
190 | if (swfFile.exists()) { |
191 | String tempString = swfFile.getPath(); |
192 | tempString = tempString.replaceAll( "\\\\" , "/" ); |
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" ); |
211 | swfFile = new File(outputPath + realName + ".swf" ); |
216 | public static void main(String s[]) { |
217 | DocConverter d = new DocConverter( "D:/1.doc" ); |
贴入上面代码前
第一步:先确认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项目中,生成的文件名可以根据自己的要求来变更,最终只用掉用上面的工具类代码就可以了。
|