com.aliyun.oss.common.utils.BinaryUtil Java Examples
The following examples show how to use
com.aliyun.oss.common.utils.BinaryUtil.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AliossTemplate.java From blade-tool with GNU Lesser General Public License v3.0 | 6 votes |
/** * TODO 上传大小限制、基础路径 * <p> * 获取上传凭证,普通上传 */ public String getUploadToken(String bucketName, long expireTime) { String baseDir = "upload"; long expireEndTime = System.currentTimeMillis() + expireTime * 1000; Date expiration = new Date(expireEndTime); PolicyConditions policyConds = new PolicyConditions(); // 默认大小限制10M policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, ossProperties.getArgs().get("contentLengthRange", 10485760)); policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, baseDir); String postPolicy = ossClient.generatePostPolicy(expiration, policyConds); byte[] binaryData = postPolicy.getBytes(StandardCharsets.UTF_8); String encodedPolicy = BinaryUtil.toBase64String(binaryData); String postSignature = ossClient.calculatePostSignature(postPolicy); Map<String, String> respMap = new LinkedHashMap<>(16); respMap.put("accessid", ossProperties.getAccessKey()); respMap.put("policy", encodedPolicy); respMap.put("signature", postSignature); respMap.put("dir", baseDir); respMap.put("host", getOssHost(bucketName)); respMap.put("expire", String.valueOf(expireEndTime / 1000)); return JsonUtil.toJson(respMap); }
Example #2
Source File: OSSUtil.java From anyline with Apache License 2.0 | 5 votes |
public Map<String,String> signature(String dir, int second) { if(second == 0){ second = config.EXPIRE_SECOND; } String host = ""; if(config.BUCKET.startsWith("http")){ host = config.BUCKET + "." + config.ENDPOINT; }else{ host = "https://" + config.BUCKET + "." + config.ENDPOINT; } Map<String,String> result = new HashMap<String,String>(); try { String postPolicy = policy(dir,second); byte[] binaryData = postPolicy.getBytes("utf-8"); String encodedPolicy = BinaryUtil.toBase64String(binaryData); String postSignature = client.calculatePostSignature(postPolicy); result.put("accessid", config.ACCESS_ID); result.put("policy", encodedPolicy); result.put("signature", postSignature); result.put("dir", dir); result.put("host", host); result.put("expire", String.valueOf((System.currentTimeMillis() + second * 1000)/1000)); } catch (Exception e) { e.printStackTrace(); } return result; }
Example #3
Source File: AliOssTemplate.java From magic-starter with GNU Lesser General Public License v3.0 | 5 votes |
/** * 获取上传凭证,普通上传 * TODO 上传大小限制、基础路径 * * @param bucketName 存储桶名称 * @param expireTime 过期时间,单位秒 * @return 上传凭证 */ public String getUploadToken(String bucketName, long expireTime) { String baseDir = "upload"; long expireEndTime = System.currentTimeMillis() + expireTime * 1000; Date expiration = new Date(expireEndTime); PolicyConditions policy = new PolicyConditions(); // 默认大小限制10M policy.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, ossProperties.getAliOss() .getArgs() .get("contentLengthRange", 1024 * 1024 * 10)); policy.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, baseDir); String postPolicy = ossClient.generatePostPolicy(expiration, policy); byte[] binaryData = postPolicy.getBytes(StandardCharsets.UTF_8); String encodedPolicy = BinaryUtil.toBase64String(binaryData); String postSignature = ossClient.calculatePostSignature(postPolicy); Map<String, String> respMap = new LinkedHashMap<>(16); respMap.put("accessid", ossProperties.getAliOss() .getAccessKey()); respMap.put("policy", encodedPolicy); respMap.put("signature", postSignature); respMap.put("dir", baseDir); respMap.put("host", getOssEndpoint(bucketName)); respMap.put("expire", String.valueOf(expireEndTime / 1000)); return JSONUtil.toJsonStr(respMap); }
Example #4
Source File: FileUploadController.java From unimall with Apache License 2.0 | 5 votes |
/** * 前台签名直传, 由服务器签名,用户可直接上传图片 * 这种只支持 Aliyun(因为我编码查看文档时,只有阿里云做了这个设计) 优点是 上传不需要占用应用服务器带宽。 目前前端是使用的这个。 * 若需要更改,请自行修改前端上传逻辑 * @param request * @param response */ @RequestMapping(method = RequestMethod.GET) public void upload(HttpServletRequest request, HttpServletResponse response) { try { long expireTime = 30; 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 = ossClient.generatePostPolicy(expiration, policyConds); byte[] binaryData = postPolicy.getBytes("utf-8"); String encodedPolicy = BinaryUtil.toBase64String(binaryData); String postSignature = ossClient.calculatePostSignature(postPolicy); JSONObject respJsonObj = new JSONObject(); respJsonObj.put("accessid", accessId); respJsonObj.put("policy", encodedPolicy); respJsonObj.put("signature", postSignature); respJsonObj.put("dir", dir); respJsonObj.put("host", host); respJsonObj.put("expire", String.valueOf(expireEndTime / 1000)); // respMap.put("expire", formatISO8601Date(expiration)); JSONObject jasonCallback = new JSONObject(); jasonCallback.put("callbackUrl", callbackUrl); jasonCallback.put("callbackBody", "filename=${object}&size=${size}&mimeType=${mimeType}&height=${imageInfo.height}&width=${imageInfo.width}"); jasonCallback.put("callbackBodyType", "application/x-www-form-urlencoded"); String base64CallbackBody = BinaryUtil.toBase64String(jasonCallback.toString().getBytes()); respJsonObj.put("callback", base64CallbackBody); // System.out.println(ja1.toString()); response.setHeader("Access-Control-Allow-Origin", "*"); response.setHeader("Access-Control-Allow-Methods", "GET, POST"); respJsonObj.put("code", 200); respJsonObj.put("baseUrl", baseUrl); response(request, response, respJsonObj.toJSONString()); } catch (Exception e) { logger.error("[上传签名] 异常", e); } }
Example #5
Source File: AliyunossProvider.java From jeesuite-libs with Apache License 2.0 | 4 votes |
@Override public Map<String, Object> createUploadToken(UploadTokenParam param) { Map<String, Object> result = new HashMap<>(); PolicyConditions policyConds = new PolicyConditions(); if(param.getFsizeMin() != null && param.getFsizeMax() != null){ policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, param.getFsizeMin(), param.getFsizeMax()); }else{ policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000); } if(param.getUploadDir() != null){ policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, param.getUploadDir()); } if(StringUtils.isBlank(param.getCallbackHost())){ param.setCallbackHost(host); } if(StringUtils.isBlank(param.getCallbackBody())){ param.setCallbackBody(DEFAULT_CALLBACK_BODY); } Date expire = DateUtils.addSeconds(new Date(), (int)param.getExpires()); String policy = ossClient.generatePostPolicy(expire, policyConds); String policyBase64 = null; String callbackBase64 = null; try { policyBase64 = BinaryUtil.toBase64String(policy.getBytes(StandardCharsets.UTF_8.name())); String callbackJson = param.getCallbackRuleAsJson(); if(callbackJson != null){ callbackBase64 = BinaryUtil.toBase64String(callbackJson.getBytes(StandardCharsets.UTF_8.name())); } } catch (Exception e) { throw new RuntimeException(e); } String signature = ossClient.calculatePostSignature(policy); result.put("OSSAccessKeyId", accessKeyId); result.put("policy", policyBase64); result.put("signature", signature); result.put("host", this.urlprefix); result.put("dir", param.getUploadDir()); result.put("expire", String.valueOf(expire.getTime())); if(callbackBase64 != null){ result.put("callback", callbackBase64); } return result; }