4453b793e17cd9b557959e305b0548ca.png

点击上方“蓝字”关注我们

6b04ee6b35118168eab43ce67c5b9724.png

1

了解onlyoffice

be65f046d731a661c46dca91c0a650a0.png

ONLYOFFICE Docs是一个开源办公套件,包括文本文档、电子表格和演示文稿的编辑器。它提供以下功能:

1、创建、编辑和查看文本文档、电子表格和演示文稿;

2、与其他队友实时协作处理文件;

3、ONLYOFFICE Docs 还支持用于将您的应用程序与在线办公室集成的WOPI 协议。

897a9b36cf0aa90aaa0d2e58320a757b.png

ec85b12022b5fe60161cdc65d9538dcc.png

2

前提准备

5b8d55d7200d2b0da9ace51e720b5bab.png

搭建安装onlyoffice,具体参考官网地址:

https://helpcenter.onlyoffice.com/installation/docs-developer-install-ubuntu.aspxfrom=api_csharp_example

f4ce657485a980544308535262f019d0.png

ea6b04d4fabf3f39ed65948f38f6a62c.png

3

开发进行中

d0dd658b1528fdbfcd40fd5ce1fbb9d1.png

1、准备一个接口返回config配置文件。

c2d01346e2e7175c9a6aa92bc221f03e.png

@GetMapping("/config/{fileId}")
@ApiOperation("返回配置信息")
public?String?getConfig(ModelMap map,@PathVariable?String?fileId){
????//具体业务处理省略
????//主要是获取一些信息,用于设置html中的脚本对象config上。
????//4、设置视图数据:a、文件类型。b、用户信息。c、文件信息。
????map.addAttribute("docType",documentType);
????map.addAttribute("user",user);
????map.addAttribute("fileManager",fileManager);????//将html页面返回回去
????return?"onlineEdit";
}

ffd00044eaa12c71da361a27d40c5f57.png

2、准备一个callback接口用于文件保存。

e88c54d9ff1924e440c1002138b7226d.png

@PostMapping("/saveFile/{fileId}/{fileCode}")
@ApiOperation("在线编辑保存回调接口")
@ResponseBody
public?void?saveFile(HttpServletRequest request , HttpServletResponse response, @PathVariable?String?fileId, @PathVariable?String?fileCode) throws IOException {
????PrintWriter writer = response.getWriter();
????Scanner scanner = new?Scanner(request.getInputStream()).useDelimiter("\A");
????String?body = scanner.hasNext() ? scanner.next() : "";
????JSONObject jsonObject = JSONObject.parseObject(body);
????System.out.println(jsonObject);
????//status等于2时表示已经准备好保存
????if((Integer) jsonObject.get("status") == 2){
??????//2、根据返回的Url去下载文件
??????URL url = new?URL((String) jsonObject.get("url"));
??????java.net.HttpURLConnection connection = (java.net.HttpURLConnection) url.openConnection();
??????InputStream stream = connection.getInputStream();
??????//此处获取到的流即是onlyoffice服务下的文件流。
??????//3、重新上传业务省略
??????connection.disconnect();
????}
????writer.write("{"error":0}");

}

9181d4feb16f829cd3772c6c7923e940.png

3、准备一个html页面。

66096004b13e17736bf08c9cf59fd2f0.png

