onlyoffice介绍和代码集成
1.onlyoffice介绍
onlyoffice可以实现网页对word,excle,ppt等文件的在线预览和编辑,详情请参考官方网址onlyoffice官网
2.onlyoffice安装
参考链接onlyoffice离线安装
3.代码集成
3.1前端代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>hello onlyoffice</title>
<!-- onlyoffice服务器安装好后,提供的api.js-->
<script type="text/javascript" src="http://{{ip}}:8311/web-apps/apps/api/documents/api.js"></script>
</head>
<body style="height: 900px;">
<div id="placeholder" ></div>
<script type="text/javascript">
var docEditor = new DocsAPI.DocEditor("placeholder",{
"document":{
"fileType": "docx",
"key": Date.now()+'', // 保证文档的唯一,可以用uuid
"permissions":{
"edit": true // false 为只读,true 可以编辑
},
"title":"onlyoffice-demo.docx",
"url":"http://{{ip:port}}/1691505725878.docx" // 编辑或查看的文档地址
},
"documentType":"text", // doc为text ,excle为spreadsheet,ppt为presentation
"editorConfig":{
"lang":"zh-CN",
"callbackUrl":"http://{{ip:port}}/onlyOffice/save/"+"1688923542597169154", // 保存文档的地址
"user":{
"id":"1",
"name":"测试人"
},
"customization":{
"autosave": false,
"chat": false,
"comments":false,
"help": false,
"plugins": false,
"forcesave": true
}
},
"height":"100%",
"width":"100%"
})
</script>
</body>
</html>
3.2 后端代码
注意,该接口需要去掉token认证,如果onlyoffice出现无法编辑等提示,请进入onlyoffice的日志文件,查看错误信息:
docker exec -it myoffice /bin/bash
cd /var/log/onlyoffice/documentserver/docservice
cat out.log
package org.jeecg.file.controller;
import cn.hutool.core.map.MapUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.util.MinioUtil;
import org.jeecg.file.entity.WwZxjyFiles;
import org.jeecg.file.service.IWwZxjyFilesService;
import org.jeecg.util.MethodUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
@Api(tags = "附件表")
@RestController
@RequestMapping()
@Slf4j
public class WwZxjyFilesController extends JeecgController<WwZxjyFiles, IWwZxjyFilesService> {
@Autowired
IWwZxjyFilesService iWwZxjyFilesService;
@ApiOperation(value = "保存onlyoffice", notes = "保存onlyoffice")
@PostMapping("/onlyOffice/save/{fileId}")
public ResponseEntity<Map> save(@RequestBody Map param,@PathVariable String fileId){
log.info("param:{}", param);
Long key = MapUtil.getLong(param,"key");
log.info("methode[save] key:{}",key);
Integer status = MapUtil.getInt(param,"status");
if(status == 2 || status ==3||status ==6){
try {
WwZxjyFiles oldFileInfo = iWwZxjyFilesService.getById(fileId);
String[] urlInfo = oldFileInfo.getUrl().split("/");
String newFileInfo = urlInfo[urlInfo.length-1];
URL url = new URL((String) param.get("url"));
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream inputStream = connection.getInputStream();
int size = inputStream.available();
String fileUrl = MinioUtil.upload(inputStream,newFileInfo);
WwZxjyFiles wwZxjyFiles=new WwZxjyFiles();
wwZxjyFiles.setId(fileId);
wwZxjyFiles.setFileSize(MethodUtil.getFileSize(String.valueOf(size)));
wwZxjyFiles.setUrl(fileUrl);
wwZxjyFiles.setShareUrl(fileUrl);
iWwZxjyFilesService.updateById(wwZxjyFiles);
} catch (Exception e) {
log.error(e.getMessage());
}
}
Map result = new HashMap();
result.put("error","0");
return ResponseEntity.ok(result);
}
}