1 packagecom.cectsims.util;2 importjava.io.BufferedInputStream;3 importjava.io.File;4 importjava.io.IOException;5 importjava.io.InputStream;6 importjava.util.Date;7
8 importcom.artofsolving.jodconverter.DocumentConverter;9 importcom.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;10 importcom.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;11 importcom.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;12
13 /**
14 * doc docx格式转换15 */
16 public classDocConverter {17 private static final int environment = 1;//环境 1:windows 2:linux
18 private String fileString;//(只涉及pdf2swf路径问题)
19 private String outputPath = "";//输入路径 ,如果不设置就输出在默认的位置
20 privateString fileName;21 privateFile pdfFile;22 privateFile swfFile;23 privateFile docFile;24
25 publicDocConverter(String fileString) {26 System.out.println("DocConverter:"+fileString);27 ini(fileString);28 }29 publicDocConverter(String fileString,String fileNames) {30 System.out.println("DocConverter:"+fileString);31 System.out.println("fileName:"+fileNames);32 ini(fileString,fileNames);33 }34 /**
35 * 重新设置file36 *37 *@paramfileString38 */
39 public voidsetFile(String fileString) {40 ini(fileString);41 }42
43 /**
44 * 初始化45 *46 *@paramfileString47 */
48 private voidini(String fileString) {49 this.fileString =fileString;50 fileName = fileString.substring(0, fileString.lastIndexOf("."));51 System.out.println("fileName:"+fileName);52 pdfFile = new File(fileName + ".pdf");53 swfFile = new File(fileName + ".swf");54 }55 private voidini(String fileString,String fileNames) {56 this.fileString =fileString;57 //fileName = fileNames.substring(0, fileString.lastIndexOf("."));
58 System.out.println("fileString:"+fileString);59 System.out.println("fileName:"+fileNames);60 String sj=new Date().getTime()+"";61 System.out.println("fileNamefileName:"+fileString+"/"+sj);62
63 docFile = new File(fileString+"/"+fileNames);64 pdfFile = new File(fileString+"/"+sj + ".pdf");65 swfFile = new File(fileString+"/"+sj + ".swf");66 }67 /**
68 * 转为PDF69 *70 *@paramfile71 */
72 private void doc2pdf() throwsException {73 if(docFile.exists()) {74 if (!pdfFile.exists()) {75 OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);76 try{77 connection.connect();78 DocumentConverter converter = newOpenOfficeDocumentConverter(connection);79 converter.convert(docFile, pdfFile);80 //close the connection
81 connection.disconnect();82 System.out.println("****pdf转换成功,PDF输出:" + pdfFile.getPath()+ "****");83 } catch(java.net.ConnectException e) {84 e.printStackTrace();85 System.out.println("****swf转换器异常,openoffice服务未启动!****");86 throwe;87 } catch(com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {88 e.printStackTrace();89 System.out.println("****swf转换器异常,读取转换文件失败****");90 throwe;91 } catch(Exception e) {92 e.printStackTrace();93 throwe;94 }95 } else{96 System.out.println("****已经转换为pdf,不需要再进行转化****");97 }98 } else{99 System.out.println("****swf转换器异常,需要转换的文档不存在,无法转换****");100 }101 }102
103 /**
104 * 转换成 swf105 */
106 private void pdf2swf() throwsException {107 Runtime r =Runtime.getRuntime();108 if (!swfFile.exists()) {109 if(pdfFile.exists()) {110 if (environment == 1) {//windows环境处理
111 try{112 Process p = r.exec("D:/SWFTools/pdf2swf.exe "+ pdfFile.getPath() + " -o "+ swfFile.getPath() + " -T 9");113 System.out.print("1"+loadStream(p.getInputStream()));114 System.err.print(loadStream(p.getErrorStream()));115 System.out.print("2"+loadStream(p.getInputStream()));116 System.err.println("****swf转换成功,文件输出:"
117 + swfFile.getPath() + "****");118 if(pdfFile.exists()) {119 pdfFile.delete();120 }121
122 } catch(IOException e) {123 e.printStackTrace();124 throwe;125 }126 } else if (environment == 2) {//linux环境处理
127 try{128 Process p = r.exec("pdf2swf " +pdfFile.getPath()129 + " -o " + swfFile.getPath() + " -T 9");130 System.out.print(loadStream(p.getInputStream()));131 System.err.print(loadStream(p.getErrorStream()));132 System.err.println("****swf转换成功,文件输出:"
133 + swfFile.getPath() + "****");134 if(pdfFile.exists()) {135 pdfFile.delete();136 }137 } catch(Exception e) {138 e.printStackTrace();139 throwe;140 }141 }142 } else{143 System.out.println("****pdf不存在,无法转换****");144 }145 } else{146 System.out.println("****swf已经存在不需要转换****");147 }148 }149
150 static String loadStream(InputStream in) throwsIOException {151
152 int ptr = 0;153 in = newBufferedInputStream(in);154 StringBuffer buffer = newStringBuffer();155
156 while ((ptr = in.read()) != -1) {157 buffer.append((char) ptr);158 }159
160 returnbuffer.toString();161 }162 /**
163 * 转换主方法164 */
165 public booleanconver() {166
167 if(swfFile.exists()) {168 System.out.println("****swf转换器开始工作,该文件已经转换为swf****");169 return true;170 }171
172 if (environment == 1) {173 System.out.println("****swf转换器开始工作,当前设置运行环境windows****");174 } else{175 System.out.println("****swf转换器开始工作,当前设置运行环境linux****");176 }177 try{178 doc2pdf();179 pdf2swf();180 } catch(Exception e) {181 e.printStackTrace();182 return false;183 }184
185 if(swfFile.exists()) {186 return true;187 } else{188 return false;189 }190 }191
192 /**
193 * 返回文件路径194 *195 *@params196 */
197 publicString getswfPath() {198 if(swfFile.exists()) {199 String tempString =swfFile.getPath();200 tempString = tempString.replaceAll("\\\\", "/");201 returntempString;202 } else{203 return "";204 }205
206 }207 /**
208 * 设置输出路径209 */
210 public voidsetOutputPath(String outputPath) {211 this.outputPath =outputPath;212 if (!outputPath.equals("")) {213 String realName = fileName.substring(fileName.lastIndexOf("/"),214 fileName.lastIndexOf("."));215 if (outputPath.charAt(outputPath.length()) == '/') {216 swfFile = new File(outputPath + realName + ".swf");217 } else{218 swfFile = new File(outputPath + realName + ".swf");219 }220 }221 }222
223 }