前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring mvc+oss存储+fileupload多文件上传实现SSO单点登录模板管理

Spring mvc+oss存储+fileupload多文件上传实现SSO单点登录模板管理

原创
作者头像
用户7788846
修改2020-11-27 18:06:59
1.5K0
修改2020-11-27 18:06:59
举报
文章被收录于专栏:java版springcloudjava版springcloud

之前给大家介绍了sso的相关知识点和集成方案,考虑到每个系统所属行业的不同,这边针对于不同行业做了一些统一的sso单点登录界面模板,使用fileupload多文件上传+OSS阿里云存储方案。

1. 阿里云oss存储Utils

代码语言:javascript
复制
public class AliyunUtils {  
      
    private static AliyunUtils aliyun;  
    private AliyunUtils() {  
          
    }  
  
    public static synchronized AliyunUtils getInstance(){  
        if(aliyun==null){  
            aliyun=new AliyunUtils();  
        }  
        return aliyun;  
    }  
      
    /** 
     * 上传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();  
    }  
      
    //base64字符串转化成图片    
    @SuppressWarnings("restriction")  
    public static byte[] BASE64DecoderStringToByte(String imgStr)    
    {   //对字节数组字符串进行Base64解码并生成图片    
        if (imgStr == null) //图像数据为空    
            return null;    
        sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();    
        try {    
            //Base64解码    
            byte[] b = decoder.decodeBuffer(imgStr);    
           return b;  
        } catch (Exception e){    
            return null;    
        }    
    }  
      
    public static void main(String[] args) {  
        //AliyunUtils.getInstance().uploadByte(BASE64DecoderStringToByte(base64), "aabb.jpg");  
    }  
}  

 2. 阿里云配置常量类(可以配置到数据库或者properties,后面会更新配置方式)(企业架构源码可以加求球:三五三六二四七二五九)

代码语言:javascript
复制
public class CloudConstant {  
      
    /****************阿里云OSS上传文件配置****************/  
    public static final String ENDPOINT = "http://oss-cn-shanghai.aliyuncs.com"; //外网访问域名  
    //public static final String ENDPOINT = "http://oss-cn-shanghai-internal.aliyuncs.com"; //内网访问域名  
    public static final String ACCESSKEYID = "12345678qwertyu; //标识用户  
    public static final String ACCESSKEYSECRET = "1234567890WERTYUIO"; //加密签名字符  
    public static final String BUCKET = "huiyi-bucket"; //存储空间  
      
    /****************背景文件路径配置****************/  
    public static final String BACK_IMG_INFO_PATH = "sso/backageImg/";   
      
}  

 3. sso templateController类

代码语言:javascript
复制
public String save(SsoTemplate ssoTemplate, Model model, RedirectAttributes redirectAttributes, @RequestParam(value = "file", required = false) MultipartFile file) {  
        if (!beanValidator(model, ssoTemplate)) {  
            return form(ssoTemplate, model);  
        }  
  
        String fileName = String.valueOf(new Date().getTime());  
        String staff = "";  
        String fileKey = "";  
        // 上传文件  
        if (file.getSize() != 0) {  
            staff = FilenameUtils.getExtension(file.getOriginalFilename());  
            fileKey = CloudConstant.BACK_IMG_INFO_PATH + fileName + "." + staff;  
            // 删除OSS文件  
            AliyunUtils.getInstance().deleteFile(fileKey);  
            // 上传文件至阿里云OSS  
            try {  
                AliyunUtils.getInstance().uploadInputStream(file.getInputStream(), fileKey);  
                ssoTemplate.setImg(fileKey);  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
          
        ssoTemplateService.save(ssoTemplate);  
        addMessage(redirectAttributes, "保存模板成功");  
        return "redirect:" + Global.getAdminPath() + "/sso/ssoTemplate";  
    }  

 4. 界面显示效果

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档