您现在的位置是:首页 > 文章详情

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

日期:2018-06-28点击:418
  1. 服务接口定义

/**

  • 文件上传 1:头像 2:显示图片 3:个人封面 :4:基础图片
  • @param request
  • @param response
  • @param uid 用户id
  • @param userType 文件上传 1:头像 2:显示图片 3:个人封面 :4:基础图片 0:视频
  • @param files 上传的文件对象
  • @return
  • @throws Exception

*/

@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封装

/**

  • 上传byte数组
  • @param fileByte
  • @param fileKey

*/

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

}

/**

  • 上传文件流
  • @param inputStream
  • @param fileKey

*/

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

}

/**

  • 删除文件
  • @param fileKey

*/

public void deleteFile(String fileKey){

// 创建OSSClient实例

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

// 删除文件

ossClient.deleteObject(CloudConstant.BUCKET, fileKey);

// 关闭client

ossClient.shutdown();

}

  1. 界面效果
原文链接:https://yq.aliyun.com/articles/604002
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章