ITPub博客

首页 > 应用开发 > Java > Spring mvc+oss存储+fileupload多文件上传实现SSO单点登录模板管理

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

原创 Java 作者:我祈美如画 时间:2020-11-12 11:14:19 0 删除 编辑

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

1. 阿里云oss存储Utils

Java代码   收藏代码
  1. public   class  AliyunUtils {  
  2.       
  3.      private   static  AliyunUtils aliyun;  
  4.      private  AliyunUtils() {  
  5.           
  6.     }  
  7.   
  8.      public   static   synchronized  AliyunUtils getInstance(){  
  9.          if (aliyun== null ){  
  10.             aliyun= new  AliyunUtils();  
  11.         }  
  12.          return  aliyun;  
  13.     }  
  14.       
  15.      /** 
  16.      * 上传byte数组 
  17.      * @param fileByte 
  18.      * @param fileKey  
  19.      */   
  20.      public   void  uploadByte( byte [] fileByte, String fileKey){  
  21.          // 创建OSSClient实例   
  22.         OSSClient ossClient =  new  OSSClient(CloudConstant.ENDPOINT, CloudConstant.ACCESSKEYID, CloudConstant.ACCESSKEYSECRET);  
  23.          // 上传byte数组   
  24.         ossClient.putObject(CloudConstant.BUCKET, fileKey,  new  ByteArrayInputStream(fileByte));  
  25.          // 关闭client   
  26.         ossClient.shutdown();  
  27.     }  
  28.       
  29.      /** 
  30.      * 上传文件流 
  31.      * @param inputStream 
  32.      * @param fileKey 
  33.      */   
  34.      public   void  uploadInputStream(InputStream inputStream, String fileKey){  
  35.          // 创建OSSClient实例   
  36.         OSSClient ossClient =  new  OSSClient(CloudConstant.ENDPOINT, CloudConstant.ACCESSKEYID, CloudConstant.ACCESSKEYSECRET);  
  37.          // 上传文件流   
  38.         ossClient.putObject(CloudConstant.BUCKET, fileKey, inputStream);  
  39.          // 关闭client   
  40.         ossClient.shutdown();  
  41.     }  
  42.       
  43.      /** 
  44.      * 删除文件 
  45.      * @param fileKey 
  46.      */   
  47.      public   void  deleteFile(String fileKey){  
  48.          // 创建OSSClient实例   
  49.         OSSClient ossClient =  new  OSSClient(CloudConstant.ENDPOINT, CloudConstant.ACCESSKEYID, CloudConstant.ACCESSKEYSECRET);  
  50.          // 删除文件   
  51.         ossClient.deleteObject(CloudConstant.BUCKET, fileKey);  
  52.          // 关闭client   
  53.         ossClient.shutdown();  
  54.     }  
  55.       
  56.      //base64字符串转化成图片     
  57.      @SuppressWarnings ( "restriction" )  
  58.      public   static   byte [] BASE64DecoderStringToByte(String imgStr)    
  59.     {    //对字节数组字符串进行Base64解码并生成图片     
  60.          if  (imgStr ==  null )  //图像数据为空     
  61.              return   null ;    
  62.         sun.misc.BASE64Decoder decoder =  new  sun.misc.BASE64Decoder();    
  63.          try  {    
  64.              //Base64解码     
  65.              byte [] b = decoder.decodeBuffer(imgStr);    
  66.             return  b;  
  67.         }  catch  (Exception e){    
  68.              return   null ;    
  69.         }    
  70.     }  
  71.       
  72.      public   static   void  main(String[] args) {  
  73.          //AliyunUtils.getInstance().uploadByte(BASE64DecoderStringToByte(base64), "aabb.jpg");   
  74.     }  
  75. }  

 2. 阿里云配置常量类(可以配置到数据库或者properties,后面会更新配置方式)

Java代码   收藏代码
  1. public   class  CloudConstant {  
  2.       
  3.      /****************阿里云OSS上传文件配置****************/   
  4.      public   static   final  String ENDPOINT =  "http://oss-cn-shanghai.aliyuncs.com" ; //外网访问域名  
  5.      //public static final String ENDPOINT = "http://oss-cn-shanghai-internal.aliyuncs.com"; //内网访问域名   
  6.      public   static   final  String ACCESSKEYID = "12345678qwertyu;  //标识用户   
  7.      public   static   final  String ACCESSKEYSECRET =  "1234567890WERTYUIO" ;  //加密签名字符   
  8.      public   static   final  String BUCKET =  "huiyi-bucket" ;  //存储空间   
  9.       
  10.      /****************背景文件路径配置****************/   
  11.      public   static   final  String BACK_IMG_INFO_PATH =  "sso/backageImg/" ;   
  12.       
  13. }  

 3. sso templateController类

Java代码   收藏代码
  1. public  String save(SsoTemplate ssoTemplate, Model model, RedirectAttributes redirectAttributes,  @RequestParam (value =  "file" , required =  false ) MultipartFile file) {  
  2.          if  (!beanValidator(model, ssoTemplate)) {  
  3.              return  form(ssoTemplate, model);  
  4.         }  
  5.   
  6.         String fileName = String.valueOf( new  Date().getTime());  
  7.         String staff =  "" ;  
  8.         String fileKey =  "" ;  
  9.          // 上传文件   
  10.          if  (file.getSize() !=  0 ) {  
  11.             staff = FilenameUtils.getExtension(file.getOriginalFilename());  
  12.             fileKey = CloudConstant.BACK_IMG_INFO_PATH + fileName +  "."  + staff;  
  13.              // 删除OSS文件   
  14.             AliyunUtils.getInstance().deleteFile(fileKey);  
  15.              // 上传文件至阿里云OSS   
  16.              try  {  
  17.                 AliyunUtils.getInstance().uploadInputStream(file.getInputStream(), fileKey);  
  18.                 ssoTemplate.setImg(fileKey);  
  19.             }  catch  (IOException e) {  
  20.                 e.printStackTrace();  
  21.             }  
  22.         }  
  23.           
  24.         ssoTemplateService.save(ssoTemplate);  
  25.         addMessage(redirectAttributes,  "保存模板成功" );  
  26.          return   "redirect:"  + Global.getAdminPath() +  "/sso/ssoTemplate" ;  
  27.     }  

 4. 界面显示效果

 

 

 

 

 


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

请登录后发表评论 登录
全部评论

注册时间:2020-10-05

  • 博文量
    33
  • 访问量
    16576