永中office之在线预览(java版)

之前因为项目需要,需要用到文件在线预览功能,上网查了后发现一个好用的在线预览——永中office云预览
永中office云预览主要是通过客户端上传文件到永中自己的服务器,之后服务器会返回给客户端一段json体的值,解析json里面值后拼接url跳转到永中自己的预览服务器里面,这样就实现在线预览功能。
1、首先导入依赖jar,是生成签名的jar
链接:https://pan.baidu.com/s/13PyAyPIMz4PYLDab7Ue6tg
提取码:7kam
生成签名sign的方法,map里面转递的参数为url上面拼接的参数,如上传文件的url上需要拼接appId,sign(除外),则就传递appId进来,sign除外,如预览文件的url上需要拼接appId,fileVersionId,sign(除外),则传递appId,fileVersionId进来。

/**
     * 获取签名信息
     * @param map 参数k-v
     * @return 签名
     * @throws Exception 异常
     */
    String getSign(Map<String,String[]> map) throws Exception {
        map.put("appId",new String[]{this.appId});
        AppAuthenticator authenticator=new UaaAppAuthenticator(UaaConstant.SIGN,null,UaaConstant.APPID);
        String sign = authenticator.generateSign(this.appKey, map);
        return sign;
    }

2、上传要预览的文件到永中的服务器,在这里我用的是java的HttpURLConnection来上传文件

//生成sign
            String sign = getSign(new HashMap<>());

            File file = new File("E:\\新建 Microsoft Word 文档.docx");
            String urlString = httpUrl + "file/upload?appId=" + appId + "&sign=" + sign;
            URL url = new URL(urlString);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            String Boundary = UUID.randomUUID().toString(); // 文件边界

            // 1.开启Http连接
            conn.setConnectTimeout(10 * 1000);
            conn.setDoOutput(true); // 允许输出

            // 2.Http请求行/头
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Charset", "utf-8");
            conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + Boundary);

            // 3.Http请求体
            DataOutputStream out = new DataOutputStream(conn.getOutputStream());
            out.writeUTF("--" + Boundary + "\r\n"
                    + "Content-Disposition: form-data; name=file; filename=" + file.getName() + "\r\n"
                    + "Content-Type: application/octet-stream; charset=utf-8" + "\r\n\r\n");
            InputStream in = new FileInputStream(file);
            byte[] b = new byte[1024];
            int l = 0;
            while ((l = in.read(b)) != -1) out.write(b, 0, l); // 写入文件
            out.writeUTF("\r\n--" + Boundary + "--\r\n");
            out.flush();
            out.close();
            in.close();

3,获取响应码和响应体,获取fileVersionId跳转到预览服务器

int responseCode = conn.getResponseCode();
            if (responseCode == 200) {
                // 4.Http响应
                BufferedReader bf = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
                String line = null;
                StringBuffer stringBuffer = new StringBuffer();
                while ((line = bf.readLine()) != null) {
                    System.out.println(line);
                    stringBuffer.append(line);
                }
                JSONObject jsonObject = JSONObject.parseObject(stringBuffer.toString());
                JSONObject data = jsonObject.getJSONObject("data");
                if (!"".equals(data.toJSONString()) && !"".equals(data.getString("fileVersionId"))) {
                    //获取返回的文件id
                    String fileVersionId = data.getString("fileVersionId");
                    //获取签名信息
                    Map<String, String[]> paramMap = new HashMap<>();
                    paramMap.put("fileVersionId", new String[]{fileVersionId});
                    String s = getSign(paramMap);
                    return "redirect:" + this.viewFileHttpUrl + "view/file?appId=" + this.appId + "&fileVersionId=" + fileVersionId + "&sign=" + s;
                }
            }

配置文件里面的值:

appId=yozobCmWHU365448
appKey=7b1c522e15bc1e136da5404ad533
httpUrl=http://dmc.yozocloud.cn/api/
viewFileHttpUrl=http://eic.yozocloud.cn/api/

获取配置文件值

 @Value("${appId}")
    String appId;

    @Value("${appKey}")
    String appKey;

    @Value("${httpUrl}")
    String httpUrl;

    @Value("${viewFileHttpUrl}")
    String viewFileHttpUrl;

用office打开效果
在这里插入图片描述

预览效果
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值