公司需要開發一個類似百度文庫功能的管理站,在網上找了好久,主要有兩種實現方法,我在這裏根據網上一篇文章,總結了一下具體的實現。
首先下載必要的文件。
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" ); |
在WEB項目中,生成的文件名可以根據自己的要求來變更,最終只用掉用上面的工具類代碼就可以了。