文档附件在线查看(类似百度文库的实现)

需求:用户上传附件后,点击查看,可以在页面直接查看到附件内容,样式排版需要和附件文档里一致。另外可以查看附件信息,下载附件。

                   附件格式 为 excle word 文档,pdf 扫描件

分析:一个附件管理的功能 + 在线查看功能。


附件管理的功能好实现,略过。


在线查看,是通过一个播放器查看flash文件,网上例子很多。

flash播放器 搜索 找到了 FlexPaper  (项目中导入flexpaper文件,在显示页面指定路径即可显示)

[javascript]  view plain  copy
  1. <script type="text/javascript">  
  2.                     var fp = new FlexPaperViewer(    
  3.                              'hlbussiness/planbaseViewer/FlexPaperViewer',  
  4.                              'viewerPlaceHolder', { config : {  
  5.                              SwfFile : escape("${swfPath}"),<span style="white-space:pre">  </span>//swf文件路径  
  6.                              Scale : 0.6,  
  7.                              ZoomTransition : 'easeOut',  
  8.                              ZoomTime : 0.5,  
  9.                              ZoomInterval : 0.2,  
  10.                              FitPageOnLoad : true,  
  11.                              FitWidthOnLoad : true//适合初始页宽度大小的装载页  
  12.                              FullScreenAsMaxWindow : true,  
  13.                              ProgressiveLoading : false,  
  14.                              MinZoomSize : 0.2,  
  15.                              MaxZoomSize : 5,  
  16.                              SearchMatchAll : false,  
  17.                              InitViewMode : 'Portrait',  
  18.                              PrintPaperAsBitmap : false,  
  19.                              ViewModeToolsVisible : true,  
  20.                              ZoomToolsVisible : true,  
  21.                              NavToolsVisible : true,  
  22.                              CursorToolsVisible : true,  
  23.                              SearchToolsVisible : true,                          
  24.                              localeChain: 'zh_CN'  
  25.                      }});    
  26.                 </script>  


剩下要做的,是将用户上传的文件转为 swf。 

