1. 程式人生 > 其它 >阿里雲Oss(圖片上傳),SMS(傳送簡訊驗證)

阿里雲Oss(圖片上傳),SMS(傳送簡訊驗證)

檔案上傳
之前做的檔案上傳是將檔案上傳到tomcat本地硬碟上,然後通過虛擬路徑訪問該檔案。
但是如果在tomcat叢集環境,這種方案肯定是不行的,所以需要用分散式的檔案系統來存取檔案。
這裡我們使用阿里雲的oss服務,來儲存檔案資料(圖片、小視訊、js、css.html)

1.阿里雲登陸註冊

https://help.aliyun.com/learn/learningpath/oss.html

建立bucket

獲取四個關鍵屬性

AccessKey LTAI5tBxZTXL91xi8hBekESS
AccessKey Secret YXhkQIfHeyfhxWa9lhTbTfCIfJ3EKy
伺服器域名Endpoint oss-cn-beijing.aliyuncs.com
資料倉庫 bucket zkp-01

2.OSS程式碼實現

  1. 導包
        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <version>3.1.0</version>
        </dependency>
  1. 引入工具類OSSClientUtil修改屬性為自己的bucket
    // endpoint  域名 機房
    private String endpoint = "oss-cn-beijing.aliyuncs.com";
    // accessKey  使用者的Id
    private String accessKeyId = "LTAI5tBxZTXL91xi8hBekESS";
    // 使用者的祕鑰
    private String accessKeySecret = "YXhkQIfHeyfhxWa9lhTbTfCIfJ3EKy";
    // 倉庫
    private String bucketName = "zkp-01";
    // 檔案儲存目錄
    private String filedir = "images/";
  1. 在controller當中呼叫
    //非同步上傳照片
    @RequestMapping("/doAddPhoto")
    public @ResponseBody boolean doAddPhoto(@RequestParam("file") MultipartFile file,HttpSession session, String picName) throws IOException {
        User user = (User)session.getAttribute("user");
        if (!file.isEmpty()){//檔案非空,上傳到伺服器
            //使用時建立一個,多例模式
            OSSClientUtil ossClientUtil = new OSSClientUtil();
            ossClientUtil.uploadFile2OSS(file.getInputStream(),file.getOriginalFilename());
            //獲取網路地址,通過檔名稱
            String url = ossClientUtil.getUrl(file.getOriginalFilename());
            //建立使用者相片物件,新增新資訊
            UserProfilePhoto userPhoto = new UserProfilePhoto();
            userPhoto.setPicUrl(url);
            userPhoto.setPicName(picName);
            userPhoto.setUserId(user.getId());
            PhotoService.insert(userPhoto);
            return true;
        }else {
            return false;
        }
    }

3.SMS(傳送簡訊,實現手機號簡訊登陸)

  1. 導包
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
            <version>1.1.0</version>
        </dependency>
  1. 引入工具類AliSmsUtil,修改部分屬性
//產品域名,開發者無需替換,以下常量基本不變
private static final String DOMAIN = "dysmsapi.aliyuncs.com";

private static final String REGION_ID = "cn-hangzhou";
private static final String PRODUCT="Dysmsapi";

public static final String ACCESS_KEY_ID = "LTAI4GHFHhok7nJ7cA77GSw9";
public static final String ACCESS_SECRET = "mGr8Q4D7mDFksn0VJA31S421M89ncU";
public static final String DEFAULT_SIGNNAME = "ABC商城";
public static final String DEFAULT_TEMPLATE_CODE = "SMS_206420122";
  1. 建立一個執行緒類SendSmsTask,進行非同步呼叫
public class SendSmsTask implements  Runnable {
    private AliSmsUtil aliSmsUtil;
    private String phone;
    private String code;

    public SendSmsTask(AliSmsUtil aliSmsUtil, String phone,String code) {
        this.aliSmsUtil = aliSmsUtil;
        this.phone = phone;
        this.code=code;
    }

    @Override
    public void run() {
        try {
            aliSmsUtil.sendValidateCodeMessage2AliyunSms(phone, code);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  1. 具體業務
//簡訊驗證碼登陸
    @RequestMapping("/msgLogin")
    public @ResponseBody boolean msgLogin(String code,HttpSession session){

        //獲得使用者驗證碼,與redis中的進行比對
        String codes =(String) redisTemplate.boundValueOps("msg").get();
        System.out.println(code);
        System.out.println(codes);
        if (code.equals(codes)){//使用者登陸成功
            User user = userService.selectByPrimaryKey(3);
            List<Menu> menuList = menuService.findMenuByUserId(user.getId());  
            session.setAttribute("menuList",menuList);
            session.setAttribute("user",user);
            return true;
        }else {
            return false;
        }
    }

    //傳送驗證碼
    @RequestMapping("/sendMsg")
    public @ResponseBody ResponseVo sendMsg(String phone) {
        ResponseVo responseVo = new ResponseVo();
        responseVo.setCode("1");
        String code = "";
        //生成隨機數
        Random random = new Random();
        for (int i=1;i<=6;i++){
            code += ""+ random.nextInt(10);
        }
        //將驗證嗎放入redis
        redisTemplate.boundValueOps("msg").set(code);
        System.out.println("code="+code);
        SendSmsTask sendSmsTask = new SendSmsTask(new AliSmsUtil(),phone,code);
        sendSmsTask.run();
        return responseVo;
    }