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

1.开发前提

 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”
key定义服务用于文档识别的唯一文档标识符。 如果发送已知密钥,文档将从缓存中获取。 每次文档被编辑和保存时,都必须重新生成密钥。 文档url可以用作密钥,但不包含特殊字符,长度限制为20个符号。(注意如果秘钥值不更换那么看到的文档还是最先加载的缓存文档)

作者:小赖快跑
链接:https://www.jianshu.com/p/262c3fc77f0c
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
String
"Khirz6zTPdfd7"
title
为查看或编辑的文档定义所需的文件名,当文档被下载时它也将被用作文件名。
String
"testTitle.docx"
url
定义存储源查看或编辑文档的绝对URL
String"https://example.com/url-to-example-document.docx"

 

  • 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();
            }
    
        }

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值