[java]  view plain  copy
  1. package com.function.util;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.File;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.IOException;  
  7. import java.io.InputStream;  
  8. import java.net.ConnectException;  
  9. import java.util.ResourceBundle;  
  10.   
  11. import com.artofsolving.jodconverter.DocumentConverter;  
  12. import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;  
  13. import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;  
  14. import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;  
  15.   
  16. /** 
  17.  * 将文件转成swf格式 
  18.  *  
  19.  * @author Administrator 
  20.  *  
  21.  */  
  22. public class ConvertSwf {  
  23.   
  24.     /** 
  25.      * 入口方法-通过此方法转换文件至swf格式 
  26.      * @param filePath  上传文件所在文件夹的绝对路径 
  27.      * @param dirPath   文件夹名称 
  28.      * @param fileName  文件名称 
  29.      * @return          生成swf文件名 
  30.      */  
  31.     public String beginConvert(String filePath, String dirName, String fileName) {  
  32.         final String DOC = ".doc";  
  33.         final String DOCX = ".docx";  
  34.         final String XLS = ".xls";  
  35.         final String XLSX = ".xlsx";  
  36.         final String PDF = ".pdf";  
  37.         final String SWF = ".swf";  
  38.         final String TOOL = "pdf2swf.exe";  
  39.         String outFile = "";  
  40.         String fileNameOnly = "";  
  41.         String fileExt = "";  
  42.         if (null != fileName && fileName.indexOf(".") > 0) {  
  43.             int index = fileName.indexOf(".");  
  44.             fileNameOnly = fileName.substring(0, index);  
  45.             fileExt = fileName.substring(index).toLowerCase();  
  46.         }  
  47.         String inputFile = filePath + File.separator + fileName;  
  48.         String outputFile = "";  
  49.   
  50.         //如果是office文档,先转为pdf文件  
  51.         if (fileExt.equals(DOC) || fileExt.equals(DOCX) || fileExt.equals(XLS)  
  52.                 || fileExt.equals(XLSX)) {  
  53.             outputFile = filePath + File.separator + fileNameOnly + PDF;  
  54.             office2PDF(inputFile, outputFile);  
  55.             inputFile = outputFile;  
  56.             fileExt = PDF;  
  57.         }  
  58.   
  59.         if (fileExt.equals(PDF)) {  
  60.             String toolFile = filePath + File.separator + TOOL;  
  61.             outputFile = filePath + File.separator + fileNameOnly + SWF;  
  62.             convertPdf2Swf(inputFile, outputFile, toolFile);  
  63.             outFile = outputFile;  
  64.         }  
  65.         return outFile;  
  66.     }  
  67.   
  68.     /** 
  69.      * 将pdf文件转换成swf文件 
  70.      * @param sourceFile pdf文件绝对路径 
  71.      * @param outFile    swf文件绝对路径 
  72.      * @param toolFile   转换工具绝对路径 
  73.      */  
  74.     private void convertPdf2Swf(String sourceFile, String outFile,  
  75.             String toolFile) {  
  76.         String command = toolFile + " \"" + sourceFile + "\" -o  \"" + outFile  
  77.                 + "\" -s flashversion=9 ";  
  78.         try {  
  79.             Process process = Runtime.getRuntime().exec(command);  
  80.             System.out.println(loadStream(process.getInputStream()));  
  81.             System.err.println(loadStream(process.getErrorStream()));  
  82.             System.out.println(loadStream(process.getInputStream()));  
  83.             System.out.println("###--Msg: swf 转换成功");  
  84.         } catch (Exception e) {  
  85.             e.printStackTrace();  
  86.         }  
  87.     }  
  88.   
  89.     /** 
  90.      * office文档转pdf文件 
  91.      * @param sourceFile    office文档绝对路径 
  92.      * @param destFile      pdf文件绝对路径 
  93.      * @return 
  94.      */  
  95.     private int office2PDF(String sourceFile, String destFile) {  
  96.         ResourceBundle rb = ResourceBundle.getBundle("OpenOfficeService");  
  97.         String OpenOffice_HOME = rb.getString("OO_HOME");  
  98.         String host_Str = rb.getString("oo_host");  
  99.         String port_Str = rb.getString("oo_port");  
  100.         try {  
  101.             File inputFile = new File(sourceFile);  
  102.             if (!inputFile.exists()) {  
  103.                 return -1// 找不到源文件   
  104.             }  
  105.             // 如果目标路径不存在, 则新建该路径    
  106.             File outputFile = new File(destFile);  
  107.             if (!outputFile.getParentFile().exists()) {  
  108.                 outputFile.getParentFile().mkdirs();  
  109.             }  
  110.             // 启动OpenOffice的服务    
  111.             String command = OpenOffice_HOME  
  112.                     + "/program/soffice.exe -headless -accept=\"socket,host="  
  113.                     + host_Str + ",port=" + port_Str + ";urp;\"";  
  114.             System.out.println("###\n" + command);  
  115.             Process pro = Runtime.getRuntime().exec(command);  
  116.             // 连接openoffice服务  
  117.             OpenOfficeConnection connection = new SocketOpenOfficeConnection(  
  118.                     host_Str, Integer.parseInt(port_Str));  
  119.             connection.connect();  
  120.             // 转换   
  121.             DocumentConverter converter = new OpenOfficeDocumentConverter(  
  122.                     connection);  
  123.             converter.convert(inputFile, outputFile);  
  124.   
  125.             // 关闭连接和服务  
  126.             connection.disconnect();  
  127.             pro.destroy();  
  128.   
  129.             return 0;  
  130.         } catch (FileNotFoundException e) {  
  131.             System.out.println("文件未找到!");  
  132.             e.printStackTrace();  
  133.             return -1;  
  134.         } catch (ConnectException e) {  
  135.             System.out.println("OpenOffice服务监听异常!");  
  136.             e.printStackTrace();  
  137.         } catch (IOException e) {  
  138.             e.printStackTrace();  
  139.         }  
  140.         return 1;  
  141.     }  
  142.       
  143.     static String loadStream(InputStream in) throws IOException{  
  144.         int ptr = 0;  
  145.         in = new BufferedInputStream(in);  
  146.         StringBuffer buffer = new StringBuffer();  
  147.           
  148.         while ((ptr=in.read())!= -1){  
  149.             buffer.append((char)ptr);  
  150.         }  
  151.         return buffer.toString();  
  152.     }  
  153.   
  154. }  
这个类的运行的前提条件:

         1、安装openoffice,新建OpenOfficeService.properties文件,放到src目录下

[html]  view plain  copy
  1. OO_HOME = D:/Program Files/OpenOffice.org 3  
  2. oo_host = 127.0.0.1  
  3. oo_port =8100  

         2、下载 jodconverter-2.2.2 ,将lib目录所有jar 复制到项目 lib目录
         3、 下载swftools-0.9.2.exe 安装后,复制安装目录中到 pdf2swf.exe 到项目附件目录中。



在action中

[java]  view plain  copy
  1. //转换上传文件格式为swf  
  2.         String outPath = new ConvertSwf().beginConvert(dirPath, dirName, fileName);   
  3.         System.out.println("生成swf文件:" + outPath);  

即可生成文件名与原文件名一直的swf文件。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值