最新onlyoffice后端

This commit is contained in:
wangqiong
2024-03-14 09:05:00 +08:00
parent ff6f67a160
commit 7c9681b236
423 changed files with 23054 additions and 17889 deletions
+320 -231
View File
@@ -1,247 +1,336 @@
## Overview
This example will help you integrate ONLYOFFICE Docs into your web application written in Java
with Spring Boot.
Spring Boot has a lot of functionality, but its most significant features are: dependency management,
auto-configuration, and built-in servlet containers.
**Please note**: It is intended for testing purposes and demonstrating functionality of the editors. Do NOT use this integration example on your own server without proper code modifications! In case you enabled the test example, disable it before going for production.
## For Windows
### Step 1. Install ONLYOFFICE Docs
Download and install ONLYOFFICE Docs (packaged as Document Server).
See the detailed guide to learn how to install Document Server [for Windows](https://helpcenter.onlyoffice.com/installation/docs-developer-install-windows.aspx).
### Step 2. Download the Java code for the editors integration
Download the [Java-Spring example](https://api.onlyoffice.com/editors/demopreview) from our site.
To connect the editors to your website, specify the path to the editors installation, server port and the path to the storage folder in the *\src\main\resources\application.properties* file:
```
files.storage=
server.port=port
files.docservice.url.site=https://documentserver/
```
where the **documentserver** is the name of the server with the ONLYOFFICE Docs installed, **port** is any available port and **files.storage** is the path where files will be created and stored (in the project folder by default). You can set an absolute path. For example, *D:\\\\folder*. Please note that on Windows OS the double backslash must be used as a separator.
If you want to experiment with the editor configuration, modify the [parameters](https://api.onlyoffice.com/editors/advanced) it the *\src\main\resources\editor.html* file.
### Step 3. Install the prerequisites
To run the Java example code, install the Java version 11 appropriate for your OS and framework **Apache Maven**:
* Java (download from [the Oracle official website](https://www.oracle.com/ru/java/technologies/javase-jdk11-downloads.html));
* Apache Maven (download from [the official website](https://maven.apache.org/download.cgi)).
### Step 4. Set environment variables
1. After you have installed Java on Windows, set the **JAVA_HOME** environment variable to point to the Java installation directory.
Find out where Java is installed. If you didn't change the path during installation, it will be something like this:
```
C:\Program Files\Java\jdk11
```
In **Windows 7** right click **My Computer** and select **Properties**, then click **Advanced**.
In **Windows 8** go to **Control Panel** -> **System** -> **Advanced System Settings**.
Click the **Environment Variables** button.
Under **System Variables**, click **New**.
In the **Variable Name** field, enter **JAVA_HOME** if you installed the **JDK** (Java Development Kit) or **JRE_HOME** if you installed the **JRE** (Java Runtime Environment).
In the **Variable Value** field, enter your **JDK** or **JRE** installation path, for example C:\Program Files\Java\jdk11.
Check if the variable created successfully by **echo** command in the **Command Prompt**:
```
echo %JAVA_HOME%
```
2. Set the **MAVEN_HOME** environment variable:
Unzip the downloaded archive with the maven to any directory, it will be something like this:
```
C:\apache-maven-3.8.1
```
In **Windows 7** right click **My Computer** and select **Properties**, then click **Advanced**.
In **Windows 8** go to **Control Panel** -> **System** -> **Advanced System Settings**.
Click the **Environment Variables** button.
Under **System Variables**, click **New**.
In the **Variable Name** field, enter **MAVEN_HOME**.
In the **Variable Value** field, enter your **JDK** or **JRE** installation path, for example C:\apache-maven-3.8.1.
Add C:\apache-maven-3.8.1\bin to the PATH system variable:
In system variables, find PATH, clicks on the Edit... button. In “Edit environment variable” dialog, clicks on the New button and add this C:\apache-maven-3.8.1\bin
Check if the variable created successfully by **echo** command in the **Command Prompt**:
```
echo %MAVEN_HOME%
```
### Step 5. Start application with Maven
1. Open the console and go the java-spring folder using the **cd** command, for example:
```
cd C:\Program Files\document-server-integration\web\documentserver-example\java-spring
```
2. In the open console enter the following commands:
```
mvn clean
mvn package
mvn spring-boot:run
```
3. Open your browser using **server.address** and **server.port**:
```
http://server.address:server.port/
```
### Step 6. Check accessibility
In case the example and Document Server are installed on different computers, make sure that your server with the example installed has access to the Document Server with the address which you specify instead of **documentserver** in the configuration files.
Make sure that the Document Server has access to the server with the example installed with the address which you specify instead of **example.com** in the configuration files.
## For Linux
### Step 1. Install ONLYOFFICE Docs
Download and install ONLYOFFICE Docs (packaged as Document Server).
See the detailed guide to learn how to install Document Server [for Linux](https://helpcenter.onlyoffice.com/installation/docs-developer-install-ubuntu.aspx).
### Step 2. Install the prerequisites and run the website with the editors
1. Install **Java**:
```
sudo add-apt-repository ppa:webupd8team/java sudo apt-get update sudo apt-get install oracle-java11-installer
```
2. Download the archive with the Java-Spring example and unpack the archive or clone git repository:
a) archive with Java-Spring:
```
wget https://api.onlyoffice.com/app_data/editor/Java%20Spring%20Example.zip
```
```
unzip Java\ Spring\ Example.zip
```
b) git repository:
```
git clone https://github.com/ONLYOFFICE/document-server-integration.git
```
3. Change the current directory for the project directory:
a) from archive
```
cd Java\ Spring\ Example/
```
b) from git repository
```
cd document-server-integration/web/documentserver-example/java-spring
```
4. Edit the *application.properties* configuration file. Specify the name of your local server with the ONLYOFFICE Document Server installed.
```
nano src/main/resources/application.properties
```
Edit the following lines:
```
files.storage=
server.port=port
files.docservice.url.site=https://documentserver/
```
where the **documentserver** is the name of the server with the ONLYOFFICE Docs installed, **port** is any available port and **files.storage** is the path where files will be created and stored (in the project folder by default). You can set an absolute path.
5. Install **Maven**:
```
sudo apt-get install maven
```
6. Build:
```
mvn package
```
7. Start Java-Spring example:
```
./mvnw spring-boot:run
```
8. Open your browser using **server.address** and **server.port**:
```
http://server.address:server.port/
```
# Spring-Boot-onlyOffice(集成onlyOffice服务)
springboot集成onlyOffice的实现。在参考网络上的资料结合自身实际情况总结,让集成oo方便快捷。 封装了oo服务的API和统一了配置。
- 在线编辑,查看,转换
* onlyOffice.yml配置文件
* 如果解决了你地方问题,请给个 **star**
-----------------------------------
# 对外使用的java类 —— OnlyServiceAPI
## 核心方法
<table>
<thead>
<tr>
<th>方法名称</th>
<th>参数</th>
<th>参数说明</th>
<th>返回值</th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="3">openDocument()</td>
<td> Map&lt;String,Object&gt; map</td>
<td> 文件数据信息<br>
{<br>
必填 fileId<br>
必填 fileName<br>
必填 fileType<br>
必填 fileSize<br>
可用携带其它值<br>
}
</td>
<td rowspan="3">
<pre>
{
"editorConfig": {
"mode": "edit",
"customization": {
"feedback": {
"visible": false
},
"help": false,
"goback": {
"blank": false
},
"macros": false,
"autosave": false,
"comments": false,
"review": {},
"hideRightMenu": true,
"anonymous": {
"request": false
},
"forcesave": true,
"logo": {},
"hideNotes": true
},
"plugins": {
"pluginsData": [],
"autostart": []
},
"callbackUrl": "http://172.31.240.1:9090/onlyOffice/save",
"lang": "zh-CN",
"user": {
"name": "TongHuic7bba5",
"id": "c7bba5"
}
},
"docServiceApiUrl": "http://172.31.240.1:8886/web-apps/apps/api/documents/api.js",
"documentType": "word",
"document": {
"permissions": {
"edit": true,
"chat": false,
"review": false
},
"title": "fdfs.docx",
"fileType": "docx",
"key": "63f560ec03a94654b10cd4fdeebec05a",
"url": "http://172.31.240.1:9090/download/09cee8767dd3476280fa865bacfaf213",
"info": {
"sharingSettings": [{
"isLink": true,
"permissions": ["Full Access"],
"user": "TongHuic7bba5"
}],
"created": "2023-08-05 21:38:25"
}
},
"type": "desktop",
"token": ""
}</pre>
</td>
</tr>
<tr>
<td>String mode</td>
<td>打开方式 <br>edit<br>view</td>
</tr>
<tr>
<td>boolean collaborativeEditing</td>
<td>是否协同编辑</td>
</tr>
<tr>
<td>handlerStatus()</td>
<td>JSONObject jsonObject</td>
<td>onlyOffice 回调传来的值。<br>https://api.onlyoffice.com/editors/callback</td>
<td></td>
</tr>
<tr>
<td rowspan="2">save():触发保存回调。</td>
<td>String key</td>
<td>是openDocument()返回值中的key</td>
<td rowspan="2">String : 提示信息</td>
</tr>
<tr>
<td>String userId</td>
<td>用户Id</td>
</tr>
<tr>
<td rowspan="5">converted()</td>
<td>String filetype</td>
<td>文件类型</td>
<td rowspan="5">转换后的文件下载地址</td>
</tr>
<tr>
<td>String fileId</td>
<td>文件Id</td>
</tr>
<tr>
<td>String outputtype</td>
<td>转化类型</td>
</tr>
<tr>
<td>String title</td>
<td>转换后的文件名称</td>
</tr>
<tr>
<td>String password</td>
<td>文档密码</td>
</tr>
</tbody>
</table>
**注意:**
* 外部触发保存操作,save()方法。
* `autosave`/`forcesave` 参数为默认值时有效
* 修改文件后,没有执行回调方法。在点击保存后执行回调
![](jpg/f.jpg)
快速使用
-----------------------------------
- 必须的依赖
``` pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.25</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.16</version>
</dependency>
<dependency>
<groupId>com.inversoft</groupId>
<artifactId>prime-jwt</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
```
- 定义关键接口(oo服务回调使用) `重要`
1.保存接口
```
参考:demo/controller/IndexController.saveFile()
对应配置文件参数:oo.call-back-url
回调状态
0 - 找不到具有密钥标识符的文档
1 - 正在编辑文档(打开文件回调)
2 - 文档已准备好保存
3 - 发生文档保存错误
4 - 不作任何更改就关闭文档(关闭回调)
6 - 正在编辑文档,但保存当前文档状态(保存回调)
7 - 强制保存文档时发生错误
```
### Step 3. Check accessibility
2.下载文件地址
```
参考:demo/controller/IndexController.download()
对应配置文件参数:oo.download-file
对应openDocument()方法返回值中的 document.url
打开文件后oo服务要下载对应的文件
```
- 实现SaveFileProcessor类
```
参考:demo.service.DemoService
```
<table>
<thead>
<tr>
<th> 方法名称 </th>
<th> 方法说明 </th>
<th> 参数 </th>
<th> 参数说明 </th>
<th> 返回值 </th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="3">saveBeforeInitialization()</td>
<td rowspan="3">保存前置方法</td>
<td>Map&lt;String, Object&gt; map</td>
<td>在openDocument()方法中传入的文件数据信息</td>
<td rowspan="3"></td>
</tr>
<tr>
<td>byte[] bytes</td>
<td>文件二进制数据</td>
</tr>
<tr>
<td>String fileExtension</td>
<td>文件后缀</td>
</tr>
<tr>
<td rowspan="4">save()</td>
<td rowspan="4">保存方法,实现自己的保存逻辑</td>
<td>Map&lt;String, Object&gt; map</td>
<td>在openDocument()方法中传入的文件数据信息</td>
<td rowspan="4">Map&lt;String, Object&gt; 更新内存中的信息</td>
</tr>
<tr>
<td>byte[] bytes</td>
<td>文件二进制数据</td>
</tr>
<tr>
<td>byte[] changes</td>
<td>当前文件和保存之前文件的区别。changes.zip</td>
</tr>
<tr>
<td>String key</td>
<td>是openDocument()返回值中的key</td>
</tr>
<tr>
<td rowspan="3">saveAfterInitialization()</td>
<td rowspan="3">保存后置方法</td>
<td>Map&lt;String, Object&gt; map</td>
<td>在openDocument()方法中传入的文件数据信息</td>
<td rowspan="3"></td>
</tr>
<tr>
<td>byte[] bytes</td>
<td>文件二进制数据</td>
</tr>
<tr>
<td>String fileExtension</td>
<td>文件后缀</td>
</tr>
</tbody>
</table>
In case the example and Document Server are installed on different computers, make sure that your server with the example installed has access to the Document Server with the address which you specify instead of **documentserver** in the configuration files.
Make sure that the Document Server has access to the server with the example installed with the address which you specify instead of **example.com** in the configuration files.
## 更新记录
- [x] 修复关闭文件后快速再次打开该文件后,保存时报错的bug 2023/11/10
- [x] 修复内存文件信息在保存后没有修改的bug 2023/10/23
- [x] 实现和编辑器的数据通信 2023/9/9 (更新链接)
https://blog.xenosp.cn/posts/87468caf/
- [x] 修复文件转pdf时只有第一页的bug 2023/8/13
-----------------------------------
# 页面集成
### html
```
参考 onlyOffice.html
style.js 中的openDocument()
```
### vue
```
参考 onlyOffice.vue
```
-----------------------------------
![](jpg/QQ截图20230805221213.jpg)
### 编辑
![](jpg/edit.jpg)
![](jpg/editxlsx.jpg)
![](jpg/editppt.jpg)
### 查看
![](jpg/view.jpg)
![](jpg/viewxls.jpg)
![](jpg/viewppt.jpg)
### 文件转换
![](jpg/docx-pdf.jpg)
![](jpg/xlsx2pdf.jpg)
## For Docker
1. Edit the *application.properties* configuration file. Specify the name of your local server with the ONLYOFFICE Document Server installed ([installation instructions](https://helpcenter.onlyoffice.com/installation/docs-developer-install-docker.aspx)).
```
nano src/main/resources/application.properties
```
2. Edit the following lines:
```
files.storage=
server.port=port
files.docservice.url.site=https://documentserver/
```
where the **documentserver** is the name of the server with the ONLYOFFICE Docs installed, **port** is any available port and **files.storage** is the path where files will be created and stored (in the project folder by default). You can set an absolute path.
3. Run the next command in the java example directory:
```
docker-compose up
```
4. Open your browser using **server.address** and **server.port**:
```
http://server.address:server.port/
```
## Important security info
Please keep in mind the following security aspects when you are using test examples:
* There is no protection of the storage from unauthorized access since there is no need for authorization.
* There are no checks against parameter substitution in links, since the parameters are generated by the code according to the pre-arranged scripts.
* There are no data checks in requests of saving the file after editing, since each test example is intended for requests only from ONLYOFFICE Document Server.
* There are no prohibitions on using test examples from other sites, since they are intended to interact with ONLYOFFICE Document Server from another domain.