(十九)java版spring cloud+spring boot+redis社交电子商务平台-Spring mvc+oss存储

电子商务平台源码请加企鹅求求:一零三八七七四六二六。之前给大家介绍了sso的相关知识点和集成方案,考虑到每个系统所属行业的不同,这边针对于不同行业做了一些统一的sso单点登录界面模板,使用fileupload多文件上传+OSS阿里云存储方案。

  1. 阿里云oss存储Utils
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");  
    }  
}  
  1. 阿里云配置常量类(可以配置到数据库或者properties,后面会更新配置方式)
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/";   
      
}  
  1. sso templateController类
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";  
    }  

你可能感兴趣的:((十九)java版spring cloud+spring boot+redis社交电子商务平台-Spring mvc+oss存储)