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);
    }
}

3.3 其他博客文章

onlyoffice+vue实现在线预览在线编辑

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值