在线文档功能后台代码

公司需要开发一个类似百度文库功能的管理站,在网上找了好久,主要有两种实现方法,我在这里根据网上一篇文章,总结了一下具体的实现。

首先下载必要的文件。

1、SWF显示组件 flexpaper  下载地址 http://flexpaper.devaldi.com/

2、DOC文件转换为PDF文件 openoffice3.2

3、PDF文件转换SWF文件  pdf2swf.exe

4、实现在java类中操作openoffice3.2 的类包  jodconverter-2.2.2


flexpaper可以去上面的官网地址下载,但直接下载的组件会有广告和一些不需要用到的功能,所以最好是自己下载Flex源码进行修改


接下来要通过java类来实现文件类型的转换,在网上直接找到该类的代码。

  1. package com;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.File;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7.   
  8. import com.artofsolving.jodconverter.DocumentConverter;  
  9. import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;  
  10. import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;  
  11. import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;  
  12.   
  13. /** 
  14.  * doc docx格式转换 
  15.  *  
  16.  * @author Administrator 
  17.  *  
  18.  */  
  19. public class DocConverter {  
  20.     private static final int environment = 1;// 环境 1:windows 2:linux  
  21.                                                 // (只涉及pdf2swf路径问题)  
  22.     private String fileString;  
  23.     private String outputPath = "";// 输入路径 ,如果不设置就输出在默认的位置  
  24.     private String fileName;  
  25.     private File pdfFile;  
  26.     private File swfFile;  
  27.     private File docFile;  
  28.   
  29.     public DocConverter(String fileString) {  
  30.         ini(fileString);  
  31.     }  
  32.   
  33.     /** 
  34.      * 重新设置file 
  35.      *  
  36.      * @param fileString 
  37.      */  
  38.     public void setFile(String fileString) {  
  39.         ini(fileString);  
  40.     }  
  41.   
  42.     /** 
  43.      * 初始化 
  44.      *  
  45.      * @param fileString 
  46.      */  
  47.     private void ini(String fileString) {  
  48.         this.fileString = fileString;  
  49.         fileName = fileString.substring(0, fileString.lastIndexOf("."));  
  50.         docFile = new File(fileString);  
  51.         pdfFile = new File(fileName + ".pdf");  
  52.         swfFile = new File(fileName + ".swf");  
  53.     }  
  54.   
  55.     /** 
  56.      * 转为PDF 
  57.      *  
  58.      * @param file 
  59.      */  
  60.     private void doc2pdf() throws Exception {  
  61.         if (docFile.exists()) {  
  62.             if (!pdfFile.exists()) {  
  63.                 OpenOfficeConnection connection = new SocketOpenOfficeConnection(  
  64.                         8100);  
  65.                 try {  
  66.                     connection.connect();  
  67.                     DocumentConverter converter = new OpenOfficeDocumentConverter(  
  68.                             connection);  
  69.                     converter.convert(docFile, pdfFile);  
  70.                     // close the connection  
  71.                     connection.disconnect();  
  72.                     System.out.println("****pdf转换成功,PDF输出:" + pdfFile.getPath()  
  73.                             + "****");  
  74.                 } catch (java.net.ConnectException e) {  
  75.                     e.printStackTrace();  
  76.                     System.out.println("****swf转换器异常,openoffice服务未启动!****");  
  77.                     throw e;  
  78.                 } catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {  
  79.                     e.printStackTrace();  
  80.                     System.out.println("****swf转换器异常,读取转换文件失败****");  
  81.                     throw e;  
  82.                 } catch (Exception e) {  
  83.                     e.printStackTrace();  
  84.                     throw e;  
  85.                 }  
  86.             } else {  
  87.                 System.out.println("****已经转换为pdf,不需要再进行转化****");  
  88.             }  
  89.         } else {  
  90.             System.out.println("****swf转换器异常,需要转换的文档不存在,无法转换****");  
  91.         }  
  92.     }  
  93.   
  94.     /** 
  95.      * 转换成 swf 
  96.      */  
  97.     private void pdf2swf() throws Exception {  
  98.         Runtime r = Runtime.getRuntime();  
  99.         if (!swfFile.exists()) {  
  100.             if (pdfFile.exists()) {  
  101.                 if (environment == 1) {// windows环境处理  
  102.                     try {  
  103.                         Process p = r.exec("D:/pdf2swf.exe "+ pdfFile.getPath() + " -o "+ swfFile.getPath() + " -T 9");  
  104.                         System.out.print(loadStream(p.getInputStream()));  
  105.                         System.err.print(loadStream(p.getErrorStream()));  
  106.                         System.out.print(loadStream(p.getInputStream()));  
  107.                         System.err.println("****swf转换成功,文件输出:"  
  108.                                 + swfFile.getPath() + "****");  
  109.                         if (pdfFile.exists()) {  
  110.                             pdfFile.delete();  
  111.                         }  
  112.   
  113.                     } catch (IOException e) {  
  114.                         e.printStackTrace();  
  115.                         throw e;  
  116.                     }  
  117.                 } else if (environment == 2) {// linux环境处理  
  118.                     try {  
  119.                         Process p = r.exec("pdf2swf " + pdfFile.getPath()  
  120.                                 + " -o " + swfFile.getPath() + " -T 9");  
  121.                         System.out.print(loadStream(p.getInputStream()));  
  122.                         System.err.print(loadStream(p.getErrorStream()));  
  123.                         System.err.println("****swf转换成功,文件输出:"  
  124.                                 + swfFile.getPath() + "****");  
  125.                         if (pdfFile.exists()) {  
  126.                             pdfFile.delete();  
  127.                         }  
  128.                     } catch (Exception e) {  
  129.                         e.printStackTrace();  
  130.                         throw e;  
  131.                     }  
  132.                 }  
  133.             } else {  
  134.                 System.out.println("****pdf不存在,无法转换****");  
  135.             }  
  136.         } else {  
  137.             System.out.println("****swf已经存在不需要转换****");  
  138.         }  
  139.     }  
  140.   
  141.     static String loadStream(InputStream in) throws IOException {  
  142.   
  143.         int ptr = 0;  
  144.         in = new BufferedInputStream(in);  
  145.         StringBuffer buffer = new StringBuffer();  
  146.   
  147.         while ((ptr = in.read()) != -1) {  
  148.             buffer.append((char) ptr);  
  149.         }  
  150.   
  151.         return buffer.toString();  
  152.     }  
  153.   
  154.     /** 
  155.      * 转换主方法 
  156.      */  
  157.     public boolean conver() {  
  158.   
  159.         if (swfFile.exists()) {  
  160.             System.out.println("****swf转换器开始工作,该文件已经转换为swf****");  
  161.             return true;  
  162.         }  
  163.   
  164.         if (environment == 1) {  
  165.             System.out.println("****swf转换器开始工作,当前设置运行环境windows****");  
  166.         } else {  
  167.             System.out.println("****swf转换器开始工作,当前设置运行环境linux****");  
  168.         }  
  169.         try {  
  170.             doc2pdf();  
  171.             pdf2swf();  
  172.         } catch (Exception e) {  
  173.             e.printStackTrace();  
  174.             return false;  
  175.         }  
  176.   
  177.         if (swfFile.exists()) {  
  178.             return true;  
  179.         } else {  
  180.             return false;  
  181.         }  
  182.     }  
  183.   
  184.     /** 
  185.      * 返回文件路径 
  186.      *  
  187.      * @param s 
  188.      */  
  189.     public String getswfPath() {  
  190.         if (swfFile.exists()) {  
  191.             String tempString = swfFile.getPath();  
  192.             tempString = tempString.replaceAll("\\\\", "/");  
  193.             return tempString;  
  194.         } else {  
  195.             return "";  
  196.         }  
  197.   
  198.     }  
  199.   
  200.     /** 
  201.      * 设置输出路径 
  202.      */  
  203.     public void setOutputPath(String outputPath) {  
  204.         this.outputPath = outputPath;  
  205.         if (!outputPath.equals("")) {  
  206.             String realName = fileName.substring(fileName.lastIndexOf("/"),  
  207.                     fileName.lastIndexOf("."));  
  208.             if (outputPath.charAt(outputPath.length()) == '/') {  
  209.                 swfFile = new File(outputPath + realName + ".swf");  
  210.             } else {  
  211.                 swfFile = new File(outputPath + realName + ".swf");  
  212.             }  
  213.         }  
  214.     }  
  215.   
  216.     public static void main(String s[]) {  
  217.         DocConverter d = new DocConverter("D:/1.doc");  
  218.         d.conver();  
  219.     }  
  220. }  
贴入上面代码前

第一步:先确认OpenOffice是否已经安装,因为在代码中要引入到文件安装的路径

第二步:确认你的项目中引入了jodconverter-2.2.2的jar包

第三步:在DOS中启动OpenOffice的服务

找到OpenOffice的安装路径并执行下面代码

C:\Program Files\OpenOffice.org 3\program  soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" –nofirststartwizard

注:我在执行上面代码时,DOS报出了找不到–nofirststartwizard文件的异常,所以我去掉了–nofirststartwizard这一段,执行也成功了。

C:\Program Files\OpenOffice.org 3\program  soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;"

确认所有准备工作都完成后,在下面代码中变更你pdf2swf.exe的文件位置

  1. Process p = r.exec("D:/pdf2swf.exe "+ pdfFile.getPath() + " -o "+ swfFile.getPath() + " -T 9");  
最后在main方法传入你要转换的文件路径和文件名,执行就可以生成swf文件了。


在WEB项目中,生成的文件名可以根据自己的要求来变更,最终只用掉用上面的工具类代码就可以了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值