基于JFinal中搭建wopi协议支撑办法

Posted littlehb

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于JFinal中搭建wopi协议支撑办法相关的知识,希望对你有一定的参考价值。

1、添加maven依赖

 <dependency>
            <groupId>com.github.icecooly</groupId>
            <artifactId>FastHttpClient</artifactId>
            <version>1.7</version>
 </dependency>
 

2、FastHttpClient示例
https://gitee.com/icecooly/FastHttpClient

3、在 Jfinal的Start.java中配置

/**
* 配置路由
 */
public void configRoute(Routes me) 
    me.add("/baseService/wopiImpl", WopiController.class);

    

4、WopiController.java

package com.demo.blog;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.core.NotAction;
import com.jfinal.ext.interceptor.GET;
import com.jfinal.ext.interceptor.POST;
import com.jfinal.upload.UploadFile;
import java.io.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

public class WopiController extends Controller 
    private String filePath = "D:\\Work\\";


    /**
     * 功能:配合wopi协议,提供文件下载
     * 作者:黄海
     * 时间:2019-08-17
     * 用例:   http://127.0.0.1/baseService/wopi/files/123.docx/contents
     */
    @Before(GET.class)
    public void download() 
        String filename = getRequest().getRequestURI().split("/")[4];
        renderFile(new File(filePath + filename));
    

    /**
     * 功能:配合wopi协议,获取文件的信息
     * 作者:黄海
     * 时间:2019-08-17
     * 用例:http://127.0.0.1/baseService/wopi/files/123.docx
     */
    @Before(GET.class)
    public void getInfo() 
        String filename = getRequest().getRequestURI().split("/")[4];
        FileInfo info = new FileInfo();
        try 
            if (filename != null && filename.length() > 0) 
                File file = new File(filePath + filename);
                if (file.exists()) 
                    info.setBaseFileName(file.getName());
                    info.setSize(file.length());
                    info.setOwnerId("admin");
                    info.setVersion(file.lastModified());
                    info.setSha256(getHash256(file));
                
            
            renderJson(info);
         catch (Exception e) 
            e.printStackTrace();
        
    

    /**
     * 功能:配合wopi协议,对修改后的文件进行保存
     * 作者:黄海
     * 时间:2019-08-17
     */
    @Before(POST.class)
    public void save() throws IOException 
        UploadFile uf=getFile("content");
        File upFile = new File(filePath+uf.getFileName());
        if (!upFile.getParentFile().exists()) 
            upFile.getParentFile().mkdirs();
        
        if(upFile.exists())
        
            upFile.delete();
        
        uf.getFile().renameTo(upFile);
        renderNull();
    

    /**
     * 获取文件的SHA-256值
     *
     * @param file
     * @return
     */
    @NotAction
    private String getHash256(File file) throws IOException, NoSuchAlgorithmException 
        String value = "";
        try (InputStream fis = new FileInputStream(file)) 
            byte[] buffer = new byte[1024];
            int numRead;
            // 返回实现指定摘要算法的 MessageDigest 对象
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            do 
                numRead = fis.read(buffer);
                if (numRead > 0) 
                    // 更新摘要
                    digest.update(buffer, 0, numRead);
                
             while (numRead != -1);

            value = new String(Base64.getEncoder().encode(digest.digest()));
        
        return value;
    

5、FileInfo.java

package com.demo.blog;

import java.io.Serializable;

/**
 * 文件属性对象
 *
 * 由于wopi的接口不遵守驼峰命名规则,所以需要用@JsonProperty指定别名
 * Created by ethendev on 2017/4/15.
 */
