java開發_模仿百度文庫_SWFTools_源碼下載


在之前有做了一篇文章:java開發_模仿百度文庫_OpenOffice2PDF_源碼下載

今天做第二步:PDF(OpenOffice+JodConverter)=>SWF(pdf2swf)

做之前,我們也要先做一些准備:

1.下載SWFTools

下載地址http://www.swftools.org/download.html

我下載的是:swftools-2012-10-15-1307.exe

2.安裝SWFTools

注意:這里的是我電腦的SWFTools安裝目,因為程序中需要用到....所以這里需要注意一下..

到這里,我們就安裝完成啦...

3.新建一個java project

/pdf2swf/src/com/b510/pdf2swf/PDF2SWF.java

 

  1 /**
  2  * 
  3  */
  4 package com.b510.pdf2swf;
  5 
  6 import java.io.BufferedReader;
  7 import java.io.File;
  8 import java.io.IOException;
  9 import java.io.InputStream;
 10 import java.io.InputStreamReader;
 11 import java.util.Date;
 12 
 13 /**
 14  * PDF轉SWF工具
 15  * 
 16  * @date 2012-11-5
 17  * @author xhw
 18  * 
 19  */
 20 public class PDF2SWF {
 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      * @param file
 39      *            文件的路徑 ,如:"c:/test/test.swf"
 40      * @return 文件的路徑
 41      */
 42     public static String 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         return result;
 50     }
 51 
 52     /**
 53      * 新建一個目錄
 54      * 
 55      * @param folderPath
 56      *            新建目錄的路徑 如:"c:\\newFolder"
 57      */
 58     public static void newFolder(String folderPath) {
 59         try {
 60             File myFolderPath = new File(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. By
 72      * convention, the value 0 indicates normal termination.
 73      * 
 74      * @param sourcePath
 75      *            pdf文件路徑 ,如:"c:/hello.pdf"
 76      * @param destPath
 77      *            swf文件路徑,如:"c:/test/test.swf"
 78      * @return 正常情況下返回:0,失敗情況返回:1
 79      * @throws IOException
 80      */
 81     public static int convertPDF2SWF(String sourcePath, String destPath) throws IOException {
 82         // 如果目標文件的路徑是新的,則新建路徑
 83         newFolder(getFilePath(destPath));
 84 
 85         // 源文件不存在則返回
 86         File source = new File(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(new Runnable() {
 98             public void run() {
 99                 BufferedReader br = new BufferedReader(new InputStreamReader(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(new InputStreamReader(is2));
110         // 保存輸出結果流
111         StringBuilder buf = new StringBuilder();
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         return process.exitValue();
125     }
126 
127     /**
128      * pdf文件轉換為swf文件操作
129      * 
130      * @param sourcePath
131      *            pdf文件路徑 ,如:"c:/hello.pdf"
132      * @param destPath
133      *            swf文件路徑,如:"c:/test/test.swf"
134      */
135     public static void pdf2swf(String sourcePath, String destPath) {
136         long begin_time = new Date().getTime();
137         try {
138             PDF2SWF.convertPDF2SWF(sourcePath, destPath);
139         } catch (Exception ex) {
140             System.out.println("轉換過程失敗!!");
141         }
142         long end_time = new Date().getTime();
143         System.out.println("轉換共耗時 :[" + (end_time - begin_time) + "]ms");
144         System.out.println("轉換文件成功!!");
145     }
146 
147     public static void main(String[] args) throws IOException {
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 }

 

4.運行效果:

5.后台運行情況

1 命令操作:C:/Program Files (x86)/SWFTools/pdf2swf.exe  -t "e:/test_1352107155307.pdf" -o  "e:/hello/test_1352107155307_1352171476399.swf" -s flashversion=9 -s languagedir=D:\xpdf\xpdf-chinese-simplified 
2 開始轉換...
3 轉換結束...
4 轉換共耗時 :[1226]ms
5 轉換文件成功!!

====================================================================

/pdf2swf/src/com/b510/pdf2swf/ConvertToSwf.java

  1 /**
  2  * 
  3  */
  4 package com.b510.pdf2swf;
  5 
  6 import java.io.BufferedReader;
  7 import java.io.File;
  8 import java.io.IOException;
  9 import java.io.InputStreamReader;
 10 import java.util.ArrayList;
 11 import java.util.Date;
 12 import java.util.List;
 13 
 14  
 15 /**
 16  * @author nlb
 17  * @version 1.0 把pdf,jpeg,font,gif,pgn,wav轉化為swf文件
 18  */ 
 19 public class ConvertToSwf { 
 20  
 21     private final String CONVERTFILETYPE = "pdf,jpg,jpeg,font,gif,png,wav"; 
 22     private String swftoolsPath; 
 23     /**
 24      * @param swftoolsPath
 25      *            用於進行把文件轉化為swf的工具地址
 26      */ 
 27     public ConvertToSwf(String swftoolsPath) { 
 28         this.swftoolsPath=swftoolsPath; 
 29     } 
 30     /**
 31      * 把文件轉化為swf格式支持"pdf,jpg,jpeg,font,gif,png,wav"
 32      * 
 33      * @param sourceFilePath
 34      *            要進行轉化為swf文件的地址
 35      * @param swfFilePath
 36      *            轉化后的swf的文件地址
 37      * @return
 38      */ 
 39     public boolean convertFileToSwf(String sourceFilePath, String swfFilePath) { 
 40         System.out.println("開始轉化文件到swf格式");
 41         if (swftoolsPath == null || swftoolsPath == "") { 
 42            System.out.println("未指定要進行swf轉化工具的地址!!!");
 43             return false; 
 44         } 
 45         String filetype = sourceFilePath.substring(sourceFilePath 
 46                 .lastIndexOf(".") + 1); 
 47         // 判讀上傳文件類型是否符合轉換為pdf 
 48        System.out.println("判斷文件類型通過");
 49         if (CONVERTFILETYPE.indexOf(filetype.toLowerCase()) == -1) { 
 50             System.out.println("當前文件不符合要轉化為SWF的文件類型!!!");
 51             return false; 
 52         } 
 53         File sourceFile = new File(sourceFilePath); 
 54  
 55         if (!sourceFile.exists()) { 
 56             System.out.println("要進行swf的文件不存在!!!");
 57             return false; 
 58         } 
 59         System.out.println("准備轉換的文件路徑存在");
 60         if (!swftoolsPath.endsWith(File.separator)) { 
 61             swftoolsPath += File.separator; 
 62         } 
 63         StringBuilder commandBuidler = new StringBuilder(swftoolsPath); 
 64         File swfFile = new File(swfFilePath); 
 65         if (!swfFile.getParentFile().exists()) { 
 66             swfFile.getParentFile().mkdirs(); 
 67         } 
 68         if (filetype.toLowerCase().equals("jpg")) { 
 69             filetype = "jpeg"; 
 70         } 
 71         List<String>  command = new   ArrayList<String>();   
 72             command.add(this.swftoolsPath+"\\"+filetype.toLowerCase()+"2swf.exe");//從配置文件里讀取     
 73             command.add("-z");     
 74             command.add("-s");     
 75             command.add("flashversion=9");     
 76             command.add("-s");     
 77             command.add("poly2bitmap");//加入poly2bitmap的目的是為了防止出現大文件或圖形過多的文件轉換時的出錯,沒有生成swf文件的異常     
 78             command.add(sourceFilePath);     
 79             command.add("-o");     
 80             command.add(swfFilePath);     
 81         try { 
 82             ProcessBuilder processBuilder = new ProcessBuilder();     
 83             processBuilder.command(command);     
 84             Process process = processBuilder.start();     
 85             System.out.println("開始生成swf文件..");
 86             dealWith(process); 
 87             try {     
 88                 process.waitFor();//等待子進程的結束,子進程就是系統調用文件轉換這個新進程     
 89             } catch (InterruptedException e) {     
 90                 e.printStackTrace();     
 91             }     
 92             File swf = new File(swfFilePath); 
 93             if (!swf.exists()) { 
 94                 return false; 
 95             } 
 96             System.out.println("轉化SWF文件成功!!!");
 97         } catch (IOException e) { 
 98             // TODO Auto-generated catch block 
 99             System.out.println("轉化為SWF文件失敗!!!");
100             e.printStackTrace(); 
101             return false; 
102         } 
103  
104         return true; 
105     } 
106     public static void main(String[] args) { 
107         ConvertToSwf a=new ConvertToSwf("C:/Program Files (x86)/SWFTools"); 
108         long begin_time=new Date().getTime();
109         a.convertFileToSwf("e:/test_1352107155307.pdf", "e:/test_1352107155307.swf"); 
110         long end_time=new Date().getTime();
111         System.out.println("result:"+(end_time-begin_time));
112     } 
113     private void dealWith(final Process pro){     
114         // 下面是處理堵塞的情況     
115         try {     
116             new Thread(){     
117                 public void run(){     
118                     BufferedReader br1 = new BufferedReader(new InputStreamReader(pro.getInputStream()));     
119                     String text;     
120                     try {     
121                         while ( (text = br1.readLine()) != null) {     
122                             System.out.println(text);     
123                         }     
124                     } catch (IOException e) {     
125                         e.printStackTrace();     
126                     }     
127                 }     
128             }.start();     
129         } catch (Exception e) {     
130             e.printStackTrace();     
131         }     
132         try {     
133             new Thread(){     
134                 public void run(){     
135                     BufferedReader br2 = new BufferedReader(new InputStreamReader(pro.getErrorStream()));//這定不要忘記處理出理時產生的信息,不然會堵塞不前的     
136                     String text;     
137                     try {     
138                         while( (text = br2.readLine()) != null){     
139                             System.err.println(text);     
140                         }     
141                     } catch (IOException e) {     
142                         e.printStackTrace();     
143                     }     
144                 }     
145             }.start();     
146         } catch (Exception e) {     
147             e.printStackTrace();     
148         }     
149     }     
150 } 

效果是一樣的,不過后台的運行情況不同:

1 開始轉化文件到swf格式
2 判斷文件類型通過
3 准備轉換的文件路徑存在
4 開始生成swf文件..
5 NOTICE  File contains links
6 NOTICE  Writing SWF file e:/test_1352107155307.swf
7 轉化SWF文件成功!!!
8 result:4131

這個程序處理的時間要長一點.....

源碼下載地址:http://files.cnblogs.com/hongten/pdf2swf.rar


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM