ITPub博客

首页 > IT职业 > IT职场 > J2EE分布式架构集成阿里云OSS存储

J2EE分布式架构集成阿里云OSS存储

IT职场 作者:funnyok 时间:2021-09-09 09:52:43 0 删除 编辑

摘要: 1. 服务接口定义 /   文件上传  1:头像 2:显示图片 3:个人封面  :4:基础图片   @param request   @param response   @param uid 用户id   @param userType 文件上传  1:头像 2:显示图片 3:个人封面  :4:基础图片 0:视频   @param files 上传的文件对象  * @return

  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);

}

  1. 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();

}

3.界面效果

欢迎大家一起学习研究相关技术

愿意了解框架技术或者源码的朋友直接求求交流分享技术2042849237

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3137/viewspace-2813944/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 四:http模块
请登录后发表评论 登录
全部评论

注册时间:2007-12-07

  • 博文量
    95
  • 访问量
    238385