JAVA b2b2c多用戶商城系統源碼-Spring mvc+oss存儲

之前給大家介紹了sso的相關知識點和集成方案,考慮到每個系統所屬行業的不同,這邊針對於不同行業做了一些統一的sso單點登錄界面模板,使用fileupload多文件上傳+OSS阿里雲存儲方案。

1.阿里雲oss存儲Utils

      
    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,後面會更新配置方式)


 `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類
    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";  
}  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章