立即注册
查看: 966|回复: 2

[原创] JEESZ分布式架构集成阿里云OSS存储

已绑定手机
发表于 2018-7-20 15:03:57 | 显示全部楼层 |阅读模式 来自 广东省惠州市

1. 服务接口定义

@RequestMapping(value = "/upload/base64Code", method = RequestMethod.POST)

public ResponseVO fileuploadBase64(@RequestBody JSONObject json, HttpServletRequest request, HttpServletResponse response) throws Exception {

String uid = json.optString("uid");

String userType = json.optString("userType");

String base64Code = json.optString("base64Code");

String oldName = json.optString("oldName");

String suffix = json.optString("suffix");

//获取当前登陆用户

if(StringUtils.isEmpty(uid)){

return CloUDResponseCode.buildEnumResponseVO(CloudResponseCode.USER_ID_NOT_NULL, null);

}

//上传文件新名字

String newName = String.valueOf(new Date().getTime());

String fileKey =  CloudConstant.VITAL_USER_INFO_PATH + uid + "/" + newName + "." + suffix;

AliyunUtils.getInstance().uploadByte(base64Code.getBytes(), fileKey);

......

.......

return CloudResponseCode.buildEnumResponseVO(CloudResponseCode.FILEUPLOAD_SUCCESS, obj);

}

2. oss utils封装


public void uploadByte(byte[] fileByte, String fileKey){

// 创建OSSClient实例

OSSClient ossClient = new OSSClient(CloudConstant.ENDPOINT, CloudConstant.ACCESSKEYID, CloudConstant.ACCESSKEYSECRET);

// 上传byte数组

ossClient.putObject(CloudConstant.BUCKET, fileKey, new ByteArrayInputStream(fileByte));

// 关闭client

ossClient.shutdown();

}


public void uploadInputStream(InputStream inputStream, String fileKey){

// 创建OSSClient实例

OSSClient ossClient = new OSSClient(CloudConstant.ENDPOINT, CloudConstant.ACCESSKEYID, CloudConstant.ACCESSKEYSECRET);

// 上传文件流

ossClient.putObject(CloudConstant.BUCKET, fileKey, inputStream);

// 关闭client

ossClient.shutdown();

}


public void deleteFile(String fileKey){

// 创建OSSClient实例

OSSClient ossClient = new OSSClient(CloudConstant.ENDPOINT, CloudConstant.ACCESSKEYID, CloudConstant.ACCESSKEYSECRET);

// 删除文件

ossClient.deleteObject(CloudConstant.BUCKET, fileKey);

// 关闭client

ossClient.shutdown();

}


已绑定手机
 楼主| 发表于 2018-7-20 15:08:57 | 显示全部楼层 来自 广东省惠州市
喜欢文章的朋友可以持续关注内容更新~
已绑定手机
发表于 2022-8-25 06:38:26 | 显示全部楼层 来自 浙江省杭州市
学习一下
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

合作/建议

TEL: 19168984579

工作时间:
周一到周五 9:00-11:30 13:30-19:30
  • 扫一扫关注公众号
  • 扫一扫打开小程序
Copyright © 2013-2024 一牛网 版权所有 All Rights Reserved. 帮助中心|隐私声明|联系我们|手机版|粤ICP备13053961号|营业执照|EDI证
在本版发帖搜索
扫一扫添加微信客服
QQ客服返回顶部
快速回复 返回顶部 返回列表