maven依赖导入

<!-- httpclient start -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
        </dependency>

后端回调代码

@PostMapping("/save")
    public Map<String, Object> handleCallback(@RequestParam("remoteUrl") String remoteUrl, @RequestBody CallbackRequest request) throws IOException {
        log.info("Callback request: {}", request);
        System.out.println(remoteUrl);
        if (request.getStatus() == 2 || request.getStatus() == 6) { // 2 表示文档已保存
            String fileUrl = request.getUrl(); //onlyoffice返回的文件下载地址
            String key = request.getKey(); // 文档唯一标识
            downloadAndSaveFile(fileUrl,remoteUrl); // 下载文件并存储
        }
        Map<String, Object> map = new HashMap<>();
        map.put("error", 0);
        return map;
    }




@Data
public class CallbackRequest {
    private String key;

    private Integer status;

    private String url;

    private String changesUrl;

    private String remoteUrl;

    private List<String> users;
}

后端的这个接口 在请求过滤拦截器那块,一定要把这个接口地址加进去,要不然onlyoffice的这个服务是无法回调后端接口的。 

前端调用代码

<template>
  <div id="editor" style="width: 100%; height: 800px;"></div>
</template>

<script>

export default {
  props: {
    documentUrl: String,
    reportId: String// 文档唯一 ID
  },
  data() {
    return {
      // documentUrl: null,
    };
  },
  created() {
  },
  async mounted() {
    // 初始化编辑器配置
    this.editorConfig = {
      document: {
        fileType: 'docx',
        key: Date.now().toString(), //唯一标识这个一定要每次都是唯一的
        title: '报告.docx',
        url: this.documentUrl,
        remoteUrl: this.documentUrl,
      },
      documentType: 'word',
      editorConfig: {
        callbackUrl: 后端回调接口地址,
        customization: { forcesave: true }  // 启用强制保存
      },

    };

    // 加载 OnlyOffice 脚本
    const script = document.createElement('script');
    script.src = 'http://部署onlyoffice的服务器地址以及端口/web-apps/apps/api/documents/api.js';
    script.onload = () => {
      new window.DocsAPI.DocEditor('editor', this.editorConfig);
    };
    document.head.appendChild(script);
  },
};
</script>

在docker中部署onlyOffice的服务,可能后续访问的时候,会说权限不足的问题

 修改启动的配置文件,将token去除,更改配置文件local.json和default.json,配置文件位置

        /etc/onlyoffice/documentserver

        local.json中将参数token都改为false,去除token

"token": {
        "enable": {
          "request": {
            "inbox": false,
            "outbox": false
          },
          "browser": false
        },
default.json中将参数request-filtering-agent改为true,token也改为false,rejectUnauthorized改为false

"request-filtering-agent" : {
                "allowPrivateIPAddress": true,
                "allowMetaIPAddress": true
            },
 
"token": {
                "enable": {
                    "browser": false,
                    "request": {
                        "inbox": false,
                        "outbox": false
                    }
                },
"rejectUnauthorized": false
        修改了以上配置参数后,重启服务,再次测试。

        这样就能解决报错文档权限问题,文档保存失败问题,文档下载问题等报错信息

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