乐于分享
好东西不私藏

从零开始部署OnlyOffice:一站式服务交互与代码实现教程

从零开始部署OnlyOffice:一站式服务交互与代码实现教程
         
01
基本信息
  • 官网地址:https://www.onlyoffice.com/
  • 中文文档地址:https://api.onlyoffice.com/zh/editors/basic
  • 本地镜像:documentserver:1.0(对应官方软件版本:6.4.2)
02
服务交互
Onlyoffice以独立服务的形式部署,需要分配独立的端口。具体集成服务交互流程如下:
03
服务端部署
1.镜像拉取
dockerpulldocumentserver:1.0
2.直接启动
docker run -itd -p 8014:80 --restart=always -e JWT_ENABLED=false -v /data1/onlyoffice/files/:/var/lib/onlyoffice/documentserverexample/files/documentserver:1.0
3.docker-stack启动
version:"3"services: nullonlyoffice: nullimage:documentserver:1.0environment:  - JWT_ENABLED=falsevolumes:  - /data1/onlyoffice/files/:/var/lib/onlyoffice/documentserverexample/files/deploy: nullreplicas:1restart_policy: nullcondition: anyports:  - 8014:80
4.测试
打开地址http://192.168.100.117:8014/
执行命令并进入example服务
04
呈现效果
PDF
word
excel
05
代码方式
1.后端接口
后端返回预览所需的所有参数给前端,前端通过调用服务端的js实现预览
方式一:返回前端对象(此方式已停止使用)
接口参数可以按照项目配置情况从数据库、配置文件或者代码中获取
@Getter@Setter@ApiModel(value="OnlyOfficePreviewVo对象", description="onlyoffice预览组件所需参数")publicclass OnlyOfficePreviewVo implements Serializable {privatestatic final long serialVersionUID = 1L;@ApiModelProperty(value = "服务端url")privateString documentServerUrl;@ApiModelProperty(value = "文件类型")privateString fileType;@ApiModelProperty(value = "文件标题")privateString title;@ApiModelProperty(value = "文件网络位置")privateString url;@ApiModelProperty(value = "文档类型")privateString documentType;
/**     * 构建文件预览对象     *     * @param documentServerUrl onlyoffice文件服务端地址(必传)     * @param url 文件网络地址(必传,可以在网页上下载的文件地址)     * @param title 文件标题(非必传,用于展示页面上的文件标题)     * @return 文件预览对象     */publicstatic OnlyOfficePreviewVo build(String documentServerUrl, String url, String title) {        OnlyOfficePreviewVo previewVo = new OnlyOfficePreviewVo();        previewVo.setDocumentServerUrl(documentServerUrl);        previewVo.setUrl(url);        previewVo.setTitle(title);        String suffix = StrUtil.subAfter(title, ".", Boolean.TRUE);        Optional<OnlyOfficeDocumentType> optional = OnlyOfficeDocumentType.of(suffix);if (optional.isPresent()) {            OnlyOfficeDocumentType documentType = optional.get();// pdf文件不需要文档类型if (documentType != OnlyOfficeDocumentType.PDF) {                previewVo.setDocumentType(documentType.type);            }            previewVo.setFileType(suffix);        }return previewVo;    }
文件类型转换(pdf类型文件不需要传documentType,给前端返回null即可)
publicenum OnlyOfficeDocumentType {/**     * 文本文档     */    WORD("word", Sets.newHashSet("doc""docm""docx""docxf""dot""dotm""dotx""epub""fb2""fodt""htm""html""mht""mhtml""odt""oform""ott""rtf""stw""sxw""txt""wps""wpt""xml")),/**     * 电子表格     */    CELL("cell", Sets.newHashSet("csv""et""ett""fods""ods""ots""sxc""xls""xlsb""xlsm""xlsx""xlt""xltm""xltx""xml")),/**     * 演示文稿     */    SLIDE("slide", Sets.newHashSet("dps""dpt""fodp""odp""otp""pot""potm""potx""pps""ppsm""ppsx""ppt""pptm""pptx""sxi")),/**     * 便携式文档格式     */    PDF("pdf", Sets.newHashSet("djvu""oxps""pdf""xps"))    ;publicStringtype;public Set<String> fileTypes;    OnlyOfficeDocumentType(Stringtype, Set<String> fileTypes) {this.type = type;this.fileTypes = fileTypes;    }/**     * 类型转换     */publicstatic Optional<OnlyOfficeDocumentType> of(String suffix){return Arrays.stream(OnlyOfficeDocumentType.values()).filter(val -> val.fileTypes.contains(suffix)).findFirst();    }}
–返回示例 {"success"null,"errorCode"0,"traceId""a78e20a3da5a4e829c106a439ea9af00","message"null,"data": {"documentServerUrl""http://192.168.100.117:8014/","fileType""xlsx","title""2024-02统计表.xlsx","url""","documentType""cell"    }}
方式二:minio文件网络地址
  1. 页面上获取,修改ip地址和端口号即可在页面上下载
2. 代码获取
/**     * 获取文件外链     *     * @param objectPath 目标对象路径     * @param expires 过期时间 <=expires 秒 (外链有效时间(单位:秒))     * @return 文件网络地址     * @throws Exception     */public String getObjectUrl(String objectPath, Integer expires) throws Exception {        GetPresignedObjectUrlArgs args = GetPresignedObjectUrlArgs.builder().method(Method.GET)                .bucket(minioProperties.getBucketName()).object(objectPath).expiry(expires).build();return minioClient.getPresignedObjectUrl(args);    }
前端实现
加载远程js
const loadScript = async (url, id) =>new Promise((resolve, reject) => {try {if (document.getElementById(id)) {if (window.DocsAPI) return resolve(null);const intervalHandler = setInterval(() => {if (!window.DocsAPI) return;          clearInterval(intervalHandler);return resolve(null);        }, 500);      } else {const script = document.createElement("script");        script.setAttribute("type""text/javascript");        script.setAttribute("id", id);        script.onload = resolve;        script.onerror = reject;        script.src = url;        script.async = true;document.body.appendChild(script);      }    } catch (e) {console.error(e);    }  });export default loadScript;
由于公众号文本内容限制,剩余前端代码可留言或私信'onlyoffice'获取。

往期推荐

Caffeine缓存库:Java性能最强本地缓存的革命,超越Guava和Ehcache

Nginx配置全解析:掌握这些指令,优化你的服务器性能

ThreadLocal揭秘:如何实现线程安全与数据隔离?

frp内网穿透全攻略:从安装到配置,实现远程访问的终极指南

Spring Boot 集成 Tess4J 实现图片OCR文字识别

Redis 6.0新特性实战:别再只知道多线程了!

系统不崩溃的秘密:全面解读计数器、滑动窗口、漏桶与令牌桶限流

JVM核心揭秘:类加载器与字节码文件的神秘之旅

一站式监控解决方案:Prometheus与Grafana的完整部署指南

HTTPS页面必备:强制使用wss协议的配置技巧

从安装到配置:手把手教你打造个性化的OpenClaw AI智能体

OpenClaw技术架构全解析:五层设计打造智能Agent中枢

OpenClaw技能大揭秘:如何让AI Agent成为全能型助手?

ConcurrentHashMap:从JDK1.7到1.8,那些面试必问的底层逻辑

分享

收藏

点赞

在看