(十九)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";  
    }  
複製代碼
相關文章
相關標籤/搜索