欢迎使用CSDN-markdown编辑器

来源:互联网 发布:阿里云os系统电脑版 编辑:程序博客网 时间:2024/06/05 22:51

首先需要安装office web apps,具体安装过程我是参照下面这篇博客完成的
Office Web Apps安装部署,安装完成后访问 http://officewebapps.pepper.cn/hosting/discovery,这里的officewebapps.pepper.cn为office web apps 服务器的域名,也可以使用 ip地址,如 192.168.1.54.成功之后会看到如下文件:
XML说明文件

接下来就是与自己的项目整合,首先需要了解WOPI协议(Web Application Open Platform Interface),

WOPI协议提供一系列基于web方式的,使文档能在Office Web Apps中查看与编辑的接口服务(Web Service)。只要web应用按照标准,实现了WOPI的接口,那么就可以调用Office Web Apps。例子很多,比如SharePoint,Exchange,SkyDriver,Dropbox集成Office Web Apps。如果我们自己做的web应用实现了相应接口,就可以调用Office Web Apps,从而实现文档的在线编辑查看。 在WOPI结构中,我们把存放Office文档的web应用叫WOPI Host或者WOPI Server。把查看编辑操作Office文档的web应用叫WOPI Client或者叫WOPI applications。所以,Office Web Apps充当的就是WOPI Client的角色。sharePoint,Exchange,自己开发的系统充当的就是WOPI Server的角色。

下图为浏览器,server,client三者的请求顺序及关系:
浏览器,server,client三者的请求顺序及关系

与office web app 集成,我们需要提供两个服务

  • checkFileInfo服务
  • getFile服务

下面看下这两个服务:

1. checkFileInfo 服务:此服务返回的是请求文件的基本信息,WOPI Server(我们自己的系统)以json方式返回给WOPI Client(office web apps),服务的url格式为:

http://server/wopi/files/{filename}?access_token=token
如下示例:
http://localhost:8080/MyOWA/wopi/files/test.docx?access_token=token
这里的filename是你需要预览的文件名称,access_token是WOPI Client请求WOPI Server 时自动加上的。访问成功返回如下json数据格式:
` {
“AllowExternalMarketplace”: true,
“AllowOAuthOverHttp”: true,
“BaseFileName”: “hello.docx”,//文件名
“OwnerId”: “admin”,//文件所有者的唯一编号
“SHA256”: “0e258391262b515b878a8d0ccb78784ad3223ce219287e7df9b3f03c0c457ee9”,
“Size”: 11644,//文件大小,以bytes为单位
“Version”: 1477191374718//文件版本号,文件如果被编辑,版本号也要跟着改变
}
““
Json中至少要包括五个属性:BaseFileName, OwnerId, Size, SHA256, 和 Version,
点击查看更多参数说明

2. GetFile服务:此服务返回的是请求文件的内容,WOPI Server以数据流的方式返回给WOPI Client.服务URl格式一般为

http://server/wopi/files/{filename}/contents?access_token=token
如下示例:
http://localhost:8080/MyOWA/wopi/files/test.docx/contents?access_token=token

package cn.com.officeWebApp.controller;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.PrintWriter;import java.net.URLEncoder;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.log4j.Logger;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.servlet.ModelAndView;import com.alibaba.fastjson.JSON;import cn.com.instai.officeWebApp.helper.SHA256Util;import cn.com.instai.officeWebApp.model.FileModel;import cn.com.instai.officeWebApp.model.OWAModel;@Controller@RequestMapping(value = "wopi")public class OWAController {    private static final Logger logger = Logger.getLogger(OWAController.class);    public static final String OWA_XML = "http://192.168.1.60/hosting/discovery";    /**     * 获取文件信息     * @Description      * @author niepei     * @param request     * @param response     * @param filename     * @param access_token     * @return     * @throws Exception     */    @SuppressWarnings({ "deprecation" })    @RequestMapping(value = "files/{filename}", method = RequestMethod.GET)    public  ModelAndView checkFileInfo(HttpServletRequest request, HttpServletResponse response,@PathVariable("filename") String filename/*, @RequestParam("access_token") String access_token*/)throws Exception {        logger.debug("------------------------------->  checkFileInfo  has   end  ");        String jsonStr = null;        OWAModel model = new OWAModel();        String reqUrl = request.getRequestURL().toString();        filename = reqUrl.substring(reqUrl.lastIndexOf("/"));        String filePath = request.getRealPath("template/"+filename);        logger.debug("------------------------------->  filePath  is " + filePath );        File file = new File(filePath);        if (file.exists()) {            model.setBaseFileName(file.getName());            model.setSize(file.length());            model.setOwnerId("admin");            model.setSHA256(SHA256Util.getFileSHA256(filePath));            model.setVersion(file.lastModified());            model.setAllowExternalMarketplace(true);            model.setAllowOAuthOverHttp(true);        }        jsonStr = JSON.toJSONString(model);        PrintWriter out = response.getWriter();        response.setCharacterEncoding("UTF-8");         response.setContentType("application/json;charset=UTF-8");        out.write(jsonStr);        logger.debug("------------------------------->  checkFileInfo has   end  ");        return null;    }    /**     * 获取文件流     * @Description      * @author niepei     * @param request     * @param response     * @param filename     * @param access_token     * @return     * @throws IOException     */    @SuppressWarnings("deprecation")    @RequestMapping(value = "files/{filename}/contents", method = RequestMethod.GET)    public HttpServletResponse  getFile(HttpServletRequest request, HttpServletResponse response,@PathVariable("filename") String filename/*, @RequestParam("access_token") String access_token*/)throws IOException {        logger.debug("------------------------------->  getFile   has  begin");        filename = request.getRealPath("template/"+filename);        File file = new File(filename);        String name = file.getName();        InputStream is = new BufferedInputStream(new FileInputStream(filename));        byte[] buffer = new byte[is.available()];        is.read(buffer);        is.close();        response.reset();        // 设置response的Header        response.addHeader("Content-Disposition","attachment;filename=" + new String(name.getBytes("utf-8"), "ISO-8859-1"));        response.addHeader("Content-Length", "" + file.length());        response.setContentType("application/octet-stream");        OutputStream toClient = new BufferedOutputStream(response.getOutputStream());        toClient.write(buffer);        toClient.flush();        toClient.close();        logger.debug("------------------------------->  getFile   has   end  ");        return response;    }    @SuppressWarnings("deprecation")    @RequestMapping(value="test",method = RequestMethod.POST)    public ModelAndView test(@RequestBody FileModel file){        logger.debug("------------------------------->  test  has   end  ");        String URL = "";        String filename = file.getFilename();        String ext = filename.substring(filename.lastIndexOf("."));        if(ext.indexOf("do")>0){            URL = "http://officewebapps.instai.cn/wv/wordviewerframe.aspx?WOPISrc=FILE_PATH";        }else if(ext.indexOf("xl")>0){            URL = "http://officewebapps.instai.cn/x/_layouts/xlviewerinternal.aspx?WOPISrc=FILE_PATH";        }else if(ext.indexOf("pp")>0){            URL = "http://officewebapps.instai.cn/p/PowerPointFrame.aspx?WOPISrc=FILE_PATH";        }        String filePath = URLEncoder.encode("http://localhost:8080/InstaiEduAi/wopi/files/"+filename);        URL = URL.replace("FILE_PATH", filePath);        logger.debug("URL ------------------> " + URL);        ModelAndView mav = new ModelAndView("redirect:" + URL);        logger.debug("------------------------------->  test  has   end  ");        return mav;         }}
0 0