TA的每日心情 衰 2021-2-2 11:21
签到天数: 36 天
[LV.5]常住居民I
一:测试平台 win7 64位系统
: c( B2 f/ g5 a- b 二:准备软件 OpenOffice、swftools 9 W( X! p T$ U. x
OpenOffice 下载地址:http://www.openoffice.org/download/index.html 8 s6 u y$ t0 m2 I$ N) o
swftools下载地址:http://www.swftools.org/download.html + {) j" q! I" M* B/ s7 D
三:测试代码
2 O* ?2 G z( k; b, x; h1 @ 1、conf.properties 软软件路径配置
0 e6 t: X$ s! j. ~ OpenOffice_HOME = C:/Program Files (x86)/OpenOffice 4
; a' G( x1 W7 P- z
& d( a' Y& X( b; A% m0 f( a SWFTools_HOME = D:/SWF/SWFTools 复制代码 第一行 是OpenOffice 的安装路径 安装的时候请记住。& c' c2 b- D8 N$ l
第二行 是swftools的安装路径。
/ |& d0 b, F7 V8 `6 Q S2 }: | 你可以自定义安装路径最好放到一起、便于管理。# [" x( u. J" m D ?1 N
2、文档转换$ G% n' B, G6 D
/**4 K2 y9 N4 B! k- m
* 将odt、doc、docx、ppt或pptx格式的文件转换为pdf文件,如果文件后缀是pdf,则直接返回true。, h+ H% |2 q. ]$ v3 o
*
4 E ]# c& C- \0 q) o4 h3 a5 t * fileSuffix 文件的后缀。0 g/ C( R* ~8 K- P) ]
*/
! ]( U2 }6 V- i) D' M private boolean converToPdf(String sourceFilePath, String pdfFileSavePath){! p! Y* f. Y8 S# u9 R! R! ]" w. L
File sourceFile = new File(sourceFilePath);* X- d3 F0 Y; Q7 q
if (sourceFile.exists()) {2 e, d; J# o1 g. V: X8 }7 s( v- s9 k
String OpenOffice_HOME = ConfProperties.getOpenOfficeHome();//OpenOffice的安装根目录
4 @8 O1 X$ q& L8 ` String command = OpenOffice_HOME + "/program/soffice.exe -headless -accept="socket,host=127.0.0.1,port=8100;urp;"";
; K1 o& J' ~( j& r, e6 _# W( i System.out.println("command"+command);
' `3 U n/ ^, \; T+ g8 f8 a try {
' o( h& [0 v% I Process process = Runtime.getRuntime().exec(command);//启动OpenOffice的服务
, u5 s7 a# m$ o OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
* M1 n+ t) K1 _1 ^/ }$ M) a1 t& {4 a connection.connect();
" [4 b1 e( a4 h. D- t if(connection.isConnected()){//如果连接成功# \5 U9 t( ]3 A: j! r, m/ \
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
* I/ T! F# n% r% G5 k converter.convert(sourceFile, new File(pdfFileSavePath));# s3 U' o0 }# w4 K
connection.disconnect();// close the connection
K9 G! E8 D/ V$ W" h* A& { process.destroy();// 封闭OpenOffice服务的进程
2 M% R. z H8 p" [7 b System.out.println("****pdf转换成功,PDF输出:" + pdfFileSavePath+ "****"); ( R" ]) x$ S) J
return true;& ?4 Q, X! U; g- f. D, _
}else{
- b8 W1 K# |/ [" f0 J+ Y3 I# N return false;//swf转换器异常,openoffice连接失败!% ?3 e1 ?- p* n' |
}2 ^( ?$ e4 A9 v# o: M( s
} catch (ConnectException e) {
$ d2 ^4 b- P* l% y, z9 H e.printStackTrace();4 q7 m. A8 T7 j# [
System.out.println("****swf转换器异常,openoffice服务未启动!****");
( K. ^- u0 v1 v$ e8 M return false;, s b3 M7 P# F( C, g
} catch (OpenOfficeException e) {7 p, W& b5 O* U
e.printStackTrace();
( D B! Y0 x/ ~$ h0 Z System.out.println("****swf转换器异常,读取转换文件失败****");3 q7 H+ \8 ]3 q$ I+ A) B# u% u2 P! m
return false;
1 k- N0 V4 h l: x2 F P } catch (Exception e) {
" Z9 J/ E0 G9 C. |( M- _3 \ r# r8 r e.printStackTrace();- K( T( l6 X- g1 z3 [
return false;" [+ `$ \0 ~, J/ L
}( T, G; Y$ c! K" ?8 q% J3 j+ o _
} else {
4 V& N4 v$ T6 A2 d; ]; {/ @4 w# W, U System.out.println("****swf转换器异常,需要转换的文档不存在,无法转换****");
! {1 Y, x( `6 U3 j0 O" j. @ return false;
* G6 B+ |/ n* q$ t1 X. S# k }, @! o' n( @+ Z9 R( V$ u% a
} 复制代码 将pdf文件转换为swf文件:
0 |2 M, n* o# l+ C: I& u9 J /**
# V+ N3 J5 ?7 v9 B7 P# d * 将pdf文件转换为swf文件。
6 r& N& i K. E0 X- j# ?. N7 a *4 H* Q* j6 R% r0 w9 t6 E5 _
* @author ghj8 c( e8 b, Y0 l( c0 ^
*/
# X ~6 m; I( j& @/ I6 \ private Map<String, String> converFromPdfToSwf(String pdfFilePath,String swfFileSavePath){
* ~1 d8 @4 H s6 S; ]7 D String OS = System.getProperty("os.name").toLowerCase();
" g* E: c( h' r0 V& C Map<String, String> returnMap = new HashMap<String, String>();% _1 }$ H& d& o4 G1 ]4 ~# I' n
File pdfFile = new File(pdfFilePath);
/ y5 Z3 N1 q, s1 |- u/ F, y Runtime r = Runtime.getRuntime();0 B4 g4 f. \# A8 \( b
if (pdfFile.exists()) {
8 M' b1 f6 N. h& G# | String SWFTools_HOME = ConfProperties.getSWFToolsHome();// SWFTools的安装根目录) F N6 u. F8 a
try {+ J. m. ?+ K5 d" O o; U7 o
if (OS.indexOf("windows")>=0) {// windows环境处理5 Z. \( C2 C: j" p& h
Process process = r.exec(SWFTools_HOME+"/pdf2swf.exe "+ pdfFilePath + " -s flashversion=9 -o "+ swfFileSavePath);& J& y( R6 n: v/ l
System.out.print(loadStream(process.getInputStream()));
2 K- R" A! j& x System.err.print(loadStream(process.getErrorStream()));5 } }- W6 Q y: w- e
System.out.print(loadStream(process.getInputStream()));
% P# s- i7 d2 E) w6 X$ c System.err.println("****swf转换成功,文件输出:"+ swfFileSavePath + "****");+ y& [0 B3 U( O+ x. }
process.destroy();// 销毁进程
' E% [. `6 k- N0 Y0 O1 p+ n# f } else if (OS.indexOf("linux")>=0) {// linux环境处理
. i; p! M- l* ~8 S7 U( m Process process = r.exec(SWFTools_HOME+"/pdf2swf "+ pdfFilePath + " -o " + swfFileSavePath + " -T 9");5 @" n: c. U, C7 v
System.out.print(loadStream(process.getInputStream()));
9 N7 \# D8 P: T System.err.print(loadStream(process.getErrorStream()));
. c L6 F& M% ?5 |0 M2 | System.err.println("****swf转换成功,文件输出:"+ swfFileSavePath + "****");
' \5 e0 \; X* h" H F! i process.destroy();// 销毁进程/ t8 c/ ]; J/ B7 M$ F$ b
}) V$ @8 n1 d" G6 _. R
if (pdfFile.exists()) {
" Z+ ]( W ~: |% { pdfFile.delete();//转换成功以后删除上传的pdf文件和转换的pdf文件。
) x! N- {" m6 K3 R% L8 i+ W; J }
- ?* p0 c) L \6 n) j# Q returnMap.put("status", "true");
7 R. I* }# j2 q5 u( w0 ^ returnMap.put("swfFileName", swfFileSavePath.substring(swfFileSavePath.lastIndexOf("/")+1));//便于获取转换后swf文件的名字
) \; [' D0 S0 ` } catch (IOException e) {
! W7 y2 n& Z' m4 z e.printStackTrace();
1 V1 H$ j+ A$ C) w returnMap.put("status", "false");* b( I) M- c9 g( @; H
}( a3 b8 C- N6 A4 I8 I) J+ ?7 A2 [* K4 h
return returnMap;
7 _6 s9 z: S/ q" B% x; `: s2 \2 y } else {
1 S2 v8 l G+ {; z9 Z) J4 V3 r, h9 w System.out.println("****pdf不存在,无法转换****");
8 Y8 c+ w+ O5 W( d/ @ returnMap.put("status", "false");
/ u$ o- p; M6 ]% U& t return returnMap; P1 K6 X- |& D( a( i# q8 @
}
9 _ u: i% J/ o7 z2 } ?. q } 复制代码
( g4 E* K, C3 L, I 四:运行项目 测试项目前 请先运行安装好的OpenOffice、swftools % ?2 Q, }/ H/ M# g. M
测试页面: 2 I- d R h6 A/ O0 t) ]
+ o4 Z* ?# t, e" r+ O
$ J& S! r$ ?+ P( I5 D
4 I7 K7 Y1 S6 ]6 d) c
0 K; r+ I! T: ~! M% A
' q, h4 f, b: y& u1 [" @ : a# {# ]' { V( G
# X$ _' `% N2 `! ] 源码 下载地址:点击下载 - M4 K' c9 w7 [4 f. D6 Y) z) B/ u
- _/ L% E8 W$ l Z; h' y% X, l, a
% L2 Z1 n& T; A, H/ R# S: X" Y& u2 Y
软件下载地址:点击下载
% J- O4 F, W% t9 V 5 T2 g2 `6 R2 z% A8 B- e7 M
$ g8 x- ~9 D; f" f# ]# B/ ?3 _& S
科帮网 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关2、本站所有主题由该帖子作者发表,该帖子作者与科帮网 享有帖子相关版权3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和科帮网 的同意4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意7、科帮网 管理员和版主有权不事先通知发贴者而删除本文
JAVA爱好者①群:
JAVA爱好者②群:
JAVA爱好者③ :