java仿百度文库-使用SWFTools转换pdf文件

java仿百度文库-使用SWFTools转换pdf文件

分类: 数据和算法 68人阅读 评论(0) 收藏 举报
  1. /** 
  2.  *  
  3.  */  
  4. package com.zxjxw.framework.util;  
  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.ArrayList;  
  12. import java.util.List;  
  13.   
  14. import org.apache.commons.logging.Log;  
  15. import org.apache.commons.logging.LogFactory;  
  16.   
  17. /** 
  18.  * @author nlb 
  19.  * @version 1.0 把pdf,jpeg,font,gif,pgn,wav转化为swf文件 
  20.  */  
  21. public class ConvertToSwf {  
  22.   
  23.     Log log = LogFactory.getLog(ConvertToSwf.class);  
  24.     private final String CONVERTFILETYPE = "pdf,jpg,jpeg,font,gif,png,wav";  
  25.     private String swftoolsPath;  
  26.     /** 
  27.      * @param swftoolsPath 
  28.      *            用于进行把文件转化为swf的工具地址 
  29.      */  
  30.     public ConvertToSwf(String swftoolsPath) {  
  31.         this.swftoolsPath=swftoolsPath;  
  32.     }  
  33.     /** 
  34.      * 把文件转化为swf格式支持"pdf,jpg,jpeg,font,gif,png,wav" 
  35.      *  
  36.      * @param sourceFilePath 
  37.      *            要进行转化为swf文件的地址 
  38.      * @param swfFilePath 
  39.      *            转化后的swf的文件地址 
  40.      * @return 
  41.      */  
  42.     public boolean convertFileToSwf(String sourceFilePath, String swfFilePath) {  
  43.         log.info("开始转化文件到swf格式");  
  44.         if (swftoolsPath == null || swftoolsPath == "") {  
  45.             if (log.isWarnEnabled()) {  
  46.                 log.warn("未指定要进行swf转化工具的地址!!!");  
  47.             }  
  48.             return false;  
  49.         }  
  50.         String filetype = sourceFilePath.substring(sourceFilePath  
  51.                 .lastIndexOf(".") + 1);  
  52.         // 判读上传文件类型是否符合转换为pdf  
  53.         log.info("判断文件类型通过");  
  54.         if (CONVERTFILETYPE.indexOf(filetype.toLowerCase()) == -1) {  
  55.             if (log.isWarnEnabled()) {  
  56.                 log.warn("当前文件不符合要转化为SWF的文件类型!!!");  
  57.             }  
  58.             return false;  
  59.         }  
  60.         File sourceFile = new File(sourceFilePath);  
  61.   
  62.         if (!sourceFile.exists()) {  
  63.             if (log.isWarnEnabled()) {  
  64.                 log.warn("要进行swf的文件不存在!!!");  
  65.             }  
  66.             return false;  
  67.         }  
  68.         log.info("准备转换的文件路径存在");  
  69.         if (!swftoolsPath.endsWith(File.separator)) {  
  70.             swftoolsPath += File.separator;  
  71.         }  
  72.         StringBuilder commandBuidler = new StringBuilder(swftoolsPath);  
  73.         File swfFile = new File(swfFilePath);  
  74.         if (!swfFile.getParentFile().exists()) {  
  75.             swfFile.getParentFile().mkdirs();  
  76.         }  
  77.         if (filetype.toLowerCase().equals("jpg")) {  
  78.             filetype = "jpeg";  
  79.         }  
  80.         List<String>  command = new   ArrayList<String>();    
  81.             command.add(this.swftoolsPath+"\\"+filetype.toLowerCase()+"2swf.exe");//从配置文件里读取      
  82.             command.add("-z");      
  83.             command.add("-s");      
  84.             command.add("flashversion=9");      
  85.             command.add("-s");      
  86.             command.add("poly2bitmap");//加入poly2bitmap的目的是为了防止出现大文件或图形过多的文件转换时的出错,没有生成swf文件的异常      
  87.             command.add(sourceFilePath);      
  88.             command.add("-o");      
  89.             command.add(swfFilePath);      
  90.         try {  
  91.             ProcessBuilder processBuilder = new ProcessBuilder();      
  92.             processBuilder.command(command);      
  93.             Process process = processBuilder.start();      
  94.             log.info("开始生成swf文件..");  
  95.             dealWith(process);  
  96.             try {      
  97.                 process.waitFor();//等待子进程的结束,子进程就是系统调用文件转换这个新进程      
  98.             } catch (InterruptedException e) {      
  99.                 e.printStackTrace();      
  100.             }      
  101.             File swf = new File(swfFilePath);  
  102.             if (!swf.exists()) {  
  103.                 return false;  
  104.             }  
  105.             log.info("转化SWF文件成功!!!");  
  106.         } catch (IOException e) {  
  107.             // TODO Auto-generated catch block  
  108.             log.error("转化为SWF文件失败!!!");  
  109.             e.printStackTrace();  
  110.             return false;  
  111.         }  
  112.   
  113.         return true;  
  114.     }  
  115.     public static void main(String[] args) {  
  116.         ConvertToSwf a=new ConvertToSwf("D:\\Program Files\\SWFTools");  
  117.         a.convertFileToSwf("D:\\aa.pdf""D:\\bb.swf");  
  118.     }  
  119.     private void dealWith(final Process pro){      
  120.         // 下面是处理堵塞的情况      
  121.         try {      
  122.             new Thread(){      
  123.                 public void run(){      
  124.                     BufferedReader br1 = new BufferedReader(new InputStreamReader(pro.getInputStream()));      
  125.                     String text;      
  126.                     try {      
  127.                         while ( (text = br1.readLine()) != null) {      
  128.                             System.out.println(text);      
  129.                         }      
  130.                     } catch (IOException e) {      
  131.                         e.printStackTrace();      
  132.                     }      
  133.                 }      
  134.             }.start();      
  135.         } catch (Exception e) {      
  136.             e.printStackTrace();      
  137.         }      
  138.               
  139.         try {      
  140.             new Thread(){      
  141.                 public void run(){      
  142.                     BufferedReader br2 = new BufferedReader(new InputStreamReader(pro.getErrorStream()));//这定不要忘记处理出理时产生的信息,不然会堵塞不前的      
  143.                     String text;      
  144.                     try {      
  145.                         while( (text = br2.readLine()) != null){      
  146.                             System.err.println(text);      
  147.                         }      
  148.                     } catch (IOException e) {      
  149.                         e.printStackTrace();      
  150.                     }      
  151.                 }      
  152.             }.start();      
  153.         } catch (Exception e) {      
  154.             e.printStackTrace();      
  155.         }      
  156.     }      


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值