onlyoffice服务在线编辑文档保存解析

1.开发前提

    搭建onlyoffice document服务 可以再本地访问 documentserver代表服务器的域名或者ip+port

2.测试+解释onlyoffice的config

官方给的查看word文档(docx)的config

window.docEditor = new DocsAPI.DocEditor("placeholder",
{
    "document": {
        "fileType": "docx",
        "key": "E7FAFC9C22A8",
        "title": "Example Document Title.docx",
        "url": "https://example.com/url-to-example-document.docx"
    },
    "documentType": "text",
    "editorConfig": {
        "callbackUrl": "https://example.com/url-to-callback.ashx",
    },
    "height": "100%",
    "width": "100%"
});
名字 描述 类型 例子 fileType 定义源查看或编辑文档的文件类型 String “docx” title 为查看或编辑的文档定义所需的文件名,当文档被下载时它也将被用作文件名。 String "testTitle.docx" url 定义存储源查看或编辑文档的绝对URL String ""
    height :页面高度 width :页面宽度 documentType : 文件编辑类型,根据文件的类型在客户端用不通的编辑器来编辑文件主要三种 文档类-text、表格类-spreadsheet、ppt类-presentation callbackUrl 文件关闭后回调路劲 这个用来保存文件用的 文件编辑保存后 当你关闭窗口后 server端会请求把你在服务器上的编辑提交到这个路劲 ,所以这个路劲的代码 一般就是上传保存 ;

3.修改后保存文件的回调代码

保存问题折腾了一阵,其实很简单——

  1. 我们在config配置了callbackUrl,文档加载时会调用这个接口,此时status = 1,我们给onlyoffice的服务返回{"error":"0"}的信息,这样onlyoffice会认为回调接口是没问题的,这样就可以在线编辑文档了,否则的话会弹出窗口说明
The document could not be saved. Please check connection settings or contact your administrator. When you click the OK button, you will be prompted to download the document. Find more information about connecting Document Server
  1. 当我们关闭编辑窗口后,十秒钟左右onlyoffice会将它存储的我们的编辑后的文件,,此时status = 2,通过request发给我们,我们需要做的就是接收到文件然后回写该文件。
  2. 注意:1.回调接口中要给唯一标识,让程序知道要回写的文件;2.post接口 @RequestMapping(value = "/docx/save", method = RequestMethod.POST, produces = "application/json;charset=UTF-8") @ResponseBody public void saveWord(HttpServletRequest request, HttpServletResponse response) { try { PrintWriter writer = response.getWriter(); String body = ""; try { Scanner scanner = new Scanner(request.getInputStream()); scanner.useDelimiter("\A"); body = scanner.hasNext() ? scanner.next() : ""; scanner.close(); } catch (Exception ex) { writer.write("get request.getInputStream error:" + ex.getMessage()); return; } if (body.isEmpty()) { writer.write("empty request.getInputStream"); return; } JSONObject jsonObj = JSON.parseObject(body); int status = (Integer) jsonObj.get("status"); int saved = 0; if(status == 2 || status == 3) //MustSave, Corrupted { String downloadUri = (String) jsonObj.get("url"); try { URL url = new URL(downloadUri); java.net.HttpURLConnection connection = (java.net.HttpURLConnection) url.openConnection(); InputStream stream = connection.getInputStream(); if (stream == null) { throw new Exception("Stream is null"); } String path = request.getParameter("path"); File savedFile = new File(upload_file_path+"/"+path); try (FileOutputStream out = new FileOutputStream(savedFile)) { int read; final byte[] bytes = new byte[1024]; while ((read = stream.read(bytes)) != -1) { out.write(bytes, 0, read); } out.flush(); } connection.disconnect(); } catch (Exception ex) { saved = 1; ex.printStackTrace(); } }writer.write("{"error":" + saved + "}"); } catch (IOException e) { writer.write("{"error":"-1"}");e.printStackTrace(); } }
经验分享 程序员 微信小程序 职场和发展