<!DOCTYPE html>
<html?lang="en"?style="height: 100%;">
<head>
????<meta?charset="UTF-8">
????<script?type="text/javascript"?src="https://192.168.53.151:9000/web-apps/apps/api/documents/api.js"></script>
????<script?type="text/javascript"?language="javascript"?>
????????var??config = {
????????????"type": "desktop",
????????????"mode": "review",
????????????"documentType": "[[${docType}]]",
????????????"document": {
????????????????"title": "[[${fileManager.fileName}]]",
????????????????"url": "文件下载地址",
????????????????"fileType": "[[${fileManager.fileType}]]",
????????????????"key": "[[${fileManager.fileManagerId}]]",
????????????????"info": {},
????????????????"permissions": {
????????????????????"comment": true,
????????????????????"copy": true,
????????????????????"download": true,
????????????????????"edit": true,
????????????????????"print": true,
????????????????????"fillForms": true,
????????????????????"modifyFilter": true,
????????????????????"modifyContentControl": true,
????????????????????"review": true,
????????????????????"commentGroups": {}
????????????????}
????????????},
????????????"editorConfig": {
????????????????"mode": "edit",
????????????????"callbackUrl": 回调接口保存文件的地址,
????????????????"lang": "zh",
????????????????"createUrl": "",
????????????????"templates": [
????????????????????{
????????????????????????"icon": "",
????????????????????????"name": "Blank",
????????????????????????"url": "http://ip地址/OnlineEditorsExampleJava_war_exploded/EditorServlet?fileExt=docx"
????????????????????},
????????????????????{
????????????????????????"icon": "http://ip地址/OnlineEditorsExampleJava_war_exploded/css/img/file_docx.svg",
????????????????????????"name": "With sample content",
????????????????????????"url": "http://ip地址/OnlineEditorsExampleJava_war_exploded/EditorServlet?fileExt=docx&sample=true"
????????????????????}
????????????????],
????????????????"user": {
????????????????????"id": "[[${user.userId}]]",
????????????????????"name": "[[${user.username}]]"
????????????????},
????????????????"customization": {
????????????????????"goback": {
????????????????????????"url": "http://ip地址/OnlineEditorsExampleJava_war_exploded/IndexServlet"
????????????????????},
????????????????????"forcesave": false,
????????????????????"submitForm": false,
????????????????????"about": true,
????????????????????"feedback": false
????????????????},
????????????????"canCoAuthoring": true,
????????????????"canUseHistory": true,
????????????????"canHistoryClose": true,
????????????????"canHistoryRestore": false,
????????????????"canSendEmailAddresses": false,
????????????????"canRequestEditRights": true,
????????????????"canRequestClose": false,
????????????????"canRename": false,
????????????????"canMakeActionLink": true,
????????????????"canRequestUsers": true,
????????????????"canRequestSendNotify": true,
????????????????"canRequestSaveAs": false,
????????????????"canRequestInsertImage": true,
????????????????"canRequestMailMergeRecipients": true
????????????},
????????????"width": "100%",
????????????"height": "100%",
????????????"events": {},
????????????"frameEditorId": "iframeEditor"
????????}
????????var?connectEditor = function?() {
????????????new?DocsAPI.DocEditor("placeholder", config);
????????};
????????if?(window.addEventListener) {
????????????window.addEventListener("load", connectEditor);
????????} else?if?(window.attachEvent) {
????????????window.attachEvent("load", connectEditor);
????????}
????</script>
????<title>在线编辑文档</title>
</head>
<body?style="height: 100%; margin: 0;">
<div?id="placeholder"?style="height: 100%"></div>
</body>
</html>

3527565a4f0766171fe96ddebfd5bab6.png

更加具体的config对象和回调处理接口内容参考官网:

https://api.onlyoffice.com/editors/getdocs

d285b5f49d2a2a6134068e875c30aec4.png

a9020dfc1380c87069f7aa5fbd9141a9.png

4

测试

d27862af6996d4b8c39e64b98385770c.png

当我调用config接口时,打开不同类型的文件,展示返回html页面如下。

8098c30aee540642f5fdf3145eef98aa.png

10e8237c33fee3903c46fd4b2af893ba.png

b63c798ec38d438cb320f1b29463227b.png

839af4c13f023a4bd845065a0bd6c524.png

e2bbd419279be1233b219dafb1389a44.png

5

总结

8d9958a41ffb5c0b09c1a589d4bae893.png

1、要使用onlyoffice去在线编辑不难,主要是掌握config的配置。

2、它的一个工作流程:当我打开在线编辑时,接口设置数据返回html页面,并将数据拼接到config上。接着页面会根据config的url地址去下载源文件,最后将内容展示到html上。最后当我们修改完毕关闭了窗口时,会调用callbackurl的接口进行文件保存。

1c94e4cb44069abfbc5ae551cd6b5d7b.png

8e4bf4e0b207094b805bd3a0dbba3da3.png

0a49b32c2159470201ade974a5678d9e.png

扫码二维码

获取更多精彩

efd709e4b5b4cc1020c6cdd6111b0f5d.png

更多推荐