java 仿百度文库源码_java开发_模仿百度文库_SWFTools_源码下载
1 /**
2 *3 */
4 packagecom.b510.pdf2swf;5
6 importjava.io.BufferedReader;7 importjava.io.File;8 importjava.io.IOException;9 importjava.io.InputStream;10 importjava.io.InputStreamReader;11 importjava.util.Date;12
13 /**
14 * PDF轉SWF工具15 *16 * @date 2012-11-517 *@authorxhw18 *19 */
20 public classPDF2SWF {21
22 /**
23 * SWTOOLS的安裝路徑,我的SWFTools安裝目錄為:"C:/Program Files (x86)/SWFTools"24 */
25 public static final String SWFTOOLS_PATH = "C:/Program Files (x86)/SWFTools";26 /**
27 * pdf文件后綴名28 */
29 public static final String FILE_NAME_OF_PDF = "pdf";30 /**
31 * swf文件后綴名32 */
33 public static final String FILE_NAME_OF_SWF = "swf";34
35 /**
36 * 獲得文件的路徑37 *38 *@paramfile39 * 文件的路徑 ,如:"c:/test/test.swf"40 *@return文件的路徑41 */
42 public staticString getFilePath(String file) {43 String result = file.substring(0, file.lastIndexOf("/"));44 if (file.substring(2, 3) == "/") {45 result = file.substring(0, file.lastIndexOf("/"));46 } else if (file.substring(2, 3) == "\\") {47 result = file.substring(0, file.lastIndexOf("\\"));48 }49 returnresult;50 }51
52 /**
53 * 新建一個目錄54 *55 *@paramfolderPath56 * 新建目錄的路徑 如:"c:\\newFolder"57 */
58 public static voidnewFolder(String folderPath) {59 try{60 File myFolderPath = newFile(folderPath.toString());61 if (!myFolderPath.exists()) {62 myFolderPath.mkdir();63 }64 } catch(Exception e) {65 System.out.println("新建目錄操作出錯");66 e.printStackTrace();67 }68 }69
70 /**
71 * the exit value of the subprocess represented by this Process object. By72 * convention, the value 0 indicates normal termination.73 *74 *@paramsourcePath75 * pdf文件路徑 ,如:"c:/hello.pdf"76 *@paramdestPath77 * swf文件路徑,如:"c:/test/test.swf"78 *@return正常情況下返回:0,失敗情況返回:179 *@throwsIOException80 */
81 public static int convertPDF2SWF(String sourcePath, String destPath) throwsIOException {82 //如果目標文件的路徑是新的,則新建路徑
83 newFolder(getFilePath(destPath));84
85 //源文件不存在則返回
86 File source = newFile(sourcePath);87 if (!source.exists()) {88 return 0;89 }90
91 //調用pdf2swf命令進行轉換
92 String command = SWFTOOLS_PATH + "/pdf2swf.exe -t \"" + sourcePath + "\" -o \"" + destPath + "\" -s flashversion=9 -s languagedir=D:\\xpdf\\xpdf-chinese-simplified ";93 System.out.println("命令操作:" + command + "\n開始轉換...");94 //調用外部程序
95 Process process =Runtime.getRuntime().exec(command);96 final InputStream is1 =process.getInputStream();97 new Thread(newRunnable() {98 public voidrun() {99 BufferedReader br = new BufferedReader(newInputStreamReader(is1));100 try{101 while (br.readLine() != null)102 ;103 } catch(IOException e) {104 e.printStackTrace();105 }106 }107 }).start(); //啟動單獨的線程來清空process.getInputStream()的緩沖區
108 InputStream is2 =process.getErrorStream();109 BufferedReader br2 = new BufferedReader(newInputStreamReader(is2));110 //保存輸出結果流
111 StringBuilder buf = newStringBuilder();112 String line = null;113 while ((line = br2.readLine()) != null)114 //循環等待ffmpeg進程結束
115 buf.append(line);116 while (br2.readLine() != null)117 ;118 try{119 process.waitFor();120 } catch(InterruptedException e) {121 e.printStackTrace();122 }123 System.out.println("轉換結束...");124 returnprocess.exitValue();125 }126
127 /**
128 * pdf文件轉換為swf文件操作129 *130 *@paramsourcePath131 * pdf文件路徑 ,如:"c:/hello.pdf"132 *@paramdestPath133 * swf文件路徑,如:"c:/test/test.swf"134 */
135 public static voidpdf2swf(String sourcePath, String destPath) {136 long begin_time = newDate().getTime();137 try{138 PDF2SWF.convertPDF2SWF(sourcePath, destPath);139 } catch(Exception ex) {140 System.out.println("轉換過程失敗!!");141 }142 long end_time = newDate().getTime();143 System.out.println("轉換共耗時 :[" + (end_time - begin_time) + "]ms");144 System.out.println("轉換文件成功!!");145 }146
147 public static void main(String[] args) throwsIOException {148 String sourcePath = "e:/test_1352107155307." +FILE_NAME_OF_PDF;149 String destPath = "e:/hello/test_1352107155307_" + new Date().getTime() + "." +FILE_NAME_OF_SWF;150 pdf2swf(sourcePath, destPath);151 }152 }
總結
以上是生活随笔為你收集整理的java 仿百度文库源码_java开发_模仿百度文库_SWFTools_源码下载的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: solr java api_solr j
- 下一篇: java gridlayout 设置列宽