public class FileInfo implements Serializable 

    /**
     * 文件名
     */
    private String baseFileName;

    /**
     * 文件所有者的唯一编号
     */
    private String ownerId;

    /**
     * 文件大小,以bytes为单位
     */
    private long size;

    /**
     * 文件的256位bit的SHA-2编码散列内容
     */
    private String sha256;

    /**
     * 文件版本号,文件如果被编辑,版本号也要跟着改变
     */
    private long version;

    /**
     * 允许外部服务的连接
     */
    private boolean allowExternalMarketplace = true;

    /**
     * 更改文件的权限
     */
    private boolean userCanWrite = true;

    /**
     * 是否支持更新
     */
    private boolean supportsUpdate = true;

    /**
     * 是否支持锁定
     */
    private boolean supportsLocks = true;

    public String getBaseFileName() 
        return baseFileName;
    

    public void setBaseFileName(String baseFileName) 
        this.baseFileName = baseFileName;
    

    public String getOwnerId() 
        return ownerId;
    

    public void setOwnerId(String ownerId) 
        this.ownerId = ownerId;
    

    public long getSize() 
        return size;
    

    public void setSize(long size) 
        this.size = size;
    

    public String getSha256() 
        return sha256;
    

    public void setSha256(String sha256) 
        this.sha256 = sha256;
    

    public long getVersion() 
        return version;
    

    public void setVersion(long version) 
        this.version = version;
    

    public boolean isAllowExternalMarketplace() 
        return allowExternalMarketplace;
    

    public void setAllowExternalMarketplace(boolean allowExternalMarketplace) 
        this.allowExternalMarketplace = allowExternalMarketplace;
    

    public boolean isUserCanWrite() 
        return userCanWrite;
    

    public void setUserCanWrite(boolean userCanWrite) 
        this.userCanWrite = userCanWrite;
    

    public boolean isSupportsUpdate() 
        return supportsUpdate;
    

    public void setSupportsUpdate(boolean supportsUpdate) 
        this.supportsUpdate = supportsUpdate;
    

    public boolean isSupportsLocks() 
        return supportsLocks;
    

    public void setSupportsLocks(boolean supportsLocks) 
        this.supportsLocks = supportsLocks;
    

    @Override
    public String toString() 
        return "FileInfo" +
                "baseFileName=‘" + baseFileName + ‘\‘‘ +
                ", ownerId=‘" + ownerId + ‘\‘‘ +
                ", size=" + size +
                ", sha256=‘" + sha256 + ‘\‘‘ +
                ", version=" + version +
                ", allowExternalMarketplace=" + allowExternalMarketplace +
                ", userCanWrite=" + userCanWrite +
                ", supportsUpdate=" + supportsUpdate +
                ", supportsLocks=" + supportsLocks +
                ‘‘;
    

6、测试类

package com.demo.blog;
import io.itit.itf.okhttp.*;
import io.itit.itf.okhttp.util.FileUtil;

import java.io.File;
import java.io.InputStream;

public class WopiTest 
    /*

    */
    public static void main(String[] args) throws Exception 
        //1、测试下载文件
        testDownload();
        //2、获取文件信息
        testGetInfo();
        //3、测试修改文件信息
        testSave();
    

    /**
     * 功能:测试下载文件
     */
    static void testDownload() throws Exception 
        String url = "http://127.0.0.1/baseService/wopi/files/123.docx/contents";
        String savePath="c:\\123.docx";
        InputStream is=FastHttpClient.get().url(url).build().execute().byteStream();
        FileUtil.saveContent(is, new File(savePath));
    

    /**
     * 功能:测试获取文件信息
     */
    static void testGetInfo() throws Exception 
        String url = "http://127.0.0.1/baseService/wopi/files/123.docx";
        String resp= FastHttpClient.get().url(url).build().execute().string();
        System.out.println(resp);
    

    /**
     * 功能:测试修改功能
     * 作者:黄海
     * 时间:2019-08-17
     *
     * @return
     * @throws Exception
     */
    static void testSave() throws Exception 
        String url = "http://127.0.0.1/baseService/wopi/files/1234.docx/contents";
        String fileName = "项目通知.docx";
        String filePath = "D:\\日常工作\\" + fileName;
        byte[] fileContent=FileUtil.getBytes(filePath);
        Response response = FastHttpClient.post().
                url(url).
                addFile("content", fileName, fileContent).
                build().
                execute();
        System.out.println(response.body().string());
    

 

以上是关于基于JFinal中搭建wopi协议支撑办法的主要内容,如果未能解决你的问题,请参考以下文章

基于开源博客系统(jpress)搭建网站

视频管理软件技术分析报告--VMS软件支撑技术分析

【Minio】基于AWS S3协议搭建个人云存储服务

Jfinal学习和环境的搭建

DNS服务器搭建

2018-12-09基于AWS S3协议搭建个人云存储服务