开发者论坛

 找回密码
 注册 (请使用非IE浏览器)
查看: 2488|回复: 0

jeesz分布式架构集成阿里云oss存储

[复制链接]

0

精华

0

贡献

1

赞扬

帖子
51
软币
241
在线时间
8 小时
注册时间
2017-6-9
发表于 2017-6-21 15:07:36 | 显示全部楼层 |阅读模式
  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);
        }


2. 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();
    }
3. 界面效果

QQ图片20170621150629.png
QQ图片20170621150831.png


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

愿意了解框架技术或者源码的朋友直接求求交流分享技术:2042849237
更多详细源码参考来源:http://minglisoft.cn/technology



回复

使用道具 举报

Archiver|手机版|小黑屋|开发者网 ( 苏ICP备08004430号-2 )
版权所有:南京韵文教育信息咨询有限公司

GMT+8, 2024-4-26 10:56

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表