java 整合永中云预览模块

官网

SDK下载地址

https://cms.yozocloud.cn/info/file/getResource/81

DEMO下载地址

https://cms.yozocloud.cn/info/file/getResource/82

上传文件:上传本地文件至服务器,上传成功后会返回对应的fileID和fileVersionId,用户可以使用fileVersionId实现在线操作。

流程:前端选择文件  >   java后台接收  >  调用永中提供的接口上传文件。

生成签名(注意点:添加接口使用到的参数以及对应的参数值到集合中):

    private String getSign(Map<String, String[]> map) throws Exception {
        map.put("appId", new String[]{config.getAppId()});
        AppAuthenticator authenticator = new UaaAppAuthenticator(UaaConstant.SIGN, null, UaaConstant.APPID);
        String sign = authenticator.generateSign(config.getAppKey(), map);
        return sign;
    }

上传文件完整版

    @ResponseBody
    @PostMapping(value = "upload")
    public void getSessionToken(@RequestParam("file") MultipartFile multipartFile) throws Exception {

        Map<String, String[]> params = new HashMap<>();
        String sign = getSign(params);
        String url = "http://dmc.yozocloud.cn/api/file/upload?appId={0}&sign={1}";
        url = url.replace("{0}", config.getAppId()).replace("{1}", sign);
        System.out.println("url:" + url);
        //上传文件
        //String filePath = "g:\\学院专业关系表.xlsx";

        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);//文件类型是multipartFile类型
        //HttpHeaders fileHeader= new HttpHeaders();
        //fileHeader.setContentType(MediaType.parseMediaType(multipartFile.getContentType()));
        //fileHeader.setContentDispositionFormData("file", multipartFile.getOriginalFilename());

        //设置请求体,注意是LinkedMultiValueMap
        //HttpEntity<ByteArrayResource> httpEntity = new HttpEntity<>(new ByteArrayResource(multipartFile.getBytes()), fileHeader);
        MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
        //form.add("file", httpEntity);
        form.add("file", multipartFile.getResource());
        //用HttpEntity封装整个请求报文
        HttpEntity files = new HttpEntity<>(form, headers);

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> response = restTemplate.postForEntity(url, files, String.class);
        System.out.println("返回信息为: " + response.getBody());


        


    }

其中文件转换参考了https://www.cnblogs.com/super-chao/p/15338098.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值