2,简单集成springboot

集成地址

http://服务器地址或者域名/web-apps/apps/api/documents/api.js

我本地环境

http://192.168.1.105/web-apps/apps/api/documents/api.js

项目结构:

maven

<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>

SpringController

path 变量定义文件读写位置

@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 = "D:\\developer_tools\\onlyoffice\\test22.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 = "D:\\developer_tools\\onlyoffice\\test22.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();

}

}

}

index.html

src 换成您本地onlyoffice地址

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta charset="utf-8" />

<title>OnlyOffice</title>

</head>

<body style="height:900px">

<div id="placeholder"></div>

<script type="text/javascript" src="http://192.168.1.105/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://127.0.0.1:8080/office/online",

// 添加权限

"permissions": {

// 设置聊天

"chat": true

}

},

// 设置编辑

"editorConfig": {

// 设置语言

"lang": "zh-CN",

// 两种编辑(edit)和查看(view)

"mode": "edit",

// 文件保存时的地址

"callbackUrl": "http://127.0.0.1:8080/office/save",

// 配置用户信息

"user": {

// 用户编号

"id": "admin",

// 用户名称

"name": "管理员"

},

},

};

var docEditor = new DocsAPI.DocEditor("placeholder", config);

</script>

</body>

</html >

启动项目验证

springboot 启动默认8080端口

访问 index.html

查看 onlyoffice 日志,我们前面安装时把日志挂载到 /app/onlyoffice/DocumentServer/logs 目录下

/app/onlyoffice/DocumentServer/logs/documentserver/docservice

本地环境介绍:

换成本机ip访问

提示跨域,下一章单独来讲解

浏览器通过文件路径访问

url 改成本机ip

再次访问成功

源码下载:https://download.youkuaiyun.com/download/qq_39835480/87546308

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值