您好,登录后才能下订单哦!
本篇文章给大家分享的是有关Pageoffice如何结合fastdfs在线编辑及预览office文档,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
背景:我们项目的文档是存放在fastdfs服务器上面的,现有需求需要实现将fastdfs上面的各种office文档在网站页面进行预览和编辑保存
方案:由于fastdfs目前是不支持文档的更新的,所以只能通过插入新文档,删除老文档的方式来实现,同时需要将原有数据库表中存储的文件uuid给更新掉,然后pageoffice只能支持文档在本服务器内的预览和编辑,因此需要在应用服务器中先下载fastdfs服务器中的文件,然后本地保存为临时文件,然后给pageoffice预览编辑,保存后再通过删除和插入来更新fastdfs的文件,从而达到目的。
补充:需要定时清理由此产生的应用服务器下载的临时文件
大部分代码可以参考pageoffice官网文档中的
PageOffice 4.5 for JAVA (同时支持IE、Chrome、Firefox版)
PageOffice 4.5 for Spring Boot[示例代码]
放代码:
pageoffice初始配置类
@Configuration
 public class PageOfficeConfiguration {
    /**
      * 添加PageOffice的服务器端授权程序Servlet(必须)
      */
     @Bean
     public ServletRegistrationBean servletRegistrationBean() {
         com.zhuozhengsoft.pageoffice.poserver.Server poserver = new com.zhuozhengsoft.pageoffice.poserver.Server();
         //设置PageOffice注册成功后,license.lic文件存放的目录
         //服务器可以存放在/geelyapp/pageoffice
         poserver.setSysPath("d:\\lic\\");
         ServletRegistrationBean srb = new ServletRegistrationBean(poserver);
         srb.addUrlMappings("/xx/pageoffice/poserver.zz");
         srb.addUrlMappings("/xx/pageoffice/posetup.exe");
         srb.addUrlMappings("/xx/pageoffice/pageoffice.js");
         srb.addUrlMappings("/xx/pageoffice/jquery.min.js");
         srb.addUrlMappings("/xx/pageoffice/pobstyle.css");
         srb.addUrlMappings("/xx/pageoffice/sealsetup.exe");
          return srb;// 
     }
 }
