您的位置:首页 > 编程语言 > Java开发

java开发_模仿百度文库_SWFTools_源码下载

2012-11-06 11:30 441 查看
在之前有做了一篇文章: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

/**
*
*/
package com.b510.pdf2swf;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Date;

/**
* PDF转SWF工具
*
* @date 2012-11-5
* @author xhw
*
*/
public class PDF2SWF {

/**
* SWTOOLS的安装路径,我的SWFTools安装目录为:"C:/Program Files (x86)/SWFTools"
*/
public static final String SWFTOOLS_PATH = "C:/Program Files (x86)/SWFTools";
/**
* pdf文件后缀名
*/
public static final String FILE_NAME_OF_PDF = "pdf";
/**
* swf文件后缀名
*/
public static final String FILE_NAME_OF_SWF = "swf";

/**
* 获得文件的路径
*
* @param file
*            文件的路径 ,如:"c:/test/test.swf"
* @return 文件的路径
*/
public static String getFilePath(String file) {
String result = file.substring(0, file.lastIndexOf("/"));
if (file.substring(2, 3) == "/") {
result = file.substring(0, file.lastIndexOf("/"));
} else if (file.substring(2, 3) == "\\") {
result = file.substring(0, file.lastIndexOf("\\"));
}
return result;
}

/**
* 新建一个目录
*
* @param folderPath
*            新建目录的路径 如:"c:\\newFolder"
*/
public static void newFolder(String folderPath) {
try {
File myFolderPath = new File(folderPath.toString());
if (!myFolderPath.exists()) {
myFolderPath.mkdir();
}
} catch (Exception e) {
System.out.println("新建目录操作出错");
e.printStackTrace();
}
}

/**
* the exit value of the subprocess represented by this Process object. By
* convention, the value 0 indicates normal termination.
*
* @param sourcePath
*            pdf文件路径 ,如:"c:/hello.pdf"
* @param destPath
*            swf文件路径,如:"c:/test/test.swf"
* @return 正常情况下返回:0,失败情况返回:1
* @throws IOException
*/
public static int convertPDF2SWF(String sourcePath, String destPath) throws IOException {
// 如果目标文件的路径是新的,则新建路径
newFolder(getFilePath(destPath));

// 源文件不存在则返回
File source = new File(sourcePath);
if (!source.exists()) {
return 0;
}

// 调用pdf2swf命令进行转换
String command = SWFTOOLS_PATH + "/pdf2swf.exe  -t \"" + sourcePath + "\" -o  \"" + destPath + "\" -s flashversion=9 -s languagedir=D:\\xpdf\\xpdf-chinese-simplified ";
System.out.println("命令操作:" + command + "\n开始转换...");
// 调用外部程序
Process process = Runtime.getRuntime().exec(command);
final InputStream is1 = process.getInputStream();
new Thread(new Runnable() {
public void run() {
BufferedReader br = new BufferedReader(new InputStreamReader(is1));
try {
while (br.readLine() != null)
;
} catch (IOException e) {
e.printStackTrace();
}
}
}).start(); // 启动单独的线程来清空process.getInputStream()的缓冲区
InputStream is2 = process.getErrorStream();
BufferedReader br2 = new BufferedReader(new InputStreamReader(is2));
// 保存输出结果流
StringBuilder buf = new StringBuilder();
String line = null;
while ((line = br2.readLine()) != null)
// 循环等待ffmpeg进程结束
buf.append(line);
while (br2.readLine() != null)
;
try {
process.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("转换结束...");
return process.exitValue();
}

/**
* pdf文件转换为swf文件操作
*
* @param sourcePath
*            pdf文件路径 ,如:"c:/hello.pdf"
* @param destPath
*            swf文件路径,如:"c:/test/test.swf"
*/
public static void pdf2swf(String sourcePath, String destPath) {
long begin_time = new Date().getTime();
try {
PDF2SWF.convertPDF2SWF(sourcePath, destPath);
} catch (Exception ex) {
System.out.println("转换过程失败!!");
}
long end_time = new Date().getTime();
System.out.println("转换共耗时 :[" + (end_time - begin_time) + "]ms");
System.out.println("转换文件成功!!");
}

public static void main(String[] args) throws IOException {
String sourcePath = "e:/test_1352107155307." + FILE_NAME_OF_PDF;
String destPath = "e:/hello/test_1352107155307_" + new Date().getTime() + "." + FILE_NAME_OF_SWF;
pdf2swf(sourcePath, destPath);
}
}


4.运行效果:





5.后台运行情况

命令操作: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
开始转换...
转换结束...
转换共耗时 :[1226]ms
转换文件成功!!


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

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

/**
*
*/
package com.b510.pdf2swf;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author nlb
* @version 1.0 把pdf,jpeg,font,gif,pgn,wav转化为swf文件
*/
public class ConvertToSwf {

private final String CONVERTFILETYPE = "pdf,jpg,jpeg,font,gif,png,wav";
private String swftoolsPath;
/**
* @param swftoolsPath
*            用于进行把文件转化为swf的工具地址
*/
public ConvertToSwf(String swftoolsPath) {
this.swftoolsPath=swftoolsPath;
}
/**
* 把文件转化为swf格式支持"pdf,jpg,jpeg,font,gif,png,wav"
*
* @param sourceFilePath
*            要进行转化为swf文件的地址
* @param swfFilePath
*            转化后的swf的文件地址
* @return
*/
public boolean convertFileToSwf(String sourceFilePath, String swfFilePath) {
System.out.println("开始转化文件到swf格式");
if (swftoolsPath == null || swftoolsPath == "") {
System.out.println("未指定要进行swf转化工具的地址!!!");
return false;
}
String filetype = sourceFilePath.substring(sourceFilePath
.lastIndexOf(".") + 1);
// 判读上传文件类型是否符合转换为pdf
System.out.println("判断文件类型通过");
if (CONVERTFILETYPE.indexOf(filetype.toLowerCase()) == -1) {
System.out.println("当前文件不符合要转化为SWF的文件类型!!!");
return false;
}
File sourceFile = new File(sourceFilePath);

if (!sourceFile.exists()) {
System.out.println("要进行swf的文件不存在!!!");
return false;
}
System.out.println("准备转换的文件路径存在");
if (!swftoolsPath.endsWith(File.separator)) {
swftoolsPath += File.separator;
}
StringBuilder commandBuidler = new StringBuilder(swftoolsPath);
File swfFile = new File(swfFilePath);
if (!swfFile.getParentFile().exists()) {
swfFile.getParentFile().mkdirs();
}
if (filetype.toLowerCase().equals("jpg")) {
filetype = "jpeg";
}
List<String>  command = new   ArrayList<String>();
command.add(this.swftoolsPath+"\\"+filetype.toLowerCase()+"2swf.exe");//从配置文件里读取
command.add("-z");
command.add("-s");
command.add("flashversion=9");
command.add("-s");
command.add("poly2bitmap");//加入poly2bitmap的目的是为了防止出现大文件或图形过多的文件转换时的出错,没有生成swf文件的异常
command.add(sourceFilePath);
command.add("-o");
command.add(swfFilePath);
try {
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command(command);
Process process = processBuilder.start();
System.out.println("开始生成swf文件..");
dealWith(process);
try {
process.waitFor();//等待子进程的结束,子进程就是系统调用文件转换这个新进程
} catch (InterruptedException e) {
e.printStackTrace();
}
File swf = new File(swfFilePath);
if (!swf.exists()) {
return false;
}
System.out.println("转化SWF文件成功!!!");
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("转化为SWF文件失败!!!");
e.printStackTrace();
return false;
}

return true;
}
public static void main(String[] args) {
ConvertToSwf a=new ConvertToSwf("C:/Program Files (x86)/SWFTools");
long begin_time=new Date().getTime();
a.convertFileToSwf("e:/test_1352107155307.pdf", "e:/test_1352107155307.swf");
long end_time=new Date().getTime();
System.out.println("result:"+(end_time-begin_time));
}
private void dealWith(final Process pro){
// 下面是处理堵塞的情况
try {
new Thread(){
public void run(){
BufferedReader br1 = new BufferedReader(new InputStreamReader(pro.getInputStream()));
String text;
try {
while ( (text = br1.readLine()) != null) {
System.out.println(text);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
} catch (Exception e) {
e.printStackTrace();
}
try {
new Thread(){
public void run(){
BufferedReader br2 = new BufferedReader(new InputStreamReader(pro.getErrorStream()));//这定不要忘记处理出理时产生的信息,不然会堵塞不前的
String text;
try {
while( (text = br2.readLine()) != null){
System.err.println(text);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}


效果是一样的,不过后台的运行情况不同:

开始转化文件到swf格式
判断文件类型通过
准备转换的文件路径存在
开始生成swf文件..
NOTICE  File contains links
NOTICE  Writing SWF file e:/test_1352107155307.swf
转化SWF文件成功!!!
result:4131


这个程序处理的时间要长一点.....

源码下载地址:http://files.cnblogs.com/hongten/pdf2swf.rar
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: