OnlyOffice java 部署使用,文件流方式 预览文件

avatar 2022年02月11日14:06:08 6 7014 views
博主分享免费Java教学视频,B站账号:Java刘哥

最近准备换一个文件预览的组件,发现了 OnlyOffice,经过使用,发现非常好用

之前用的 kkFileView 跟 OnlyOffice 对比就是天差地别

本文主要介绍 OnlyOffice 的部署使用

一、部署 OnlyOffice 文档服务

官方Gihub地址:https://github.com/ONLYOFFICE/DocumentServer

官网地址:https://www.onlyoffice.com

使用 docker 安装 OnlyOffice 服务:

https://helpcenter.onlyoffice.com/installation/docs-community-install-docker.aspx

直接通过上面的链接安装

我这里也是用的 docker 安装,安装教程看上面链接,这里不赘述

如下图

启动服务命令

docker run -i -t -d -p 10001:80 731f9669f88e

查看运行日志命令

docker logs -f 00980737aa44

进入容器命令

docker exec -ti 00980737aa44 /bin/bash

nginx日志在

/var/log/onlyoffice/documentserver/nginx.error.log

 

二、运行Java预览Demo: OnlineEditorsExampleJava

官方Demo地址:https://github.com/ONLYOFFICE/document-server-integration/tree/master/web/documentserver-example/java

代码下载下来是这样的

tomcat部署

首页是这样的

 

可以上传文件进行预览

 

 

 

三、文件流的方式预览文件,预览SVN仓库文件

在上面例子里加一个 Servlet 接口,用于预览文件

import entities.FileModel;
import helpers.ConfigManager;
import helpers.DocumentManager;
import org.apache.commons.codec.binary.Base64;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.UUID;


@WebServlet(name = "onlinePreview", urlPatterns = {"/onlinePreview"})
public class OnlinePreviewServlet extends HttpServlet
{

    /**
     * 处理请求
     * 请求示例:http://10.1.17.88:8089/OnlineEditorsExampleJava/onlinePreview?url=xxxxxx
     * 这个 url参数是经过 BASE64加密的
     * 解密后是 http://网关域名/bis-svn/commonfile/svn/fileReview/2222.docx?fileId=31957&token=xxxxxxxx
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        String url = request.getParameter("url");
        String fileUrl = new String(Base64.decodeBase64(url), StandardCharsets.UTF_8);
        String fileName = this.getFileNameBySvnFileUrl(fileUrl);
        fileName = java.net.URLDecoder.decode(fileName, "UTF-8");

        DocumentManager.Init(request, response);

        FileModel file = new FileModel(fileName, fileUrl);
        file.editorConfig.mode = "view";
        file.document.key = UUID.randomUUID().toString();
        request.setAttribute("file", file);
        request.setAttribute("docserviceApiUrl", ConfigManager.GetProperty("files.docservice.url.api"));
        request.getRequestDispatcher("editor.jsp").forward(request, response);
    }

    /**
     * 根据URL获取文件名称
     *
     * @param url 示例  http://网关域名/bis-svn/commonfile/svn/fileReview/2222.docx?fileId=31957&token=xxxxxxxx
     * @return 2222.docx
     */
    private String getFileNameBySvnFileUrl(String url)
    {
        String startWord = "/fileReview/";
        String endWord = "?";
        String fileName = url.substring(url.indexOf(startWord) + startWord.length(), url.indexOf(endWord));
        System.out.println(fileName);
        return fileName;
    }


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        processRequest(request, response);
    }

}

主要是给 file.document.url 设置文件的 URL,确保这个路径是能访问的,就能进行预览

 

文件 URL 的接口是返回文件流

伪代码如下

/**
 * 文件预览
 * @param fileName
 * @param suffix
 * @param fileId
 * @throws Exception
 */
@RequestMapping(value = "/svn/fileReview/{fileName}.{suffix}", method = RequestMethod.GET)
public void fileReview(@PathVariable String fileName, @PathVariable String suffix, 
					   Long fileId, HttpServletResponse response) throws Exception
{
	// 根据文件id查询文件信息
	CommonFileDO commonFileDO = commonFileService.selectById(fileId);
	if (CommonUtil.isEmpty(commonFileDO))
	{
		throw new BusinessException("获取文件信息失败");
	}
	// 获取文件物理路径...
	// 读取文件数据写入 response
	// ... 省略
}

文件正常预览

 

  • 微信
  • 交流学习,有偿服务
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:0   待审核评论数:0