应用controller类
@Controller
 @RequestMapping("/xx/pageoffice")
 @Api(value = "文档在线编辑接口")
 public class PageOfficeController {
     @Value("${pageoffice.path.doc:'D:/doc/'}")
     private String pageofficeDocPath;
     
     @Autowired
     private StorageService storageService;//fastdfs封装服务类
     @Autowired
     private ICommonUtilService commonUtilService;
     
     private static final Logger LOGGER = LoggerFactory.getLogger(PageOfficeController.class);
     
     @RequestMapping(value="/index", method=RequestMethod.GET)
     public ModelAndView showIndex(
         @ApiParam(name = "busi", required = true, value = "业务代码")
         @RequestParam(name = "busi", required = true) String busi,
         @ApiParam(name = "filePath", required = true, value = "文件远程路径(相对路径,不是包含http的全路径)")
         @RequestParam(name = "filePath", required = true) String filePath,
         Map<String,Object> map) {
         map.put("busi",busi);
         map.put("filePath",filePath);
         ModelAndView mv = new ModelAndView("Index");
         return mv;
     }
     
 @RequestMapping(value="/word", method=RequestMethod.GET)
     public ModelAndView showWord(HttpServletRequest request, Map<String,Object> map,
             @ApiParam(name = "busi", required = false, value = "业务代码")
             @RequestParam(name = "busi", required = false) String busi,
             @ApiParam(name = "filePath", required = false, value = "文件远程路径(相对路径,不是包含http的全路径)")
             @RequestParam(name = "filePath", required = false) String filePath){
         String[] param = busi.split("%");//这里因为使用&符合传递多个参数时会导致被编译为&导致无法调用成功
         busi = param[0];
         filePath = param[2];
 //        pageofficeDocPath = "D:\\doc\\"; //本地开发环境时可使用
         String[] filePathArr = filePath.split("/");
         String url = pageofficeDocPath+filePathArr[filePathArr.length-1];
         LOGGER.info("showWord url:"+url);
         File tmpFile = new File(url);
         try (FileOutputStream fos = new FileOutputStream(tmpFile)){
             fos.write(this.storageService.readFileContent(filePath));
         } catch (Exception e) {
             LOGGER.error("预览临时文件生成错误",e);
             throw BusinessException.withErrorCode(Errors.System.ILLEAGAL_DATA);
         }
         //--- PageOffice的调用代码 开始 -----
         PageOfficeCtrl poCtrl=new PageOfficeCtrl(request);
         poCtrl.setServerPage("/xx/pageoffice/poserver.zz");//设置授权程序servlet
         poCtrl.addCustomToolButton("保存","Save",1); //添加自定义按钮
         poCtrl.setSaveFilePage("/xx/pageoffice/save?filePath="+filePath+"&busi="+busi+"&url="+url);//设置保存时的action
         String prefix = "file://";//linux服务器查找文件需要额外前缀
         url = prefix + url;
         LOGGER.info("showWord pageoffice url:"+url);
         if(filePath.endsWith(".doc")||filePath.endsWith(".docx")) {
             poCtrl.webOpen(url,OpenModeType.docAdmin,SessionContextUtil.getSrmUserName());
         }else if(filePath.endsWith(".xls")||filePath.endsWith(".xlsx")) {
             poCtrl.webOpen(url,OpenModeType.xlsNormalEdit,SessionContextUtil.getSrmUserName());
         }else if(filePath.endsWith(".ppt")||filePath.endsWith(".pptx")) {
             poCtrl.webOpen(url,OpenModeType.pptNormalEdit,SessionContextUtil.getSrmUserName());
         }
         map.put("pageoffice",poCtrl.getHtmlCode("PageOfficeCtrl1"));
         //--- PageOffice的调用代码 结束 -----
         ModelAndView mv = new ModelAndView("Word");
         return mv;
     }
     
     @RequestMapping("/save")
     public void saveFile(HttpServletRequest request, HttpServletResponse response,
         @ApiParam(name = "busi", required = true, value = "业务代码")
         @RequestParam(name = "busi", required = true) String busi,
         @ApiParam(name = "url", required = false, value = "临时文件url")
         @RequestParam(name = "url", required = false) String url,
         @ApiParam(name = "filePath", required = true, value = "文件远程路径(相对路径,不是包含http的全路径)")
         @RequestParam(name = "filePath", required = true) String filePath){
         FileSaver fs = new FileSaver(request, response);
         //由于fastdfs并不支持文件更新
         //目前考虑删除原有的文件,重新生成文件
         //TODO 后面可以考虑支持文件的更新,即框架来做删除插入,并且新插入的uuid需要跟删除的一致
         String newCode = this.storageService.store(fs.getFileName(), fs.getFileStream());
         //将原来业务表中的文档uuid更新
         commonUtilService.updateFilePath(busi,filePath,newCode);
         this.storageService.delete(filePath);
 //        fs.saveToFile(url);
         fs.close();
        File tmpFile = new File(url);
         tmpFile.delete();
     }
 }
Index.ftl
<!DOCTYPE html>
 <html xmlns="http://www.w3.org/1999/xhtml">
     <head>
         <title>Index</title>
         <script type="text/javascript" src="jquery.min.js"></script>
          <script type="text/javascript" src="pageoffice.js" id="po_js_main"></script>
    </head>
     <body>
         <h2>PageOffice预览准备</h2>
         
         <a href="javascript:POBrowser.openWindowModeless('/xx/pageoffice/word?busi=${busi}%filePath%${filePath}','width=1200px;height=800px;');">打开文件 </a>
     </body>
 </html>
Word.ftl
<!DOCTYPE html>
 <html xmlns="http://www.w3.org/1999/xhtml">
     <head>
         <title>Hello World!</title>
         <script type="text/javascript">
               function Save() {
                   document.getElementById("PageOfficeCtrl1").WebSave();
             }
         </script>
         <script type="text/javascript">
          function AddSeal() {
             try{
                   document.getElementById("PageOfficeCtrl1").ZoomSeal.AddSeal();
             }catch (e){ };
             }
           </script>
     </head>
     <body>
         <div >${pageoffice}</div>
     </body>
 </html>
以上就是Pageoffice如何结合fastdfs在线编辑及预览office文档,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。