oss直传和回调

来源:互联网 发布:淘宝授权书模板 编辑:程序博客网 时间:2024/05/22 06:45

服务端签名直传并设置上传回调

生成数据签名

SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");//设置日期格式String dateString = df.format(new Date());// new Date()为获取当前系统时间String endpoint = ConstantsUtils.OSS_endpoint;String accessId = ConstantsUtils.OSS_accessId;String accessKey = ConstantsUtils.OSS_accessKey;String bucket = ConstantsUtils.OSS_bucket;String dir = dateString + "/";String host = "http://" + bucket + "." + endpoint;OSSClient client = new OSSClient(endpoint, accessId, accessKey);/// 由于回调函数涉及到数据库操作,因此oss调用回调函数时,需要带上改参数,简单的鉴权String calbackright = EncryptUtil.encrypt(ConstantsUtils.OSS_callback_md5 + orderid + ConstantsUtils.OSS_callback_md5,"MD5");try {    long expireTime = ConstantsUtils.OSS_expireTime;    long expireEndTime = System.currentTimeMillis() + expireTime * 1000;    Date expiration = new Date(expireEndTime);    PolicyConditions policyConds = new PolicyConditions();    policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);    policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir);    String postPolicy = client.generatePostPolicy(expiration, policyConds);    byte[] binaryData = postPolicy.getBytes("utf-8");    String encodedPolicy = BinaryUtil.toBase64String(binaryData);    String postSignature = client.calculatePostSignature(postPolicy);    Map<String, String> respMap = new LinkedHashMap<String, String>();    respMap.put("accessid", accessId);    respMap.put("policy", encodedPolicy);    respMap.put("signature", postSignature);    //respMap.put("expire", formatISO8601Date(expiration));    respMap.put("dir", dir);    respMap.put("host", host);    respMap.put("expire", String.valueOf(expireEndTime / 1000));    String callback = "{\"callbackUrl\":\"" + callbackurl + "\",\"callbackBody\":\"filename=${object}&size=${size}&mimeType=${mimeType}&orderid=" + orderid + "&calbackright=" + calbackright + "\",\"callbackBodyType\":\"application/x-www-form-urlencoded\"}";    byte[] bytes = Base64.encodeBase64(callback.getBytes("utf-8"));    respMap.put("callback", new String(bytes));    respMap.put("callback123", callback);    return ResponseResult.ok(respMap);} catch (Exception e) {    e.printStackTrace();}return ResponseResult.error("oss签名错误");

回调函数

public ResponseResult reportCallBack(String filename, String size, Long orderid) {    if (filename == null || size == null || orderid == null) {        return ResponseResult.error("有必填项为空");    }    // 需要的操作    return this.orderService.updateByid(order);}