TA的每日心情 | 衰 2021-2-2 11:21 |
|---|
签到天数: 36 天 [LV.5]常住居民I
|
一:测试平台 win7 64位系统
0 c6 L% ` ?& h! I( ~' g二:准备软件 OpenOffice、swftools; u/ P) Z: |; G1 u6 b7 p
OpenOffice 下载地址:http://www.openoffice.org/download/index.html N4 q: n" [; |# a: G, R) o! Z
swftools下载地址:http://www.swftools.org/download.html
) K" A \$ n0 q/ h5 `, J三:测试代码9 _9 L2 f7 }( B# C& R
1、conf.properties 软软件路径配置
& q5 z" ^9 z. ~ P% C2 H5 \" s4 ~* R- OpenOffice_HOME = C:/Program Files (x86)/OpenOffice 4
4 p! ?7 U7 a% e8 t
* b! }0 V# Q: i4 i4 A2 c, S- X5 K0 ]- SWFTools_HOME = D:/SWF/SWFTools
复制代码 第一行 是OpenOffice 的安装路径 安装的时候请记住。/ @1 t* P% p0 o
第二行 是swftools的安装路径。8 I6 j3 c* S4 }' P& F; {
你可以自定义安装路径最好放到一起、便于管理。3 t4 g( \4 }8 a! J& W; e5 D
2、文档转换
# ?1 _) g$ B' f } h% o- /**2 q$ h6 M% K1 }7 W1 s2 u* S. U
- * 将odt、doc、docx、ppt或pptx格式的文件转换为pdf文件,如果文件后缀是pdf,则直接返回true。' Y8 ~4 w( v, ~# v* Z
- * : n+ j; C8 X( j3 }6 |7 \; @
- * fileSuffix 文件的后缀。; B q' X: M0 {% B. t$ U
- */- w0 u: b$ i/ i
- private boolean converToPdf(String sourceFilePath, String pdfFileSavePath){+ B' u( ?+ b1 D) ]- `( p3 x
- File sourceFile = new File(sourceFilePath);7 B" H4 u8 C6 m$ o6 _
- if (sourceFile.exists()) {
7 v: `6 C" p$ a( x6 R) m - String OpenOffice_HOME = ConfProperties.getOpenOfficeHome();//OpenOffice的安装根目录
/ f! x6 J* B( w4 Z3 [ - String command = OpenOffice_HOME + "/program/soffice.exe -headless -accept="socket,host=127.0.0.1,port=8100;urp;"";
3 y; C1 j) u6 n. }8 b6 M+ ~ - System.out.println("command"+command);
) s" B! c( a, I J - try {5 g6 H# f9 L- x3 Y) c! E( a1 n
- Process process = Runtime.getRuntime().exec(command);//启动OpenOffice的服务$ [3 K3 W; U9 L3 C) q
- OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
0 Q+ P; j/ I3 v% B% I - connection.connect();
$ k% w: `( q( P1 b h; [4 b' w - if(connection.isConnected()){//如果连接成功
8 Q* ]) f1 _( a4 G8 A7 i9 O - DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
4 Z- @: f, w, m8 h- J* v6 t8 Y - converter.convert(sourceFile, new File(pdfFileSavePath));9 ^0 P, E- e2 P% g/ p! K, ], ]
- connection.disconnect();// close the connection/ m3 b1 m7 B/ w' W% C
- process.destroy();// 封闭OpenOffice服务的进程- y. V) a- R% j/ G6 P
- System.out.println("****pdf转换成功,PDF输出:" + pdfFileSavePath+ "****"); ( P$ a- k3 U+ p/ G% s! f
- return true;
" l% z: o: U/ e) W3 R - }else{
- w1 A( }; F" _% w8 _2 ~: V - return false;//swf转换器异常,openoffice连接失败!5 C; r& X c: {5 C4 b; ?9 Z
- }
: f/ @6 M- o1 d, \) i: f - } catch (ConnectException e) {
' I; x7 ^8 U) C - e.printStackTrace();+ X1 _. t r" L& v
- System.out.println("****swf转换器异常,openoffice服务未启动!****");
* c2 A( S" K- O7 U. ?: x - return false;
# j% m# e! q, [" j: v8 T1 K' U) p - } catch (OpenOfficeException e) { V% H5 b' W9 L5 Z
- e.printStackTrace();% `& F' S- K) u; c( n$ I
- System.out.println("****swf转换器异常,读取转换文件失败****");
+ E+ v" t- p- I+ f - return false;' M* k5 v3 X3 O' ?" Z. q3 ?
- } catch (Exception e) {
8 {- D% M" ?0 v - e.printStackTrace();
" e. o+ Z& e$ p$ |- y - return false;
& a( u' x H0 C" C( d" t c - }& O& `% r1 J: g# @" R
- } else {
% `: @# e* {% S- } - System.out.println("****swf转换器异常,需要转换的文档不存在,无法转换****");1 ]: n$ z* {, y
- return false;8 A" t4 Z; O* h3 {; }1 g( s, P
- }
3 e& Z1 n; U6 |2 I; j! K) Q - }
复制代码 将pdf文件转换为swf文件:3 Q8 ^* g* l3 R) n8 p- W7 N
- /**
* {! R$ X2 h$ `& {% Q - * 将pdf文件转换为swf文件。
5 q9 e( T; |& p4 N - *3 R" A% w- l0 M, Z; O3 x# U
- * @author ghj
9 o. I! U& {/ O' h2 v - */
6 P# U6 s8 U6 J3 ]' Y( g3 b - private Map<String, String> converFromPdfToSwf(String pdfFilePath,String swfFileSavePath){8 g' k* Q9 K) G* E) ?4 K
- String OS = System.getProperty("os.name").toLowerCase();
4 F" D- h! d/ k6 ~; q1 O, y - Map<String, String> returnMap = new HashMap<String, String>();, x) N/ @# V& L, @+ |5 H1 W
- File pdfFile = new File(pdfFilePath);+ f/ l& s. S7 y: |, l3 Q) p o
- Runtime r = Runtime.getRuntime();
C5 {/ `8 q3 m, u - if (pdfFile.exists()) {% d0 b0 G, k( f9 S0 h- Q
- String SWFTools_HOME = ConfProperties.getSWFToolsHome();// SWFTools的安装根目录/ L. p- t& g" Y( b( s. H3 X
- try {/ X9 e+ s+ N& h ^2 v! S+ y% u+ {
- if (OS.indexOf("windows")>=0) {// windows环境处理
. {; Q: ]7 f. I/ {" Z! |" Q/ r/ E& S& x - Process process = r.exec(SWFTools_HOME+"/pdf2swf.exe "+ pdfFilePath + " -s flashversion=9 -o "+ swfFileSavePath);0 H' ~/ l2 @: {) `% k
- System.out.print(loadStream(process.getInputStream()));# s8 I3 o5 w: z% p+ R9 v
- System.err.print(loadStream(process.getErrorStream()));7 S/ ?3 T5 Q8 y) g; j9 b3 X; N# ]
- System.out.print(loadStream(process.getInputStream()));0 `$ F+ `" V& r
- System.err.println("****swf转换成功,文件输出:"+ swfFileSavePath + "****");; y8 k& |; b% y: T y( K
- process.destroy();// 销毁进程3 Q% s: e/ i+ u f
- } else if (OS.indexOf("linux")>=0) {// linux环境处理
6 I4 B# ~8 X) e% ? - Process process = r.exec(SWFTools_HOME+"/pdf2swf "+ pdfFilePath + " -o " + swfFileSavePath + " -T 9");
+ W: d, {* C2 Z- n& ^& T - System.out.print(loadStream(process.getInputStream()));
/ e5 E6 H* C7 S3 s9 P: u7 C) j - System.err.print(loadStream(process.getErrorStream()));
8 b# e6 l' ^6 I - System.err.println("****swf转换成功,文件输出:"+ swfFileSavePath + "****");1 u% a5 L+ A9 T0 ?
- process.destroy();// 销毁进程
/ B- D: ~% P+ [* @6 B$ v - }' D3 B2 w' \# C# a, R
- if (pdfFile.exists()) {% Y2 r- b% B8 g4 ? n- Q
- pdfFile.delete();//转换成功以后删除上传的pdf文件和转换的pdf文件。 Z% T% D) `3 Z, g7 Y
- }5 u+ D% x$ [, ]2 _ E% y( E
- returnMap.put("status", "true");9 H9 v: J! i. P0 V
- returnMap.put("swfFileName", swfFileSavePath.substring(swfFileSavePath.lastIndexOf("/")+1));//便于获取转换后swf文件的名字
3 O& d! K: O% t' T - } catch (IOException e) {
; e* @- }6 N5 ? - e.printStackTrace();8 Y* n3 n% R% j/ h
- returnMap.put("status", "false");
2 ~3 U' L1 h9 z" O1 ^" X - }, O# i# z& T" K2 H8 d6 m
- return returnMap;
! m( u* S2 j& R& p - } else {, h; S9 ^6 w. i3 |$ ~: m
- System.out.println("****pdf不存在,无法转换****");# j/ M3 M) |& X
- returnMap.put("status", "false");
S( K6 I8 i1 B4 ?6 [ - return returnMap;
5 \1 F0 K& V1 p4 D+ g - }
1 Q( \# c' g2 q5 S4 }, f! w% t7 r - }
复制代码 2 H% v) ~) |+ v- | H# ]6 o
四:运行项目测试项目前 请先运行安装好的OpenOffice、swftools
% ]% c- y5 P4 ?测试页面:5 f- F; n* k, V6 E" T# P# a' Z
0 |( u7 ~2 O; w8 X
0 H) z _1 R" K+ q9 o
5 k8 ^. N$ T6 ]. B
( g/ a3 Y1 n# s4 Z
* p W# I8 S5 Q2 {% p% ?7 i3 ~
8 g5 a. d) p5 ^. l
4 Q- ~( x+ k* e: J6 Q- n/ c
源码下载地址:点击下载
/ j4 ?+ K6 R* N& \; A% k/ _* z& @
0 v$ f$ ]0 T [ `9 p8 L! I9 `( b) v6 V/ \; a, W% }
软件下载地址:点击下载
. z: r$ p9 w R" b
: G5 r7 B2 O# Z* z+ Z- r* D1 V) q4 s3 U; s
|
|