赞
踩
# 英文版 https://www.onlyoffice.com # 中文版 https://www.onlyoffice.com/zh/ # GitHub上的地址 https://github.com/ONLYOFFICE/DocumentServer # 社区版Docker安装 https://helpcenter.onlyoffice.com/installation/docs-community-install-docker.aspx # DockerHub地址 https://hub.docker.com/r/onlyoffice/documentserver # 开发文档 https://api.onlyoffice.com/editors/basic # 后端参考的demo https://api.onlyoffice.com/editors/demopreview # 前端参考的配置地址 https://api.onlyoffice.com/editors/config/editor#createUrl # 前端自定义编辑参数 https://api.onlyoffice.com/editors/config/editor/customization
# 下载onlyoffice/documentserver:7.1.1
sudo docker pull onlyoffice/documentserver:7.1.1
# 创建Onlyoffice容器
# 对外端口81
sudo docker run -itd \
--name my_onlyoffice \
--restart=always \
-p 81:80 \
-v /home/onlyoffice/logs:/var/log/onlyoffice \
-v /home/onlyoffice/data:/var/www/onlyoffice/Data \
-v /home/onlyoffice/lib:/var/lib/onlyoffice \
-v /home/onlyoffice/db:/var/lib/postgresql \
onlyoffice/documentserver:7.1.1
默认会跳转到欢迎页面,"192.168.108.200"是我的地址
http://192.168.108.200:81/
Status返回值说明
https://api.onlyoffice.com/editors/callback#status-descr
status | 描述 |
---|---|
1 | 每次用户连接到文档协同编辑或断开文档协同编辑时,都会收到该消息。 |
2,3 | 关闭正在编辑的文档10秒钟后,会收到该消息,用户的标识符是最后一个向文档编辑服务发送更改的用户。 |
4 | 在最后一个用户关闭文档时收到。 |
6,7 | 在执行强制保存请求时收到。 |
注意:容器的宿主机与开发电脑需要在同一个局域网中,保证前端访问后端的IP地址能够访问到。
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8" /> <title>OnlyOffice</title> </head> <body style="height:800px"> <div id="placeholder"></div> <script type="text/javascript" src="http://192.168.108.200:81/web-apps/apps/api/documents/api.js"></script> <script type="text/javascript"> var config = { // 设置文档信息 "document": { "fileType": "docx", // 文档唯一标识,最大长度128 "key": "Khirz6zTPdfd7", // 文档名称 "title": "Example Document Title.docx", // 文件地址 "url": "http://192.168.108.1:8080/office/online", // 添加权限 "permissions": { // 设置聊天 "chat": true } }, // 设置编辑 "editorConfig": { // 设置语言 "lang": "zh-CN", // 两种编辑(edit)和查看(view) "mode": "edit", // 文件保存时的地址 "callbackUrl": "http://192.168.108.1:8080/office/save", /* // 配置用户信息 "user": { // 用户编号 "id": "admin", // 用户名称 "name": "管理员" }, */ }, }; var docEditor = new DocsAPI.DocEditor("placeholder", config); </script> </body> </html >
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.mason</groupId> <artifactId>myonlyoffice</artifactId> <version>0.0.1-SNAPSHOT</version> <name>myonlyoffice</name> <description>myonlyoffice project for Spring Boot</description> <properties> <java.version>11</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.alibaba.fastjson2</groupId> <artifactId>fastjson2</artifactId> <version>2.0.10</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
package com.mason.myonlyoffice.controller; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; import org.springframework.stereotype.Controller; import org.springframework.util.StreamUtils; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.URL; @Controller @ResponseBody @RequestMapping("/office") public class EditorController { @GetMapping("/online") public void online(HttpServletResponse response, HttpServletRequest request) { try { response.setCharacterEncoding("utf-8"); response.setContentType("application/octet-stream"); // 读文件输入流 String filePath = "E:\\onlyoffice.docx"; InputStream inputStream = new FileInputStream(filePath); // 复制文件流 StreamUtils.copy(inputStream, response.getOutputStream()); // 关闭输入流 inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } @PostMapping(path = "/save") public void save(HttpServletResponse response, HttpServletRequest request) { try { // 获得response信息 PrintWriter writer = response.getWriter(); // 获取数据文件信息 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); StreamUtils.copy(request.getInputStream(), byteArrayOutputStream); String fileInfoStr = byteArrayOutputStream.toString(); byteArrayOutputStream.close(); if (fileInfoStr.isEmpty()) { writer.write("request的输入流为空"); return; } System.out.println(fileInfoStr); // 转化字符串对象转化为JSON对象 JSONObject fileInfoObj = JSON.parseObject(fileInfoStr); // 获取编辑文件的状态 int status = (Integer) fileInfoObj.get("status"); int saved = 0; // 关闭正在编辑的文档10秒钟后,会收到该消息 if(status == 2 || status == 3) { // 获取文件数据 try { URL url = new URL((String) fileInfoObj.get("url")); java.net.HttpURLConnection connection = (java.net.HttpURLConnection) url.openConnection(); InputStream inputStream = connection.getInputStream(); if (inputStream == null){ throw new Exception("Stream is null"); } // 保存编辑后的数据 String path = "E:\\onlyoffice.docx"; File file = new File(path); // 复制数据流 FileOutputStream outputStream = new FileOutputStream(file); StreamUtils.copy(inputStream, outputStream); // 关闭数据资源 outputStream.close(); inputStream.close(); connection.disconnect(); } catch (Exception ex) { saved = 1; ex.printStackTrace(); } } System.out.println("保存文件成功"); writer.write("{\"error\":" + saved + "}"); } catch (IOException e) { e.printStackTrace(); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。