文檔附件在線查看(類似百度文庫的實現)

需求:用戶上傳附件後,點擊查看,可以在頁面直接查看到附件內容,樣式排版需要和附件文檔裏一致。另外可以查看附件信息,下載附件。

                   附件格式 爲 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文件。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章