我是靠谱客的博主 想人陪小白菜,最近开发中收集的这篇文章主要介绍AliYun OSS 图片上传回调,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

  • pom.xml

<!-- 阿里云oss -->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.10.2</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-imageseg</artifactId>
<version>1.1.9</version>
</dependency>
<!-- 图片转MockMultipartFile -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.6.RELEASE</version>
<scope>compile</scope>
</dependency>

  • 获取前端上传参数返回给前端,
    callbackBody可自动自定义参数在回调接口接收
public static Map<String, String> getUploadParams(String userId) {
OSSClient client = new OSSClient(END_POINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
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, FILE_DIR);
String postPolicy = client.generatePostPolicy(expiration, policyConds);
byte[] binaryData = postPolicy.getBytes(StandardCharsets.UTF_8);
String encodedPolicy = BinaryUtil.toBase64String(binaryData);
String postSignature = client.calculatePostSignature(postPolicy);
Map<String, String> respMap = new LinkedHashMap<>();
respMap.put("accessid", ACCESS_KEY_ID);
respMap.put("policy", encodedPolicy);
respMap.put("signature", postSignature);
respMap.put("dir", FILE_DIR);
respMap.put("host", HOST);
String name = UUIDGenerator.getUUID(8) + "_";
respMap.put("name", name + TimeUtils.getCurrentTime() + ".png");
respMap.put("expire", String.valueOf(expireEndTime / 1000));
JSONObject jasonCallback = new JSONObject();
jasonCallback.put("callbackUrl", CALLBACK_URL);
Map<String, Object> map = new HashMap<>();
map.put("userId", userId);
map.put("fileUrl", FILE_DIR + name + TimeUtils.getCurrentTime() + ".png");
jasonCallback.put("callbackBody", JSON.toJSONString(map));
jasonCallback.put("callbackBodyType", "application/json");
String base64CallbackBody = BinaryUtil.toBase64String(jasonCallback.toString().getBytes());
respMap.put("callback", base64CallbackBody);
System.out.println(JSON.toJSONString(respMap));
return respMap;
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
return null;
}
}
  • 回调接口 :返回给前端的参数写在这里
public JSONObject callBack(JSONObject jsonObject) {
JSONObject result = new JSONObject();
try {
PortraitSplitLog log = new PortraitSplitLog();
String request = jsonObject.toJSONString();
System.out.println("=====callbackn" + request);
String imageUrl = jsonObject.getString("fileUrl");
String userId = jsonObject.getString("userId");
log.setOriginalUrl(imageUrl);
log.setUserId(userId);
log.setDateline(TimeUtils.getCurrentUnixTime());
RestResult restResult = AliYunOSSUtils.portraitSplit(imageUrl);
if (restResult.getCode() != 200) {
result.put("message", restResult.getMessage());
result.put("status", "err");
log.setErrMsg(restResult.getMessage());
return result;
}
JSONObject object = JSONObject.parseObject(restResult.getData().toString());
log.setResult(object.toJSONString());
String requestId = object.getString("requestId");
log.setRequestId(requestId);
JSONObject dataObject = object.getJSONObject("data");
System.out.println("dataObject=" + dataObject);
JSONArray elArray = dataObject.getJSONArray("elements");
System.out.println(elArray.size());
if (elArray.size() > 1) {
System.out.println("请上传单人照片进行识别");
result.put("message", "请上传单人照片进行识别");
result.put("status", "err");
log.setErrMsg(restResult.getMessage());
return result;
}
JSONObject o = (JSONObject) elArray.get(0);
String url = o.getString("imageURL");
String splitUrl = AliYunOSSUtils.uploadImageWithUrl(url, imageUrl);
Long w = o.getLong("width");
Long h = o.getLong("height");
Long x = o.getLong("x");
Long y = o.getLong("y");
log.setSplitUrl(splitUrl);
log.setWidth(w);
log.setHeight(h);
log.setX(x);
log.setY(y);
portraitSplitLogService.add(log);
result.put("data", log.getSplitUrl());
result.put("status", "ok");
} catch (Exception e) {
e.printStackTrace();
result.put("message", "处理过程出错");
result.put("status", "ok");
}
return result;
}
  •  阿里云人脸分割
 public static RestResult portraitSplit(String imageUrl) {
try {
DefaultProfile profile = DefaultProfile.getProfile(REGION_ID, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
IAcsClient client = new DefaultAcsClient(profile);
SegmentHeadRequest request = new SegmentHeadRequest();
request.setRegionId(REGION_ID);
request.setImageURL(HOST + "/" + imageUrl);
request.setReturnForm("PNG");
SegmentHeadResponse response = client.getAcsResponse(request);
JSONObject object = JSONObject.parseObject(JSON.toJSONString(response));
return RestResult.ok(object);
} catch (com.aliyuncs.exceptions.ClientException e) {
System.out.println("ErrCode:" + e.getErrCode());
System.out.println("ErrMsg:" + e.getErrMsg());
System.out.println("RequestId:" + e.getRequestId());
if ("InvalidFile.Content".equals(e.getErrCode())) {//输入图像内容不合法。例如:1输入的image是空字符串。2无效的base64编码图像。3无效的image content。
return RestResult.error("没有识别到人脸,请重新选择图片");
}
if ("InvalidImage.Download".equals(e.getErrCode())) {
return RestResult.error("图片下载失败");
}
if ("InvalidParameter".equals(e.getMessage())) {
return RestResult.error("图片处理参数有误");
}
return RestResult.error("图片分割失败,请重新上传");
}
}

最后

以上就是想人陪小白菜为你收集整理的AliYun OSS 图片上传回调的全部内容,希望文章能够帮你解决AliYun OSS 图片上传回调所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(26)

评论列表共有 0 条评论

立即
投稿
返回
顶部