在之前有做了一篇文章: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
这个程序处理的时间要长一点.....