TA的每日心情 | 衰 2021-2-2 11:21 |
|---|
签到天数: 36 天 [LV.5]常住居民I
|
一:测试平台 win7 64位系统# \2 C5 X* c2 \
二:准备软件 OpenOffice、swftools* r; e) P! r2 e+ y7 D5 A
OpenOffice 下载地址:http://www.openoffice.org/download/index.html
" Y. }6 ~' d r1 f% Aswftools下载地址:http://www.swftools.org/download.html
' P( O* {7 l+ o+ y三:测试代码
9 Z1 j: O5 g/ M, P1、conf.properties 软软件路径配置$ E) W+ Y E5 o) D
- OpenOffice_HOME = C:/Program Files (x86)/OpenOffice 4 m8 H9 n& `0 i4 P
. [$ e8 c% U" n; N) [* u' @( b- SWFTools_HOME = D:/SWF/SWFTools
复制代码 第一行 是OpenOffice 的安装路径 安装的时候请记住。! F( }; d9 ^1 P1 |( G
第二行 是swftools的安装路径。3 H) d' z8 _" _" I: T3 S
你可以自定义安装路径最好放到一起、便于管理。
* U/ f% b8 Z/ G" S2、文档转换
8 [" H5 Q3 k! f; J8 F- /**
/ y4 d$ \8 \- h% T; B3 }* d - * 将odt、doc、docx、ppt或pptx格式的文件转换为pdf文件,如果文件后缀是pdf,则直接返回true。
" [8 z, A0 P# } - * & p5 _' s, J1 t% V6 U
- * fileSuffix 文件的后缀。
% Q8 o# a- P/ E+ y' x - */5 d# H& A% Q* c/ M
- private boolean converToPdf(String sourceFilePath, String pdfFileSavePath){
1 U5 j- w; N) T' n- x+ z4 J' g. k* D: L5 b - File sourceFile = new File(sourceFilePath);$ D/ c$ t+ \ v+ F
- if (sourceFile.exists()) {5 `& i( a6 p) M6 H/ p5 v
- String OpenOffice_HOME = ConfProperties.getOpenOfficeHome();//OpenOffice的安装根目录
4 }/ ?" q9 D( R+ S: ?# F - String command = OpenOffice_HOME + "/program/soffice.exe -headless -accept="socket,host=127.0.0.1,port=8100;urp;"";/ K6 x$ c7 a) q
- System.out.println("command"+command);6 _) ]) }8 _) p3 ?
- try {
3 y6 |* B9 k1 r+ b* P1 e. J - Process process = Runtime.getRuntime().exec(command);//启动OpenOffice的服务
3 ^3 s9 D3 b; l - OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100); c: Q7 m0 ?5 e; f
- connection.connect();) D) s/ C7 \( u4 a1 r0 ]
- if(connection.isConnected()){//如果连接成功
M* [1 d! i; Y0 q* \ - DocumentConverter converter = new OpenOfficeDocumentConverter(connection);+ k. O5 ?. I8 D) C
- converter.convert(sourceFile, new File(pdfFileSavePath));5 w- i8 N7 z& U9 d3 |) [% d
- connection.disconnect();// close the connection9 e# e2 h% y# B
- process.destroy();// 封闭OpenOffice服务的进程
7 `% G9 A' w9 w+ ~$ t/ c2 |0 R' k - System.out.println("****pdf转换成功,PDF输出:" + pdfFileSavePath+ "****");
0 }/ l. A5 A' i+ W a4 w0 g2 _ - return true;( L4 o( H; H+ @' d4 e3 \) O" A
- }else{- K- M0 d( O( g! G. Q. d
- return false;//swf转换器异常,openoffice连接失败!
# ?; l# m0 G8 j0 Z: g- { - }+ S& b0 {- E3 k/ l
- } catch (ConnectException e) {+ ]- ]" k+ J' V' S4 ^
- e.printStackTrace();& L+ v4 c2 H; r6 U& o( f1 Z0 W4 {) g1 J
- System.out.println("****swf转换器异常,openoffice服务未启动!****");+ o( H" x' w3 t0 o k! X, Q
- return false;
7 {* f7 B( S; n- y4 F- ]" `% ^ - } catch (OpenOfficeException e) {& `6 F& S( o$ ~( D
- e.printStackTrace();
6 s6 k+ t0 l# m7 S, k1 L2 `/ n" W - System.out.println("****swf转换器异常,读取转换文件失败****");( E; B% n8 E$ f+ _4 c9 N7 ^+ V
- return false;
) Y* u3 Y; j' g - } catch (Exception e) {# `1 V# v4 |5 p+ r, t* l
- e.printStackTrace();
: Z' A0 A7 P: x q, g# P& [1 @- L - return false;
/ m6 k4 a. r7 _5 @( y - }
3 e. O+ K2 G+ q' ` - } else {
2 |6 A3 ]% u) i2 H" d x - System.out.println("****swf转换器异常,需要转换的文档不存在,无法转换****");
' p2 }1 z. r" |" C6 c$ O - return false;
* _7 i- ^% ^# d. h: j6 X6 V& u - }
( l; I) d3 z$ R4 o* @ - }
复制代码 将pdf文件转换为swf文件:; K# ^% v/ C( A: L+ e/ S2 j' I
- /**. M3 I2 N* f# \# i
- * 将pdf文件转换为swf文件。% ?# o" q% A; `" k/ u4 c
- *
; h9 D/ V( P- x/ g( i5 f - * @author ghj
# C* D _3 g7 W4 _5 {* X1 n8 M - */# N- M/ k/ p! r
- private Map<String, String> converFromPdfToSwf(String pdfFilePath,String swfFileSavePath){
. l9 _& W' O& B- l - String OS = System.getProperty("os.name").toLowerCase(); - x( ~1 G& v- w( p) k0 z
- Map<String, String> returnMap = new HashMap<String, String>();
! S( S3 A8 y0 A1 Q - File pdfFile = new File(pdfFilePath);1 j& f5 Y# Z0 j; U) S6 A/ t4 m5 r' h
- Runtime r = Runtime.getRuntime();
! b; \! H/ c5 {$ I5 g9 L3 @ - if (pdfFile.exists()) {
, G9 E$ N5 E9 a - String SWFTools_HOME = ConfProperties.getSWFToolsHome();// SWFTools的安装根目录
" r5 P# f1 ?5 l - try {
& k% N) p! b% M: e7 j! `6 i5 V# C - if (OS.indexOf("windows")>=0) {// windows环境处理4 {8 X# g E# Q' G, H
- Process process = r.exec(SWFTools_HOME+"/pdf2swf.exe "+ pdfFilePath + " -s flashversion=9 -o "+ swfFileSavePath);' K+ o8 `: v6 I% x1 E) M P
- System.out.print(loadStream(process.getInputStream()));
; @% Y; L5 J. j' Z1 F# y - System.err.print(loadStream(process.getErrorStream()));
0 r" z+ j% S3 c' Q+ d# q9 X - System.out.print(loadStream(process.getInputStream()));+ e7 Y, Q! L' k4 N& ^' b5 M
- System.err.println("****swf转换成功,文件输出:"+ swfFileSavePath + "****");1 n2 p5 a' _5 o0 s* I' _* d2 _
- process.destroy();// 销毁进程
9 l4 Y" a- X9 H' z8 N- @ - } else if (OS.indexOf("linux")>=0) {// linux环境处理- r/ a1 f1 J% _
- Process process = r.exec(SWFTools_HOME+"/pdf2swf "+ pdfFilePath + " -o " + swfFileSavePath + " -T 9");
% x1 V; I( R& Z _ ~& \% G - System.out.print(loadStream(process.getInputStream()));6 c( p6 o* [5 D5 g1 q
- System.err.print(loadStream(process.getErrorStream()));
. n9 A% w/ S- ]: ]# P - System.err.println("****swf转换成功,文件输出:"+ swfFileSavePath + "****");
' R1 ^3 r; n7 y" l+ c: y$ w - process.destroy();// 销毁进程# K1 ]' ?- a: x# @4 w. E
- }
K6 N2 E: X( L6 d. ~+ [ - if (pdfFile.exists()) {9 Y- ^5 s% Z5 V- e8 y0 u2 C. `) C
- pdfFile.delete();//转换成功以后删除上传的pdf文件和转换的pdf文件。
4 l l2 X5 r% U! F. w8 t - }7 v& W' T9 J) T3 W% P5 n/ Z1 O0 m
- returnMap.put("status", "true");6 n J' ~1 q; }! j2 f j4 y. Q5 S
- returnMap.put("swfFileName", swfFileSavePath.substring(swfFileSavePath.lastIndexOf("/")+1));//便于获取转换后swf文件的名字- H0 y$ ^+ y/ p; \- ?
- } catch (IOException e) {
8 d" u& O6 e) |+ F - e.printStackTrace();
& |9 Z0 ?3 k- j' b& e - returnMap.put("status", "false");
& M0 {( {* r8 s0 \/ _1 J3 \5 y4 V - }
/ @: Y( O6 y: ]( m$ @ - return returnMap;
( h9 R6 K+ L. q0 f( |0 ` - } else {
. x& O2 O: c+ T* _5 v- Q ]: J; H6 u - System.out.println("****pdf不存在,无法转换****");# t5 p$ N6 e% c. ?
- returnMap.put("status", "false");$ r5 o& a; U8 `( l6 _# n$ c
- return returnMap;
- M; o( p' _8 g/ ^# V - }* i7 [ v* P! T/ ^7 |& K
- }
复制代码
! m a4 [2 ]5 K1 F2 z& E四:运行项目测试项目前 请先运行安装好的OpenOffice、swftools8 ]- r1 k! ~: Q7 S
测试页面:! W) C. Q. d4 |0 n' W/ O: ?2 \% a
4 q& [% k+ k0 K2 A8 ^, \
" ?7 |0 X" d. l7 B9 e- Q( ~
9 A6 A% ^* n0 k5 D6 P" }6 J7 w8 Q7 W9 R( N
0 L+ g/ B) c- p3 v) q- S1 e% L) P: _9 `% G# v
2 r! o1 r8 s& q! P
源码下载地址:点击下载: D1 ~& a- J" e! ?
( x, u. h4 G/ S8 u B
7 t. }* t4 O) g& X软件下载地址:点击下载
5 s5 {, ^7 _, R, J. n0 _" g2 h1 L2 c
* ^& z ~9 l6 v9 g; y9 H. g% q3 F |
|