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

2024-04-04 10:18

本文主要是介绍文档附件在线查看(类似百度文库的实现),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

                   附件格式 为 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文件。

这篇关于文档附件在线查看(类似百度文库的实现)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/875512

相关文章

C++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在

Java实现检查多个时间段是否有重合

《Java实现检查多个时间段是否有重合》这篇文章主要为大家详细介绍了如何使用Java实现检查多个时间段是否有重合,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录流程概述步骤详解China编程步骤1:定义时间段类步骤2:添加时间段步骤3:检查时间段是否有重合步骤4:输出结果示例代码结语作

使用C++实现链表元素的反转

《使用C++实现链表元素的反转》反转链表是链表操作中一个经典的问题,也是面试中常见的考题,本文将从思路到实现一步步地讲解如何实现链表的反转,帮助初学者理解这一操作,我们将使用C++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定

Java覆盖第三方jar包中的某一个类的实现方法

《Java覆盖第三方jar包中的某一个类的实现方法》在我们日常的开发中,经常需要使用第三方的jar包,有时候我们会发现第三方的jar包中的某一个类有问题,或者我们需要定制化修改其中的逻辑,那么应该如何... 目录一、需求描述二、示例描述三、操作步骤四、验证结果五、实现原理一、需求描述需求描述如下:需要在

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

如何通过Python实现一个消息队列

《如何通过Python实现一个消息队列》这篇文章主要为大家详细介绍了如何通过Python实现一个简单的消息队列,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录如何通过 python 实现消息队列如何把 http 请求放在队列中执行1. 使用 queue.Queue 和 reque

Python如何实现PDF隐私信息检测

《Python如何实现PDF隐私信息检测》随着越来越多的个人信息以电子形式存储和传输,确保这些信息的安全至关重要,本文将介绍如何使用Python检测PDF文件中的隐私信息,需要的可以参考下... 目录项目背景技术栈代码解析功能说明运行结php果在当今,数据隐私保护变得尤为重要。随着越来越多的个人信息以电子形

使用 sql-research-assistant进行 SQL 数据库研究的实战指南(代码实现演示)

《使用sql-research-assistant进行SQL数据库研究的实战指南(代码实现演示)》本文介绍了sql-research-assistant工具,该工具基于LangChain框架,集... 目录技术背景介绍核心原理解析代码实现演示安装和配置项目集成LangSmith 配置(可选)启动服务应用场景

使用Python快速实现链接转word文档

《使用Python快速实现链接转word文档》这篇文章主要为大家详细介绍了如何使用Python快速实现链接转word文档功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 演示代码展示from newspaper import Articlefrom docx import