OfficeOnline 使用

package com.datashare.datasharebusinessserver.lyqx.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.datashare.bean.ConfigBean;
import com.datashare.datasharebusinessserver.dao.FileInfo;
import com.datashare.datasharebusinessserver.util.propertiesUtil;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import com.fasterxml.jackson.databind.ObjectMapper;

@RestController
@RequestMapping(value = “/v1/officeOnline”)
public class OfficeOnlineController {

@Resource
private ConfigBean configBean;


//private String officeServer= propertiesUtil.readPropertyFile("PSapplication.properties","office.http.server.host.ip");

private static final String CHARSET_UTF8 = "UTF-8";
/**
 * office跳转
 *
 * @param path
 * @param response
 * @throws IOException
 */

@RequestMapping(value = "/officeView")
@ResponseBody
public void officeView(HttpServletResponse response, String path) throws IOException {
    System.out.println("officeServer"+configBean.getOfficeOnlineIp());
    String officeUrl = configBean.getOfficeOnlineWopiost();
    String urlTmp = "http://" + configBean.getOfficeOnlineIp() + "/{aspx}?WOPISrc=" + officeUrl;
    System.out.println("officeUrl"+officeUrl);
    String aspx = "";
    if (path.endsWith(".pptx") || path.endsWith(".PPTX") || path.endsWith(".ppt") || path.endsWith(".PPT")) {
        aspx = "p/PowerPointFrame.aspx";
    } else if (path.endsWith(".doc") || path.endsWith(".docx") || path.endsWith(".DOCX") || path.endsWith(".DOC")) {
        aspx = "wv/wordviewerframe.aspx";
    } else if (path.endsWith(".xlsx") || path.endsWith(".XLSX") || path.endsWith(".xls") || path.endsWith(".XLS")) {
        aspx = "x/_layouts/xlviewerinternal.aspx";
    }
    path = path.replaceAll("/", "_q_");
    path = path.replaceAll("\\.", "_d_");
    path = path.replaceAll(" ", "_K_");
    urlTmp = urlTmp.replace("{aspx}", aspx);
    path = URLEncoder.encode(path,CHARSET_UTF8);

// path = URLEncoder.encode(path,CHARSET_UTF8);
urlTmp = urlTmp.replace("{path}", path);
System.out.println(“urlTmp”+urlTmp);
response.sendRedirect(urlTmp);
}

@RequestMapping(value = "/wopi/files/{path}", method = RequestMethod.GET)
public void getFileWopiInfo(@PathVariable("path") String path, HttpServletResponse response) {
    System.out.println("path"+path);
    FileInfo info = new FileInfo();
    try {
        //逗号转义
        path = path.replace("_d_", ".");
        //目录转义
        path = path.replace("_q_", "/");
        path = path.replace("_K_", " ");
        //文件路径
        String filePath = URLDecoder.decode(path, "UTF-8");
        //共通路径
        String officePath = configBean.getOfficeOnlinePath();
        if(officePath == null)
            officePath = "";
        String fileName = officePath + filePath;
        if (fileName != null && fileName.length() > 0) {
            File file = new File(fileName);
            if (file.exists()) {
                info.setBaseFileName(file.getName());
                info.setSize(file.length());
                info.setOwnerId("admin");
                info.setVersion(file.lastModified());
                info.setSha256(getHash256(file));
            }
        }
        ObjectMapper mapper = new ObjectMapper();
        response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
        response.getWriter().write(mapper.writeValueAsString(info));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@RequestMapping(value = "/wopi/files/{path}/contents", method = RequestMethod.GET)
public void downFile(@PathVariable("path") String path, HttpServletResponse response) {
    //逗号转义
    path = path.replace("_d_", ".");
    //目录转义
    path = path.replace("_q_", "/");
    path = path.replace("_K_", " ");
    //共通路径
    String officePath = configBean.getOfficeOnlinePath();
    if(officePath == null)
        officePath = "";
    String fileName = officePath + path;
    File file = new File(fileName);
    String filename = file.getName();

    try (InputStream fis = new BufferedInputStream(new FileInputStream(fileName));
         OutputStream toClient = new BufferedOutputStream(response.getOutputStream())) {
        byte[] buffer = new byte[fis.available()];
        fis.read(buffer);
        // 清空response
        response.reset();

        // 设置response的Header
        response.addHeader("Content-Disposition", "attachment;filename=" +
                new String(filename.getBytes(CHARSET_UTF8), "ISO-8859-1"));
        response.addHeader("Content-Length", String.valueOf(file.length()));

        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        toClient.write(buffer);
        toClient.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

/**
 * 获取文件的SHA-256值
 * @param file
 * @return
 */
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.encodeBase64(digest.digest()));
    }
    return value;
}

}
加粗样式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值