springboot对接上上签(三)

663 阅读2分钟

springboot对接上上签,经常遇到的方法和接口,我再这里给大家写了一些案例代码,大家可以学习一起研究了 创建合同有两种方式

  • 先上传文件再创建合同
  • 上传文件同时创建合同

上传合同

/**
    * 上传合同
    * @throws Exception
    */
   public  void uploadContract(FileInputStream file) throws Exception {
      String host = serverHost;
      String method = "/storage/upload/";
      // 生成一个时间戳参数
      String rtick = RSAUtils.getRtick();
//    FileInputStream file = new FileInputStream("d:\test\滴滴电子发票.pdf");
      byte[] bdata = IOUtils.toByteArray(file);
      String fmd5 = DigestUtils.md5Hex(bdata);
      String fdata = Base64.encodeBase64String(bdata);
      // 组装请求参数,作为requestbody
      JSONObject requestBody = new JSONObject();
      requestBody.put("account","xxx");
      requestBody.put("fmd5",fmd5);
      requestBody.put("ftype","pdf");
      requestBody.put("fname","合同.pdf");
      requestBody.put("fpages",2);
      requestBody.put("fdata",fdata);
      requestBody.put("isCleanup",0);
     // 计算参数签名
      String paramsSign = RSAUtils.calcRsaSign(developerId,
         privateKey, host, method, rtick, null,
         requestBody.toJSONString());
      // 签名参数追加为url参数
      String urlParams = String.format(urlSignParams, developerId,
         rtick, paramsSign);
      // 发送请求
      // 发送请求
      String responseBody = HttpClientSender.sendHttpPost(host, method,
         urlParams, requestBody.toJSONString());
      // 返回结果解析
      System.out.println("返回结果"+responseBody);
      // 返回errno为0,表示成功,其他表示失败
      // 返回结果解析
      JSONObject userObj = JSON.parseObject(responseBody);
      if (userObj.getIntValue("errno") == 0) {
         JSONObject data = userObj.getJSONObject("data");
         System.out.println(data);
      } else {
         throw new Exception(userObj.getIntValue("errno") + ":"
            + userObj.getString("errmsg"));
      }
   }

创建合同

/**
 * 创建合同
 *
 */
public static void createContract() throws IOException, ParseException {
   String host = serverHost;
   String method = "/contract/create/";

   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   String enddate="2021-08-01 00:00:00";
       long systime=System.currentTimeMillis()/1000L;
   Date endTime=sdf.parse(enddate);
   String rtick = RSAUtils.getRtick();
   JSONObject requestBody = new JSONObject();
   requestBody.put("account","xxxxxx");
   requestBody.put("fid","xxx");//文件id
   requestBody.put("expireTime",systime+604800);
   requestBody.put("title","测试合同");

   // 计算参数签名
   String paramsSign = RSAUtils.calcRsaSign(developerId,
      privateKey, host, method, rtick, null,
      requestBody.toJSONString());
   // 签名参数追加为url参数
   String urlParams = String.format(urlSignParams, developerId,
      rtick, paramsSign);
   // 发送请求
   // 发送请求
   String responseBody = HttpClientSender.sendHttpPost(host, method,
      urlParams, requestBody.toJSONString());

   System.out.println("返回结果"+responseBody);
}

上传并创建合同

/**
    * 上传并创建合同
    * @throws Exception
    */
// FileInputStream file,
   public   String uploadCContract(String name, FileInputStream file) throws Exception {
      String contractId="";
      String host = serverHost;
      String method = "/storage/contract/upload/";
      // 生成一个时间戳参数
      String rtick = RSAUtils.getRtick();
//    FileInputStream file = new FileInputStream("d:\test\滴滴电子发票.pdf");
      PdfReader pdfReader = new PdfReader(file);
      int pages = pdfReader.getNumberOfPages();//获取pdf的页数

      byte[] bdata = IOUtils.toByteArray(file);
      String fmd5 = DigestUtils.md5Hex(bdata);
      String fdata = Base64.encodeBase64String(bdata);
      // 组装请求参数,作为requestbody
      long systime=System.currentTimeMillis()/1000L;
      JSONObject requestBody = new JSONObject();
      requestBody.put("account","xxxx");
      requestBody.put("fmd5",fmd5);
      requestBody.put("ftype","pdf");
      requestBody.put("fname","合同.pdf");
      requestBody.put("fpages",pages);
      requestBody.put("fdata",fdata);
      requestBody.put("title",name);
      requestBody.put("expireTime",systime+604800+"");//默认7天有效期
      // 计算参数签名
      String paramsSign = RSAUtils.calcRsaSign(developerId,
         privateKey, host, method, rtick, null,
         requestBody.toJSONString());
      // 签名参数追加为url参数
      String urlParams = String.format(urlSignParams, developerId,
         rtick, paramsSign);
      // 发送请求
      // 发送请求
      String responseBody = HttpClientSender.sendHttpPost(host, method,
         urlParams, requestBody.toJSONString());
      // 返回结果解析
      System.out.println("返回结果"+responseBody);
      // 返回errno为0,表示成功,其他表示失败
      // 返回结果解析
      JSONObject userObj = JSON.parseObject(responseBody);
      if (userObj.getIntValue("errno") == 0) {
         JSONObject data = userObj.getJSONObject("data");
         System.out.println(data);
         contractId=data.getString("contractId");
      } else {
         throw new Exception(userObj.getIntValue("errno") + ":"
            + userObj.getString("errmsg"));
      }
      return contractId;
   }